ApplicationDescription.php 3.4 KB

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