ApplicationDescription.php 3.7 KB

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