PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Descriptor / ApplicationDescription.php

ApplicationDescription.php in Depicter — Popup & Slider Builder 1.3.2, at vendor/symfony/console/Descriptor/ApplicationDescription.php

144 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public const GLOBAL_NAMESPACE = '_global';
26
27 private $application;
28 private $namespace;
29 private $showHidden;
30
31 /**
32 * @var array
33 */
34 private $namespaces;
35
36 /**
37 * @var Command[]
38 */
39 private $commands;
40
41 /**
42 * @var Command[]
43 */
44 private $aliases;
45
46 public function __construct(Application $application, string $namespace = null, bool $showHidden = false)
47 {
48 $this->application = $application;
49 $this->namespace = $namespace;
50 $this->showHidden = $showHidden;
51 }
52
53 public function getNamespaces(): array
54 {
55 if (null === $this->namespaces) {
56 $this->inspectApplication();
57 }
58
59 return $this->namespaces;
60 }
61
62 /**
63 * @return Command[]
64 */
65 public function getCommands(): array
66 {
67 if (null === $this->commands) {
68 $this->inspectApplication();
69 }
70
71 return $this->commands;
72 }
73
74 /**
75 * @throws CommandNotFoundException
76 */
77 public function getCommand(string $name): Command
78 {
79 if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
80 throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
81 }
82
83 return $this->commands[$name] ?? $this->aliases[$name];
84 }
85
86 private function inspectApplication()
87 {
88 $this->commands = [];
89 $this->namespaces = [];
90
91 $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
92 foreach ($this->sortCommands($all) as $namespace => $commands) {
93 $names = [];
94
95 /** @var Command $command */
96 foreach ($commands as $name => $command) {
97 if (!$command->getName() || (!$this->showHidden && $command->isHidden())) {
98 continue;
99 }
100
101 if ($command->getName() === $name) {
102 $this->commands[$name] = $command;
103 } else {
104 $this->aliases[$name] = $command;
105 }
106
107 $names[] = $name;
108 }
109
110 $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names];
111 }
112 }
113
114 private function sortCommands(array $commands): array
115 {
116 $namespacedCommands = [];
117 $globalCommands = [];
118 $sortedCommands = [];
119 foreach ($commands as $name => $command) {
120 $key = $this->application->extractNamespace($name, 1);
121 if (\in_array($key, ['', self::GLOBAL_NAMESPACE], true)) {
122 $globalCommands[$name] = $command;
123 } else {
124 $namespacedCommands[$key][$name] = $command;
125 }
126 }
127
128 if ($globalCommands) {
129 ksort($globalCommands);
130 $sortedCommands[self::GLOBAL_NAMESPACE] = $globalCommands;
131 }
132
133 if ($namespacedCommands) {
134 ksort($namespacedCommands);
135 foreach ($namespacedCommands as $key => $commandsSet) {
136 ksort($commandsSet);
137 $sortedCommands[$key] = $commandsSet;
138 }
139 }
140
141 return $sortedCommands;
142 }
143 }
144