ApplicationDescription.php 3.6 KB

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