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