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