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