ExecCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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) { return "$e (local)"; }, $composer->getPackage()->getBinaries()));
  44. if (!$bins) {
  45. throw new \RuntimeException("No binaries found in composer.json or in bin-dir ($binDir)");
  46. }
  47. $this->getIO()->write(<<<EOT
  48. <comment>Available binaries:</comment>
  49. EOT
  50. );
  51. foreach ($bins as $bin) {
  52. // skip .bat copies
  53. if (isset($previousBin) && $bin === $previousBin.'.bat') {
  54. continue;
  55. }
  56. $previousBin = $bin;
  57. $bin = basename($bin);
  58. $this->getIO()->write(<<<EOT
  59. <info>- $bin</info>
  60. EOT
  61. );
  62. }
  63. return 0;
  64. }
  65. $binary = $input->getArgument('binary');
  66. $dispatcher = $composer->getEventDispatcher();
  67. $dispatcher->addListener('__exec_command', $binary);
  68. if ($output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) {
  69. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  70. }
  71. return $dispatcher->dispatchScript('__exec_command', true, $input->getArgument('args'));
  72. }
  73. }