ApplicationDescription.php 4.0 KB

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