ApplicationDescription.php 4.0 KB

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