ApplicationDescription.php 3.7 KB

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