菜谱项目

ApplicationDescription.php 3.8KB

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