annotate vendor/symfony/console/Descriptor/ApplicationDescription.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Descriptor;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Application;
Chris@0 15 use Symfony\Component\Console\Command\Command;
Chris@0 16 use Symfony\Component\Console\Exception\CommandNotFoundException;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
Chris@0 20 *
Chris@0 21 * @internal
Chris@0 22 */
Chris@0 23 class ApplicationDescription
Chris@0 24 {
Chris@0 25 const GLOBAL_NAMESPACE = '_global';
Chris@0 26
Chris@0 27 /**
Chris@0 28 * @var Application
Chris@0 29 */
Chris@0 30 private $application;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * @var null|string
Chris@0 34 */
Chris@0 35 private $namespace;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * @var array
Chris@0 39 */
Chris@0 40 private $namespaces;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * @var Command[]
Chris@0 44 */
Chris@0 45 private $commands;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * @var Command[]
Chris@0 49 */
Chris@0 50 private $aliases;
Chris@0 51
Chris@0 52 /**
Chris@0 53 * Constructor.
Chris@0 54 *
Chris@0 55 * @param Application $application
Chris@0 56 * @param string|null $namespace
Chris@0 57 */
Chris@0 58 public function __construct(Application $application, $namespace = null)
Chris@0 59 {
Chris@0 60 $this->application = $application;
Chris@0 61 $this->namespace = $namespace;
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * @return array
Chris@0 66 */
Chris@0 67 public function getNamespaces()
Chris@0 68 {
Chris@0 69 if (null === $this->namespaces) {
Chris@0 70 $this->inspectApplication();
Chris@0 71 }
Chris@0 72
Chris@0 73 return $this->namespaces;
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * @return Command[]
Chris@0 78 */
Chris@0 79 public function getCommands()
Chris@0 80 {
Chris@0 81 if (null === $this->commands) {
Chris@0 82 $this->inspectApplication();
Chris@0 83 }
Chris@0 84
Chris@0 85 return $this->commands;
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * @param string $name
Chris@0 90 *
Chris@0 91 * @return Command
Chris@0 92 *
Chris@0 93 * @throws CommandNotFoundException
Chris@0 94 */
Chris@0 95 public function getCommand($name)
Chris@0 96 {
Chris@0 97 if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
Chris@0 98 throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name));
Chris@0 99 }
Chris@0 100
Chris@0 101 return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
Chris@0 102 }
Chris@0 103
Chris@0 104 private function inspectApplication()
Chris@0 105 {
Chris@0 106 $this->commands = array();
Chris@0 107 $this->namespaces = array();
Chris@0 108
Chris@0 109 $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
Chris@0 110 foreach ($this->sortCommands($all) as $namespace => $commands) {
Chris@0 111 $names = array();
Chris@0 112
Chris@0 113 /** @var Command $command */
Chris@0 114 foreach ($commands as $name => $command) {
Chris@0 115 if (!$command->getName() || $command->isHidden()) {
Chris@0 116 continue;
Chris@0 117 }
Chris@0 118
Chris@0 119 if ($command->getName() === $name) {
Chris@0 120 $this->commands[$name] = $command;
Chris@0 121 } else {
Chris@0 122 $this->aliases[$name] = $command;
Chris@0 123 }
Chris@0 124
Chris@0 125 $names[] = $name;
Chris@0 126 }
Chris@0 127
Chris@0 128 $this->namespaces[$namespace] = array('id' => $namespace, 'commands' => $names);
Chris@0 129 }
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * @param array $commands
Chris@0 134 *
Chris@0 135 * @return array
Chris@0 136 */
Chris@0 137 private function sortCommands(array $commands)
Chris@0 138 {
Chris@0 139 $namespacedCommands = array();
Chris@0 140 $globalCommands = array();
Chris@0 141 foreach ($commands as $name => $command) {
Chris@0 142 $key = $this->application->extractNamespace($name, 1);
Chris@0 143 if (!$key) {
Chris@0 144 $globalCommands['_global'][$name] = $command;
Chris@0 145 } else {
Chris@0 146 $namespacedCommands[$key][$name] = $command;
Chris@0 147 }
Chris@0 148 }
Chris@0 149 ksort($namespacedCommands);
Chris@0 150 $namespacedCommands = array_merge($globalCommands, $namespacedCommands);
Chris@0 151
Chris@0 152 foreach ($namespacedCommands as &$commandsSet) {
Chris@0 153 ksort($commandsSet);
Chris@0 154 }
Chris@0 155 // unset reference to keep scope clear
Chris@0 156 unset($commandsSet);
Chris@0 157
Chris@0 158 return $namespacedCommands;
Chris@0 159 }
Chris@0 160 }