Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Descriptor; Chris@0: Chris@0: use Symfony\Component\Console\Application; Chris@0: use Symfony\Component\Console\Command\Command; Chris@0: use Symfony\Component\Console\Exception\CommandNotFoundException; Chris@0: Chris@0: /** Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class ApplicationDescription Chris@0: { Chris@0: const GLOBAL_NAMESPACE = '_global'; Chris@0: Chris@0: private $application; Chris@0: private $namespace; Chris@14: private $showHidden; Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: private $namespaces; Chris@0: Chris@0: /** Chris@0: * @var Command[] Chris@0: */ Chris@0: private $commands; Chris@0: Chris@0: /** Chris@0: * @var Command[] Chris@0: */ Chris@0: private $aliases; Chris@0: Chris@0: /** Chris@0: * @param Application $application Chris@0: * @param string|null $namespace Chris@14: * @param bool $showHidden Chris@0: */ Chris@14: public function __construct(Application $application, $namespace = null, $showHidden = false) Chris@0: { Chris@0: $this->application = $application; Chris@0: $this->namespace = $namespace; Chris@14: $this->showHidden = $showHidden; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return array Chris@0: */ Chris@0: public function getNamespaces() Chris@0: { Chris@0: if (null === $this->namespaces) { Chris@0: $this->inspectApplication(); Chris@0: } Chris@0: Chris@0: return $this->namespaces; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return Command[] Chris@0: */ Chris@0: public function getCommands() Chris@0: { Chris@0: if (null === $this->commands) { Chris@0: $this->inspectApplication(); Chris@0: } Chris@0: Chris@0: return $this->commands; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $name Chris@0: * Chris@0: * @return Command Chris@0: * Chris@0: * @throws CommandNotFoundException Chris@0: */ Chris@0: public function getCommand($name) Chris@0: { Chris@0: if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) { Chris@0: throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name)); Chris@0: } Chris@0: Chris@0: return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name]; Chris@0: } Chris@0: Chris@0: private function inspectApplication() Chris@0: { Chris@17: $this->commands = []; Chris@17: $this->namespaces = []; Chris@0: Chris@0: $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null); Chris@0: foreach ($this->sortCommands($all) as $namespace => $commands) { Chris@17: $names = []; Chris@0: Chris@0: /** @var Command $command */ Chris@0: foreach ($commands as $name => $command) { Chris@14: if (!$command->getName() || (!$this->showHidden && $command->isHidden())) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if ($command->getName() === $name) { Chris@0: $this->commands[$name] = $command; Chris@0: } else { Chris@0: $this->aliases[$name] = $command; Chris@0: } Chris@0: Chris@0: $names[] = $name; Chris@0: } Chris@0: Chris@17: $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return array Chris@0: */ Chris@0: private function sortCommands(array $commands) Chris@0: { Chris@17: $namespacedCommands = []; Chris@17: $globalCommands = []; Chris@0: foreach ($commands as $name => $command) { Chris@0: $key = $this->application->extractNamespace($name, 1); Chris@0: if (!$key) { Chris@0: $globalCommands['_global'][$name] = $command; Chris@0: } else { Chris@0: $namespacedCommands[$key][$name] = $command; Chris@0: } Chris@0: } Chris@0: ksort($namespacedCommands); Chris@0: $namespacedCommands = array_merge($globalCommands, $namespacedCommands); Chris@0: Chris@0: foreach ($namespacedCommands as &$commandsSet) { Chris@0: ksort($commandsSet); Chris@0: } Chris@0: // unset reference to keep scope clear Chris@0: unset($commandsSet); Chris@0: Chris@0: return $namespacedCommands; Chris@0: } Chris@0: }