Drupal investigation

ApplicationDescription.php 3.8KB

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