comparison vendor/symfony/console/Descriptor/ApplicationDescription.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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
14 use Symfony\Component\Console\Application;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Exception\CommandNotFoundException;
17
18 /**
19 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
20 *
21 * @internal
22 */
23 class ApplicationDescription
24 {
25 const GLOBAL_NAMESPACE = '_global';
26
27 /**
28 * @var Application
29 */
30 private $application;
31
32 /**
33 * @var null|string
34 */
35 private $namespace;
36
37 /**
38 * @var array
39 */
40 private $namespaces;
41
42 /**
43 * @var Command[]
44 */
45 private $commands;
46
47 /**
48 * @var Command[]
49 */
50 private $aliases;
51
52 /**
53 * Constructor.
54 *
55 * @param Application $application
56 * @param string|null $namespace
57 */
58 public function __construct(Application $application, $namespace = null)
59 {
60 $this->application = $application;
61 $this->namespace = $namespace;
62 }
63
64 /**
65 * @return array
66 */
67 public function getNamespaces()
68 {
69 if (null === $this->namespaces) {
70 $this->inspectApplication();
71 }
72
73 return $this->namespaces;
74 }
75
76 /**
77 * @return Command[]
78 */
79 public function getCommands()
80 {
81 if (null === $this->commands) {
82 $this->inspectApplication();
83 }
84
85 return $this->commands;
86 }
87
88 /**
89 * @param string $name
90 *
91 * @return Command
92 *
93 * @throws CommandNotFoundException
94 */
95 public function getCommand($name)
96 {
97 if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
98 throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name));
99 }
100
101 return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
102 }
103
104 private function inspectApplication()
105 {
106 $this->commands = array();
107 $this->namespaces = array();
108
109 $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
110 foreach ($this->sortCommands($all) as $namespace => $commands) {
111 $names = array();
112
113 /** @var Command $command */
114 foreach ($commands as $name => $command) {
115 if (!$command->getName() || $command->isHidden()) {
116 continue;
117 }
118
119 if ($command->getName() === $name) {
120 $this->commands[$name] = $command;
121 } else {
122 $this->aliases[$name] = $command;
123 }
124
125 $names[] = $name;
126 }
127
128 $this->namespaces[$namespace] = array('id' => $namespace, 'commands' => $names);
129 }
130 }
131
132 /**
133 * @param array $commands
134 *
135 * @return array
136 */
137 private function sortCommands(array $commands)
138 {
139 $namespacedCommands = array();
140 $globalCommands = array();
141 foreach ($commands as $name => $command) {
142 $key = $this->application->extractNamespace($name, 1);
143 if (!$key) {
144 $globalCommands['_global'][$name] = $command;
145 } else {
146 $namespacedCommands[$key][$name] = $command;
147 }
148 }
149 ksort($namespacedCommands);
150 $namespacedCommands = array_merge($globalCommands, $namespacedCommands);
151
152 foreach ($namespacedCommands as &$commandsSet) {
153 ksort($commandsSet);
154 }
155 // unset reference to keep scope clear
156 unset($commandsSet);
157
158 return $namespacedCommands;
159 }
160 }