Esempio n. 1
0
 /**
  * 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);
         }
     }
 }
 public function testProcessFindersInvalidMethod()
 {
     $this->setConfig(array('finder' => array(array('invalidMethod' => 'whargarbl'))));
     $this->setExpectedException('InvalidArgumentException', 'The method "Finder::invalidMethod" does not exist.');
     $this->config->getFinders();
 }
Esempio n. 3
0
 /**
  * @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;
 }