Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Descriptor;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Console\Application;
|
Chris@0
|
15 use Symfony\Component\Console\Command\Command;
|
Chris@0
|
16 use Symfony\Component\Console\Exception\CommandNotFoundException;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
|
Chris@0
|
20 *
|
Chris@0
|
21 * @internal
|
Chris@0
|
22 */
|
Chris@0
|
23 class ApplicationDescription
|
Chris@0
|
24 {
|
Chris@0
|
25 const GLOBAL_NAMESPACE = '_global';
|
Chris@0
|
26
|
Chris@0
|
27 private $application;
|
Chris@0
|
28 private $namespace;
|
Chris@14
|
29 private $showHidden;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * @var array
|
Chris@0
|
33 */
|
Chris@0
|
34 private $namespaces;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * @var Command[]
|
Chris@0
|
38 */
|
Chris@0
|
39 private $commands;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * @var Command[]
|
Chris@0
|
43 */
|
Chris@0
|
44 private $aliases;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * @param Application $application
|
Chris@0
|
48 * @param string|null $namespace
|
Chris@14
|
49 * @param bool $showHidden
|
Chris@0
|
50 */
|
Chris@14
|
51 public function __construct(Application $application, $namespace = null, $showHidden = false)
|
Chris@0
|
52 {
|
Chris@0
|
53 $this->application = $application;
|
Chris@0
|
54 $this->namespace = $namespace;
|
Chris@14
|
55 $this->showHidden = $showHidden;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * @return array
|
Chris@0
|
60 */
|
Chris@0
|
61 public function getNamespaces()
|
Chris@0
|
62 {
|
Chris@0
|
63 if (null === $this->namespaces) {
|
Chris@0
|
64 $this->inspectApplication();
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 return $this->namespaces;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * @return Command[]
|
Chris@0
|
72 */
|
Chris@0
|
73 public function getCommands()
|
Chris@0
|
74 {
|
Chris@0
|
75 if (null === $this->commands) {
|
Chris@0
|
76 $this->inspectApplication();
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 return $this->commands;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * @param string $name
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return Command
|
Chris@0
|
86 *
|
Chris@0
|
87 * @throws CommandNotFoundException
|
Chris@0
|
88 */
|
Chris@0
|
89 public function getCommand($name)
|
Chris@0
|
90 {
|
Chris@0
|
91 if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
|
Chris@0
|
92 throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name));
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 private function inspectApplication()
|
Chris@0
|
99 {
|
Chris@17
|
100 $this->commands = [];
|
Chris@17
|
101 $this->namespaces = [];
|
Chris@0
|
102
|
Chris@0
|
103 $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
|
Chris@0
|
104 foreach ($this->sortCommands($all) as $namespace => $commands) {
|
Chris@17
|
105 $names = [];
|
Chris@0
|
106
|
Chris@0
|
107 /** @var Command $command */
|
Chris@0
|
108 foreach ($commands as $name => $command) {
|
Chris@14
|
109 if (!$command->getName() || (!$this->showHidden && $command->isHidden())) {
|
Chris@0
|
110 continue;
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 if ($command->getName() === $name) {
|
Chris@0
|
114 $this->commands[$name] = $command;
|
Chris@0
|
115 } else {
|
Chris@0
|
116 $this->aliases[$name] = $command;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 $names[] = $name;
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@17
|
122 $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names];
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * @return array
|
Chris@0
|
128 */
|
Chris@0
|
129 private function sortCommands(array $commands)
|
Chris@0
|
130 {
|
Chris@17
|
131 $namespacedCommands = [];
|
Chris@17
|
132 $globalCommands = [];
|
Chris@0
|
133 foreach ($commands as $name => $command) {
|
Chris@0
|
134 $key = $this->application->extractNamespace($name, 1);
|
Chris@0
|
135 if (!$key) {
|
Chris@0
|
136 $globalCommands['_global'][$name] = $command;
|
Chris@0
|
137 } else {
|
Chris@0
|
138 $namespacedCommands[$key][$name] = $command;
|
Chris@0
|
139 }
|
Chris@0
|
140 }
|
Chris@0
|
141 ksort($namespacedCommands);
|
Chris@0
|
142 $namespacedCommands = array_merge($globalCommands, $namespacedCommands);
|
Chris@0
|
143
|
Chris@0
|
144 foreach ($namespacedCommands as &$commandsSet) {
|
Chris@0
|
145 ksort($commandsSet);
|
Chris@0
|
146 }
|
Chris@0
|
147 // unset reference to keep scope clear
|
Chris@0
|
148 unset($commandsSet);
|
Chris@0
|
149
|
Chris@0
|
150 return $namespacedCommands;
|
Chris@0
|
151 }
|
Chris@0
|
152 }
|