RunScriptCommand.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Composer\Script\ScriptEvents;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * @author Fabien Potencier <fabien.potencier@gmail.com>
  19. */
  20. class RunScriptCommand extends Command
  21. {
  22. protected function configure()
  23. {
  24. $this
  25. ->setName('run-script')
  26. ->setDescription('Run the scripts defined in composer.json.')
  27. ->setDefinition(array(
  28. new InputArgument('script', InputArgument::REQUIRED, 'Script name to run.'),
  29. new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
  30. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
  31. ))
  32. ->setHelp(<<<EOT
  33. The <info>run-script</info> command runs scripts defined in composer.json:
  34. <info>php composer.phar run-script post-update-cmd</info>
  35. EOT
  36. )
  37. ;
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output)
  40. {
  41. $script = $input->getArgument('script');
  42. if (!in_array($script, array(
  43. ScriptEvents::PRE_INSTALL_CMD,
  44. ScriptEvents::POST_INSTALL_CMD,
  45. ScriptEvents::PRE_UPDATE_CMD,
  46. ScriptEvents::POST_UPDATE_CMD,
  47. ScriptEvents::PRE_STATUS_CMD,
  48. ScriptEvents::POST_STATUS_CMD,
  49. ))) {
  50. if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  51. throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script));
  52. }
  53. throw new \InvalidArgumentException(sprintf('Script "%s" does not exist', $script));
  54. }
  55. $this->getComposer()->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev'));
  56. }
  57. }