GlobalCommand.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Factory;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\StringInput;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class GlobalCommand extends Command
  21. {
  22. protected function configure()
  23. {
  24. $this
  25. ->setName('global')
  26. ->setDescription('Allows running commands in the global composer dir ($COMPOSER_HOME).')
  27. ->setDefinition(array(
  28. new InputArgument('command-name', InputArgument::REQUIRED, ''),
  29. new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
  30. ))
  31. ->setHelp(<<<EOT
  32. Use this command as a wrapper to run other Composer commands
  33. within the global context of COMPOSER_HOME.
  34. You can use this to install CLI utilities globally, all you need
  35. is to add the COMPOSER_HOME/vendor/bin dir to your PATH env var.
  36. COMPOSER_HOME is c:\Users\<user>\AppData\Roaming\Composer on Windows
  37. and /home/<user>/.composer on unix systems.
  38. Note: This path may vary depending on customizations to bin-dir in
  39. composer.json or the environmental variable COMPOSER_BIN_DIR.
  40. EOT
  41. )
  42. ;
  43. }
  44. public function run(InputInterface $input, OutputInterface $output)
  45. {
  46. // extract real command name
  47. $tokens = preg_split('{\s+}', $input->__toString());
  48. $args = array();
  49. foreach ($tokens as $token) {
  50. if ($token && $token[0] !== '-') {
  51. $args[] = $token;
  52. if (count($args) >= 2) {
  53. break;
  54. }
  55. }
  56. }
  57. // show help for this command if no command was found
  58. if (count($args) < 2) {
  59. return parent::run($input, $output);
  60. }
  61. // change to global dir
  62. $config = Factory::createConfig();
  63. chdir($config->get('home'));
  64. $this->getIO()->writeError('<info>Changed current directory to '.$config->get('home').'</info>');
  65. // create new input without "global" command prefix
  66. $input = new StringInput(preg_replace('{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '', $input->__toString(), 1));
  67. return $this->getApplication()->run($input, $output);
  68. }
  69. }