/** * 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); } $crate = $binary ? $this->crate->getPhar() : $this->crate; $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); } } $crate->addFile($file, $relative); } } }
public function testSignUsingFileReadError() { $root = vfsStream::newDirectory('test'); $root->addChild(vfsStream::newFile('private.key', 00)); vfsStreamWrapper::setRoot($root); $this->setExpectedException('CmPayments\\Crate\\Exception\\FileException', 'failed to open stream'); $this->crate->signUsingFile('vfs://test/private.key'); }
private function preparePhar() { touch('bootstrap.php'); file_put_contents('crate.json', json_encode(array('bootstrap' => 'bootstrap.php', 'compactors' => 'CmPayments\\Crate\\Compactor\\Php', 'main' => 'bin/run', 'replacements' => array('name' => 'world'), 'stub' => true))); $crate = Crate::create('test.phar'); $crate->addCompactor(new Php()); $crate->setValues(array('name' => 'world')); $crate->addFromString('bin/run', <<<CODE #!/usr/bin/run php <?php require __DIR__ . '/../src/hello.php'; CODE ); $crate->addFromString('src/hello.php', <<<CODE <?php /** * Just saying hello! */ echo "Hello, @name@! "; CODE ); $crate->getPhar()->setStub(StubGenerator::create()->index('bin/run')->generate()); unset($crate); }
/** * @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->crate = Crate::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->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); } } // add the file $phar = $this->crate->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->crate->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->crate->addFromString($local, preg_replace('/^#!.*\\s*/', '', $contents)); } else { $this->putln('?', "Adding file: {$file}"); $this->crate->addFile($file, $local); } unset($this->crate, $phar); $this->putln('*', 'Done.'); return 0; }