ApplicationDescription.php 3.9 KB

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