Example #1
0
 /**
  * Execute command
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return null|int
  *
  * @throws \InvalidArgumentException
  * @throws InvalidArgumentException
  * @throws RuntimeException
  * @throws \BadMethodCallException
  * @throws \UnexpectedValueException
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if ((bool) ini_get('phar.readonly')) {
         throw new RuntimeException('For use compiler you must set phar.readonly = Off in php.ini');
     }
     $output->writeln('<fg=green>Check configuration</>');
     $configFile = $input->getOption('config');
     if ($configFile && file_exists($configFile)) {
         $configuration = Configuration::fromXML($configFile);
     } else {
         $configuration = Configuration::fromInput($input);
     }
     $configuration->display($output, OutputInterface::VERBOSITY_DEBUG);
     if (!($pharFile = $configuration->getOutput())) {
         throw new InvalidArgumentException('Phar --output file not defined');
     }
     $qHelper = $this->getHelper('question');
     $question = new ConfirmationQuestion("<fg=yellow;options=bold>File {$pharFile} already exists, overwrite?</> [y/n]", false);
     if (!$qHelper->ask($input, $output, $question)) {
         return;
     }
     if (file_exists($pharFile) && !@unlink($pharFile)) {
         throw new RuntimeException('Cannot overwrite file ' . $pharFile . ', permission denied');
     }
     $finder = new Finder();
     $finder->files()->ignoreVCS(true)->name('*.php');
     $configuration->apply($finder);
     if (!count($finder)) {
         throw new RuntimeException('Nothing found to add to phar');
     }
     /* @var $file \SplFileInfo */
     $output->writeln('<fg=green>Add files</>');
     foreach ($finder as $file) {
         $output->writeln(' ' . $file->getPathname(), OutputInterface::VERBOSITY_DEBUG);
     }
     $output->writeln('<fg=green>Compile phar</>');
     $phar = new \Phar($pharFile, 0, $pharFile);
     $phar->setSignatureAlgorithm(\Phar::SHA1);
     $phar->startBuffering();
     /* @var $file \SplFileInfo */
     foreach ($finder as $file) {
         $this->addFile($phar, $file, getcwd());
     }
     if (!($binFile = $configuration->getBinary())) {
         if (file_exists('vendor/autoload.php')) {
             $binFile = 'vendor/autoload.php';
         } else {
             throw new RuntimeException('--binary option is required for non composer projects');
         }
     }
     $output->writeln('<fg=green>Generate stub</>');
     $stub = $this->getStub($pharFile, $binFile);
     $output->writeln($this->indent($stub, 1), OutputInterface::VERBOSITY_DEBUG);
     $phar->setStub($stub);
     $phar->stopBuffering();
     unset($phar);
     $output->writeln('<fg=green>Phar executable successfully created</>');
 }