ApplicationDescription.php 3.5 KB

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