Example #1
0
 /**
  * @override
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->config = $this->getConfig($input);
     $path = $this->config->getOutputPath();
     // load bootstrap file
     if (null !== ($bootstrap = $this->config->getBootstrapFile())) {
         $this->config->loadBootstrap();
         $this->putln('?', "Loading bootstrap file: {$bootstrap}");
         unset($bootstrap);
     }
     // remove any previous work
     if (file_exists($path)) {
         $this->putln('?', 'Removing previously built Phar...');
         unlink($path);
     }
     // set up Crate
     if ($this->isVerbose()) {
         $this->putln('*', 'Building...');
     } else {
         $output->writeln('Building...');
     }
     $this->putln('?', "Output path: {$path}");
     $this->crate = Crate::create($path);
     $this->crate->getPhar()->startBuffering();
     // set replacement values, if any
     if (array() !== ($values = $this->config->getProcessedReplacements())) {
         $this->putln('?', 'Setting replacement values...');
         if ($this->isVerbose()) {
             foreach ($values as $key => $value) {
                 $this->putln('+', "{$key}: {$value}");
             }
         }
         $this->crate->setValues($values);
         unset($values, $key, $value);
     }
     // register configured compactors
     if (array() !== ($compactors = $this->config->getCompactors())) {
         $this->putln('?', 'Registering compactors...');
         foreach ($compactors as $compactor) {
             $this->putln('+', get_class($compactor));
             $this->crate->addCompactor($compactor);
         }
     }
     // alert about mapped paths
     if (array() !== ($map = $this->config->getMap())) {
         $this->putln('?', 'Mapping paths:');
         foreach ($map as $item) {
             foreach ($item as $match => $replace) {
                 if (empty($match)) {
                     $match = "(all)";
                 }
                 $this->putln('-', "{$match} <info>></info> {$replace}");
             }
         }
     }
     // start adding files
     if (array() !== ($iterators = $this->config->getFinders())) {
         $this->putln('?', 'Adding Finder files...');
         foreach ($iterators as $iterator) {
             $this->add($iterator, null);
         }
     }
     if (array() !== ($iterators = $this->config->getBinaryFinders())) {
         $this->putln('?', 'Adding binary Finder files...');
         foreach ($iterators as $iterator) {
             $this->add($iterator, null, true);
         }
     }
     $this->add($this->config->getDirectoriesIterator(), 'Adding directories...');
     $this->add($this->config->getBinaryDirectoriesIterator(), 'Adding binary directories...', true);
     $this->add($this->config->getFilesIterator(), 'Adding files...');
     $this->add($this->config->getBinaryFilesIterator(), 'Adding binary files...', true);
     if (null !== ($main = $this->config->getMainScriptPath())) {
         $this->putln('?', 'Adding main file: ' . $this->config->getBasePath() . DIRECTORY_SEPARATOR . $main);
         $mapper = $this->config->getMapper();
         $pharPath = $mapper($main);
         if (null !== $pharPath) {
             $this->putln('>', $pharPath);
             $main = $pharPath;
         }
         $this->crate->addFromString($main, $this->config->getMainScriptContents());
     }
     // set the appropriate stub
     if (true === $this->config->isStubGenerated()) {
         $this->putln('?', 'Generating new stub...');
         $stub = StubGenerator::create()->alias($this->config->getAlias())->extract($this->config->isExtractable())->index($main)->intercept($this->config->isInterceptFileFuncs())->mimetypes($this->config->getMimetypeMapping())->mung($this->config->getMungVariables())->notFound($this->config->getNotFoundScriptPath())->web($this->config->isWebPhar());
         if (null !== ($shebang = $this->config->getShebang())) {
             $this->putln('-', 'Using custom shebang line: ' . $shebang);
             $stub->shebang($shebang);
         }
         if (null !== ($lsbInitParams = $this->config->getLsbInitParams())) {
             foreach ($lsbInitParams as $param => $value) {
                 $stub->lsbInitParam($param, $value);
             }
             unset($param, $value);
         }
         if (null !== ($banner = $this->config->getStubBanner())) {
             $this->putln('-', 'Using custom banner.');
             $stub->banner($banner);
         } elseif (null !== ($banner = $this->config->getStubBannerFromFile())) {
             $this->putln('-', 'Using custom banner from file: ' . $this->config->getBasePath() . DIRECTORY_SEPARATOR . $this->config->getStubBannerPath());
             $stub->banner($banner);
         }
         $this->crate->getPhar()->setStub($stub->generate());
     } elseif (null !== ($stub = $this->config->getStubPath())) {
         $stub = $this->config->getBasePath() . DIRECTORY_SEPARATOR . $stub;
         $this->putln('?', "Using stub file: {$stub}");
         $this->crate->setStubUsingFile($stub);
     } else {
         $this->putln('?', 'Using default stub.');
     }
     // set metadata, if any
     if (null !== ($metadata = $this->config->getMetadata())) {
         $this->putln('?', 'Setting metadata...');
         $this->crate->getPhar()->setMetadata($metadata);
     }
     // compress, if algorithm set
     if (null !== ($algorithm = $this->config->getCompressionAlgorithm())) {
         $this->putln('?', 'Compressing...');
         $this->crate->getPhar()->compressFiles($algorithm);
     }
     $this->crate->getPhar()->stopBuffering();
     // sign using private key, if applicable
     if (file_exists($path . '.pubkey')) {
         unlink($path . '.pubkey');
     }
     if (null !== ($key = $this->config->getPrivateKeyPath())) {
         $this->putln('?', 'Signing using a private key...');
         $passphrase = $this->config->getPrivateKeyPassphrase();
         if ($this->config->isPrivateKeyPrompt()) {
             /** @var $dialog DialogHelper */
             $dialog = $this->getHelper('dialog');
             $passphrase = $dialog->askHiddenResponse($output, 'Private key passphrase:');
         }
         $this->crate->signUsingFile($key, $passphrase);
         // set the signature algorithm if no key is used
     } elseif (null !== ($algorithm = $this->config->getSigningAlgorithm())) {
         $this->crate->getPhar()->setSignatureAlgorithm($algorithm);
     }
     unset($this->crate);
     // chmod, if configured
     if (null !== ($chmod = $this->config->getFileMode())) {
         $this->putln('?', 'Setting file permissions...');
         chmod($path, $chmod);
     }
     $this->putln('*', 'Done.');
     if (!file_exists($path)) {
         $output->writeln('<fg=yellow>The archive was not generated because it did not have any contents.</fg=yellow>');
     }
 }
Example #2
0
 public function testGetMungVariablesSet()
 {
     $mung = array('REQUEST_URI');
     $this->setConfig(array('mung' => $mung));
     $this->assertEquals($mung, $this->config->getMungVariables());
 }