<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?php

namespace Clue\PharComposer\Phar;

use Clue\PharComposer\Package\Bundle;

/**
 * Represents the target phar to be created.
 */
class TargetPhar
{
    /** @var \Phar */
    private $phar;

    /** @var  PharComposer */
    private $pharComposer;

    public function __construct(\Phar $phar, PharComposer $pharComposer)
    {
        $phar->startBuffering();
        $this->phar = $phar;
        $this->pharComposer = $pharComposer;
    }

    /**
     * finalize writing of phar file
     */
    public function stopBuffering()
    {
        $this->phar->stopBuffering();
    }

    /**
     * adds given list of resources to phar
     *
     * @param  Bundle  $bundle
     */
    public function addBundle(Bundle $bundle)
    {
        foreach ($bundle as $resource) {
            if (is_string($resource)) {
                $this->addFile($resource);
            } else {
                $this->buildFromIterator($resource);
            }
        }
    }

     /**
     * Adds a file to the Phar
     *
     * @param string $file  The file name.
     */
    public function addFile($file)
    {
        $this->phar->addFile($file, $this->pharComposer->getPathLocalToBase($file));
    }

    public function buildFromIterator(\Traversable $iterator)
    {
        $this->phar->buildFromIterator($iterator, $this->pharComposer->getPackageRoot()->getDirectory());
    }

    /**
     * Used to set the PHP loader or bootstrap stub of a Phar archive
     *
     * @param  string $stub
     */
    public function setStub($stub)
    {
        $this->phar->setStub($stub);
    }

    public function addFromString($local, $contents)
    {
        $this->phar->addFromString($local, $contents);
    }
}
