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