ExecCommand.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. /**
  17. * @author Davey Shafik <me@daveyshafik.com>
  18. */
  19. class ExecCommand extends BaseCommand
  20. {
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('exec')
  25. ->setDescription('Execute a vendored binary/script.')
  26. ->setDefinition(array(
  27. new InputOption('list', 'l', InputOption::VALUE_NONE),
  28. new InputArgument('binary', InputArgument::OPTIONAL, 'The binary to run, e.g. phpunit'),
  29. new InputArgument(
  30. 'args',
  31. InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
  32. 'Arguments to pass to the binary. Use <info>--</info> to separate from composer arguments'
  33. ),
  34. ))
  35. ;
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $composer = $this->getComposer();
  40. $binDir = $composer->getConfig()->get('bin-dir');
  41. if ($input->getOption('list') || !$input->getArgument('binary')) {
  42. $bins = glob($binDir . '/*');
  43. $bins = array_merge($bins, array_map(function ($e) {
  44. return "$e (local)";
  45. }, $composer->getPackage()->getBinaries()));
  46. if (!$bins) {
  47. throw new \RuntimeException("No binaries found in composer.json or in bin-dir ($binDir)");
  48. }
  49. $this->getIO()->write(<<<EOT
  50. <comment>Available binaries:</comment>
  51. EOT
  52. );
  53. foreach ($bins as $bin) {
  54. // skip .bat copies
  55. if (isset($previousBin) && $bin === $previousBin.'.bat') {
  56. continue;
  57. }
  58. $previousBin = $bin;
  59. $bin = basename($bin);
  60. $this->getIO()->write(<<<EOT
  61. <info>- $bin</info>
  62. EOT
  63. );
  64. }
  65. return 0;
  66. }
  67. $binary = $input->getArgument('binary');
  68. $dispatcher = $composer->getEventDispatcher();
  69. $dispatcher->addListener('__exec_command', $binary);
  70. if ($output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) {
  71. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  72. }
  73. return $dispatcher->dispatchScript('__exec_command', true, $input->getArgument('args'));
  74. }
  75. }