RunScriptCommand.php 2.9 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 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. /**
  23. * @var array Array with command events
  24. */
  25. protected $commandEvents = array(
  26. ScriptEvents::PRE_INSTALL_CMD,
  27. ScriptEvents::POST_INSTALL_CMD,
  28. ScriptEvents::PRE_UPDATE_CMD,
  29. ScriptEvents::POST_UPDATE_CMD,
  30. ScriptEvents::PRE_STATUS_CMD,
  31. ScriptEvents::POST_STATUS_CMD,
  32. ScriptEvents::POST_ROOT_PACKAGE_INSTALL,
  33. ScriptEvents::POST_CREATE_PROJECT_CMD
  34. );
  35. /**
  36. * @var array Array with script events
  37. */
  38. protected $scriptEvents = array(
  39. ScriptEvents::PRE_ARCHIVE_CMD,
  40. ScriptEvents::POST_ARCHIVE_CMD,
  41. ScriptEvents::PRE_AUTOLOAD_DUMP,
  42. ScriptEvents::POST_AUTOLOAD_DUMP
  43. );
  44. protected function configure()
  45. {
  46. $this
  47. ->setName('run-script')
  48. ->setDescription('Run the scripts defined in composer.json.')
  49. ->setDefinition(array(
  50. new InputArgument('script', InputArgument::REQUIRED, 'Script name to run.'),
  51. new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
  52. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
  53. ))
  54. ->setHelp(<<<EOT
  55. The <info>run-script</info> command runs scripts defined in composer.json:
  56. <info>php composer.phar run-script post-update-cmd</info>
  57. EOT
  58. )
  59. ;
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output)
  62. {
  63. $script = $input->getArgument('script');
  64. if (!in_array($script, $this->commandEvents) || !in_array($script, $this->scriptEvents)) {
  65. if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  66. throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script));
  67. }
  68. throw new \InvalidArgumentException(sprintf('Script "%s" does not exist', $script));
  69. }
  70. if (in_array($script, $this->commandEvents)) {
  71. $this->getComposer()->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev'));
  72. } else {
  73. $this->getComposer()->getEventDispatcher()->dispatchScript($script);
  74. }
  75. }
  76. }