GlobalCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Composer\Util\Filesystem;
  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 BaseCommand
  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(
  33. <<<EOT
  34. Use this command as a wrapper to run other Composer commands
  35. within the global context of COMPOSER_HOME.
  36. You can use this to install CLI utilities globally, all you need
  37. is to add the COMPOSER_HOME/vendor/bin dir to your PATH env var.
  38. COMPOSER_HOME is c:\Users\<user>\AppData\Roaming\Composer on Windows
  39. and /home/<user>/.composer on unix systems.
  40. If your system uses freedesktop.org standards, then it will first check
  41. XDG_CONFIG_HOME or default to /home/<user>/.config/composer
  42. Note: This path may vary depending on customizations to bin-dir in
  43. composer.json or the environmental variable COMPOSER_BIN_DIR.
  44. Read more at https://getcomposer.org/doc/03-cli.md#global
  45. EOT
  46. )
  47. ;
  48. }
  49. public function run(InputInterface $input, OutputInterface $output)
  50. {
  51. // extract real command name
  52. $tokens = preg_split('{\s+}', $input->__toString());
  53. $args = array();
  54. foreach ($tokens as $token) {
  55. if ($token && $token[0] !== '-') {
  56. $args[] = $token;
  57. if (count($args) >= 2) {
  58. break;
  59. }
  60. }
  61. }
  62. // show help for this command if no command was found
  63. if (count($args) < 2) {
  64. return parent::run($input, $output);
  65. }
  66. // change to global dir
  67. $config = Factory::createConfig();
  68. $home = $config->get('home');
  69. if (!is_dir($home)) {
  70. $fs = new Filesystem();
  71. $fs->ensureDirectoryExists($home);
  72. if (!is_dir($home)) {
  73. throw new \RuntimeException('Could not create home directory');
  74. }
  75. }
  76. try {
  77. chdir($home);
  78. } catch (\Exception $e) {
  79. throw new \RuntimeException('Could not switch to home directory "'.$home.'"', 0, $e);
  80. }
  81. $this->getIO()->writeError('<info>Changed current directory to '.$home.'</info>');
  82. // create new input without "global" command prefix
  83. $input = new StringInput(preg_replace('{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '', $input->__toString(), 1));
  84. $this->getApplication()->resetComposer();
  85. return $this->getApplication()->run($input, $output);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function isProxyCommand()
  91. {
  92. return true;
  93. }
  94. }