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