ApplicationDescription.php 3.6 KB

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