From 7311bc77e68b229dbb19f7bc12f2b8a3b17ab936 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 13 Aug 2013 14:15:54 +0200 Subject: [PATCH 1/2] Add global command to be able to require/install/update global packages, fixes #1813, fixes #55 --- src/Composer/Command/GlobalCommand.php | 79 ++++++++++++++++++++++++++ src/Composer/Console/Application.php | 1 + 2 files changed, 80 insertions(+) create mode 100644 src/Composer/Command/GlobalCommand.php diff --git a/src/Composer/Command/GlobalCommand.php b/src/Composer/Command/GlobalCommand.php new file mode 100644 index 000000000..d37958939 --- /dev/null +++ b/src/Composer/Command/GlobalCommand.php @@ -0,0 +1,79 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Command; + +use Composer\Installer; +use Composer\Factory; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\StringInput; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @author Jordi Boggiano + */ +class GlobalCommand extends Command +{ + protected function configure() + { + $this + ->setName('global') + ->setDescription('Allows running commands in the global composer dir ($COMPOSER_HOME).') + ->setDefinition(array( + new InputArgument('command-name', InputArgument::REQUIRED, ''), + new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), + )) + ->setHelp(<<\AppData\Roaming\Composer on Windows +and /home//.composer on unix systems. + +EOT + ) + ; + } + + public function run(InputInterface $input, OutputInterface $output) + { + // extract real command name + $tokens = preg_split('{\s+}', $input->__toString()); + $args = array(); + foreach ($tokens as $token) { + if ($token && $token[0] !== '-') { + $args[] = $token; + if (count($args) >= 2) { + break; + } + } + } + + // show help for this command if no command was found + if (count($args) < 2) { + return parent::run($input, $output); + } + + // change to global dir + $config = Factory::createConfig(); + chdir($config->get('home')); + + // create new input without "global" command prefix + $input = new StringInput(preg_replace('{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '', $input->__toString(), 1)); + + return $this->getApplication()->get($args[1])->run($input, $output); + } +} diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index f4021c958..0d1e45500 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -225,6 +225,7 @@ class Application extends BaseApplication $commands[] = new Command\DiagnoseCommand(); $commands[] = new Command\RunScriptCommand(); $commands[] = new Command\LicensesCommand(); + $commands[] = new Command\GlobalCommand(); if ('phar:' === substr(__FILE__, 0, 5)) { $commands[] = new Command\SelfUpdateCommand(); From ad2b8c48974e0a0b77795e2ea31c4a08e82babd3 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 17 Aug 2013 03:45:51 +0200 Subject: [PATCH 2/2] Add docs for global command --- doc/03-cli.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/03-cli.md b/doc/03-cli.md index 995d2029e..be0b252e8 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -148,6 +148,24 @@ to the command. * **--no-progress:** Removes the progress display that can mess with some terminals or scripts which don't handle backspace characters. +## global + +The global command allows you to run other commands like `install`, `require` +or `update` as if you were running them from the [COMPOSER_HOME](#COMPOSER_HOME) +directory. + +This can be used to install CLI utilities globally and if you add +`$COMPOSER_HOME/vendor/bin` to your `$PATH` environment variable. Here is an +example: + + $ php composer.phar global require fabpot/php-cs-fixer:dev-master + +Now the `php-cs-fixer` binary is available globally (assuming you adjusted +your PATH). If you wish to update the binary later on you can just run a +global update: + + $ php composer.phar global update + ## search The search command allows you to search through the current project's package @@ -356,6 +374,11 @@ performance. autoloader. This is recommended especially for production, but can take a bit of time to run so it is currently not done by default. +## licenses + +Lists the name, version and license of every package installed. Use +`--format=json` to get machine readable output. + ## run-script To run [scripts](articles/scripts.md) manually you can use this command,