/** * Adds files using an iterator. * * @param Traversable $iterator The iterator. * @param string $message The message to announce. * @param boolean $binary Should the adding be binary-safe? * * @throws RuntimeException If a file is not readable. */ private function add(Traversable $iterator = null, $message = null, $binary = false) { static $count = 0; if ($iterator) { if ($message) { $this->putln('?', $message); } $box = $binary ? $this->box->getPhar() : $this->box; $baseRegex = $this->config->getBasePathRegex(); $mapper = $this->config->getMapper(); /** @var $file SplFileInfo */ foreach ($iterator as $file) { if (0 === ++$count % 100) { gc_collect_cycles(); } $relative = preg_replace($baseRegex, '', $file->getPathname()); if (null !== ($mapped = $mapper($relative))) { $relative = $mapped; } if ($this->isVerbose()) { if (false === $file->isReadable()) { throw new RuntimeException(sprintf('The file "%s" is not readable.', $file->getPathname())); } $this->putln('+', $file); if (null !== $mapped) { $this->putln('>', $relative); } } $box->addFile($file, $relative); } } }
protected function createPhar($name, $echo) { unlink($file = $this->createFile($name)); $box = Box::create($file); $box->addFromString('index.php', '<?php echo ' . var_export($echo, true) . ';'); unset($box); return $file; }
public function testSignUsingFileReadError() { $root = vfsStream::newDirectory('test'); $root->addChild(vfsStream::newFile('private.key', 00)); vfsStreamWrapper::setRoot($root); $this->setExpectedException('Herrera\\Box\\Exception\\FileException', 'failed to open stream'); $this->box->signUsingFile('vfs://test/private.key'); }
/** * Add a folder to the PHAR * * @param string $folder * @param array $ignore * * @return array */ protected function addFolder($folder, array $ignore = array()) { $finder = new Finder(); $finder = $finder->files()->ignoreVCS(true)->name('*.php')->in($folder); // Ignore some files or folders if ($ignore) { foreach ($ignore as $file) { $finder->exclude($file); } } $this->box->buildFromIterator($finder, dirname($folder)); return iterator_to_array($finder); }
/** * create new instance in target path * * @param string $target * @param PharComposer $pharComposer * @return TargetPhar */ public static function create($target, PharComposer $pharComposer) { return new self(Box::create($target), $pharComposer); }
private function preparePhar() { touch('bootstrap.php'); file_put_contents('box.json', json_encode(array('bootstrap' => 'bootstrap.php', 'compactors' => 'Herrera\\Box\\Compactor\\Php', 'main' => 'bin/run', 'replacements' => array('name' => 'world'), 'stub' => true))); $box = Box::create('test.phar'); $box->addCompactor(new Php()); $box->setValues(array('name' => 'world')); $box->addFromString('bin/run', <<<CODE #!/usr/bin/run php <?php require __DIR__ . '/../src/hello.php'; CODE ); $box->addFromString('src/hello.php', <<<CODE <?php /** * Just saying hello! */ echo "Hello, @name@! "; CODE ); $box->getPhar()->setStub(StubGenerator::create()->index('bin/run')->generate()); unset($box); }
/** * @override */ protected function execute(InputInterface $input, OutputInterface $output) { if (false === $input->getOption('stub') && null === $input->getArgument('local')) { $output->writeln('<error>The <info>local</info> argument is required.</error>'); return 1; } $this->config = $this->getConfig($input); $phar = $input->getArgument('phar'); $file = $input->getArgument('file'); // load bootstrap file if (null !== ($bootstrap = $this->config->getBootstrapFile())) { $this->config->loadBootstrap(); $this->putln('?', "Loading bootstrap file: {$bootstrap}"); unset($bootstrap); } $this->putln('*', 'Adding to the Phar...'); if (false === is_file($phar)) { $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $phar)); return 1; } if (false === is_file($file)) { $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $file)); return 1; } if (false == preg_match('/^\\w+:\\/\\//', $file)) { $file = realpath($file); } $this->box = Box::create($phar); // 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->box->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->box->addCompactor($compactor); } } // add the file $phar = $this->box->getPhar(); $local = str_replace('\\', '/', Path::canonical($input->getArgument('local'))); if (isset($phar[$local]) && false === $input->getOption('replace')) { $output->writeln(sprintf('<error>The file "%s" already exists in the Phar.</error>', $local)); return 1; } if ($input->getOption('binary')) { $this->putln('?', "Adding binary file: {$file}"); $phar->addFile($file, $local); } elseif ($input->getOption('stub')) { $this->putln('?', "Using stub file: {$file}"); $this->box->setStubUsingFile($file); } elseif ($input->getOption('main')) { $this->putln('?', "Adding main file: {$file}"); if (false === ($contents = @file_get_contents($file))) { $error = error_get_last(); throw new RuntimeException($error['message']); } $this->box->addFromString($local, preg_replace('/^#!.*\\s*/', '', $contents)); } else { $this->putln('?', "Adding file: {$file}"); $this->box->addFile($file, $local); } unset($this->box, $phar); $this->putln('*', 'Done.'); return 0; }