TYPO3  7.6
ApplicationDescription.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Descriptor;
13 
16 
23 {
24  const GLOBAL_NAMESPACE = '_global';
25 
29  private $application;
30 
34  private $namespace;
35 
39  private $namespaces;
40 
44  private $commands;
45 
49  private $aliases;
50 
58  {
59  $this->application = $application;
60  $this->namespace = $namespace;
61  }
62 
66  public function getNamespaces()
67  {
68  if (null === $this->namespaces) {
69  $this->inspectApplication();
70  }
71 
72  return $this->namespaces;
73  }
74 
78  public function getCommands()
79  {
80  if (null === $this->commands) {
81  $this->inspectApplication();
82  }
83 
84  return $this->commands;
85  }
86 
94  public function getCommand($name)
95  {
96  if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
97  throw new \InvalidArgumentException(sprintf('Command %s does not exist.', $name));
98  }
99 
100  return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
101  }
102 
103  private function inspectApplication()
104  {
105  $this->commands = array();
106  $this->namespaces = array();
107 
108  $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
109  foreach ($this->sortCommands($all) as $namespace => $commands) {
110  $names = array();
111 
113  foreach ($commands as $name => $command) {
114  if (!$command->getName()) {
115  continue;
116  }
117 
118  if ($command->getName() === $name) {
119  $this->commands[$name] = $command;
120  } else {
121  $this->aliases[$name] = $command;
122  }
123 
124  $names[] = $name;
125  }
126 
127  $this->namespaces[$namespace] = array('id' => $namespace, 'commands' => $names);
128  }
129  }
130 
136  private function sortCommands(array $commands)
137  {
138  $namespacedCommands = array();
139  foreach ($commands as $name => $command) {
140  $key = $this->application->extractNamespace($name, 1);
141  if (!$key) {
142  $key = '_global';
143  }
144 
145  $namespacedCommands[$key][$name] = $command;
146  }
147  ksort($namespacedCommands);
148 
149  foreach ($namespacedCommands as &$commandsSet) {
150  ksort($commandsSet);
151  }
152  // unset reference to keep scope clear
153  unset($commandsSet);
154 
155  return $namespacedCommands;
156  }
157 }