diff options
Diffstat (limited to 'vendor/symfony/console/Descriptor/ApplicationDescription.php')
-rw-r--r-- | vendor/symfony/console/Descriptor/ApplicationDescription.php | 81 |
1 files changed, 32 insertions, 49 deletions
diff --git a/vendor/symfony/console/Descriptor/ApplicationDescription.php b/vendor/symfony/console/Descriptor/ApplicationDescription.php index 89961b9..3970b90 100644 --- a/vendor/symfony/console/Descriptor/ApplicationDescription.php +++ b/vendor/symfony/console/Descriptor/ApplicationDescription.php @@ -22,17 +22,11 @@ use Symfony\Component\Console\Exception\CommandNotFoundException; */ class ApplicationDescription { - const GLOBAL_NAMESPACE = '_global'; + public const GLOBAL_NAMESPACE = '_global'; - /** - * @var Application - */ private $application; - - /** - * @var null|string - */ private $namespace; + private $showHidden; /** * @var array @@ -49,22 +43,14 @@ class ApplicationDescription */ private $aliases; - /** - * Constructor. - * - * @param Application $application - * @param string|null $namespace - */ - public function __construct(Application $application, $namespace = null) + public function __construct(Application $application, string $namespace = null, bool $showHidden = false) { $this->application = $application; $this->namespace = $namespace; + $this->showHidden = $showHidden; } - /** - * @return array - */ - public function getNamespaces() + public function getNamespaces(): array { if (null === $this->namespaces) { $this->inspectApplication(); @@ -76,7 +62,7 @@ class ApplicationDescription /** * @return Command[] */ - public function getCommands() + public function getCommands(): array { if (null === $this->commands) { $this->inspectApplication(); @@ -86,33 +72,29 @@ class ApplicationDescription } /** - * @param string $name - * - * @return Command - * * @throws CommandNotFoundException */ - public function getCommand($name) + public function getCommand(string $name): Command { if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) { - throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name)); + throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); } - return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name]; + return $this->commands[$name] ?? $this->aliases[$name]; } private function inspectApplication() { - $this->commands = array(); - $this->namespaces = array(); + $this->commands = []; + $this->namespaces = []; $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null); foreach ($this->sortCommands($all) as $namespace => $commands) { - $names = array(); + $names = []; /** @var Command $command */ foreach ($commands as $name => $command) { - if (!$command->getName()) { + if (!$command->getName() || (!$this->showHidden && $command->isHidden())) { continue; } @@ -125,36 +107,37 @@ class ApplicationDescription $names[] = $name; } - $this->namespaces[$namespace] = array('id' => $namespace, 'commands' => $names); + $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names]; } } - /** - * @param array $commands - * - * @return array - */ - private function sortCommands(array $commands) + private function sortCommands(array $commands): array { - $namespacedCommands = array(); - $globalCommands = array(); + $namespacedCommands = []; + $globalCommands = []; + $sortedCommands = []; foreach ($commands as $name => $command) { $key = $this->application->extractNamespace($name, 1); - if (!$key) { - $globalCommands['_global'][$name] = $command; + if (\in_array($key, ['', self::GLOBAL_NAMESPACE], true)) { + $globalCommands[$name] = $command; } else { $namespacedCommands[$key][$name] = $command; } } - ksort($namespacedCommands); - $namespacedCommands = array_merge($globalCommands, $namespacedCommands); - foreach ($namespacedCommands as &$commandsSet) { - ksort($commandsSet); + if ($globalCommands) { + ksort($globalCommands); + $sortedCommands[self::GLOBAL_NAMESPACE] = $globalCommands; + } + + if ($namespacedCommands) { + ksort($namespacedCommands); + foreach ($namespacedCommands as $key => $commandsSet) { + ksort($commandsSet); + $sortedCommands[$key] = $commandsSet; + } } - // unset reference to keep scope clear - unset($commandsSet); - return $namespacedCommands; + return $sortedCommands; } } |