ApplicationDescription.php 3.7 KB

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