Debian Codegraph @

vendor/symfony/console/Descriptor/ApplicationDescription.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Console\Descriptor;
13
14 use Symfony\Component\Console\Application;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Exception\CommandNotFoundException;
17
18 /**
19 * @author Jean-François Simon <[email protected]>
20 *
21 * @internal
22 */
23 class ApplicationDescription
24 {
25 public const GLOBAL_NAMESPACE = '_global';
26
27 private array $namespaces;
28
29 /**
30 * @var array<string, Command>
31 */
32 private array $commands;
33
34 /**
35 * @var array<string, Command>
36 */
37 private array $aliases = [];
38
39 public function __construct(
40 private Application $application,
41 private ?string $namespace = null,
42 private bool $showHidden = false,
43 ) {
44 }
45
46 public function getNamespaces(): array
47 {
48 if (!isset($this->namespaces)) {
49 $this->inspectApplication();
50 }
51
52 return $this->namespaces;
53 }
54
55 /**
56 * @return Command[]
57 */
58 public function getCommands(): array
59 {
60 if (!isset($this->commands)) {
61 $this->inspectApplication();
62 }
63
64 return $this->commands;
65 }
66
67 /**
68 * @throws CommandNotFoundException
69 */
70 public function getCommand(string $name): Command
71 {
72 if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
73 throw new CommandNotFoundException(\sprintf('Command "%s" does not exist.', $name));
74 }
75
76 return $this->commands[$name] ?? $this->aliases[$name];
77 }
78
79 private function inspectApplication(): void
80 {
81 $this->commands = [];
82 $this->namespaces = [];
83
84 $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
85 foreach ($this->sortCommands($all) as $namespace => $commands) {
86 $names = [];
87
88 foreach ($commands as $name => $command) {
89 if (!$command->getName() || (!$this->showHidden && $command->isHidden())) {
90 continue;
91 }
92
93 if ($command->getName() === $name) {
94 $this->commands[$name] = $command;
95 } else {
96 $this->aliases[$name] = $command;
97 }
98
99 $names[] = $name;
100 }
101
102 $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names];
103 }
104 }
105
106 /**
107 * @return array<string, array<string, Command>>
108 */
109 private function sortCommands(array $commands): array
110 {
111 $namespacedCommands = [];
112 $globalCommands = [];
113 $sortedCommands = [];
114 foreach ($commands as $name => $command) {
115 $key = $this->application->extractNamespace($name, 1);
116 if (\in_array($key, ['', self::GLOBAL_NAMESPACE], true)) {
117 $globalCommands[$name] = $command;
118 } else {
119 $namespacedCommands[$key][$name] = $command;
120 }
121 }
122
123 if ($globalCommands) {
124 ksort($globalCommands);
125 $sortedCommands[self::GLOBAL_NAMESPACE] = $globalCommands;
126 }
127
128 if ($namespacedCommands) {
129 ksort($namespacedCommands, \SORT_STRING);
130 foreach ($namespacedCommands as $key => $commandsSet) {
131 ksort($commandsSet);
132 $sortedCommands[$key] = $commandsSet;
133 }
134 }
135
136 return $sortedCommands;
137 }
138 }