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\Input\InputArgument;
|
Chris@0
|
17 use Symfony\Component\Console\Input\InputDefinition;
|
Chris@0
|
18 use Symfony\Component\Console\Input\InputOption;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * JSON descriptor.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @author Jean-François Simon <contact@jfsimon.fr>
|
Chris@0
|
24 *
|
Chris@0
|
25 * @internal
|
Chris@0
|
26 */
|
Chris@0
|
27 class JsonDescriptor extends Descriptor
|
Chris@0
|
28 {
|
Chris@0
|
29 /**
|
Chris@0
|
30 * {@inheritdoc}
|
Chris@0
|
31 */
|
Chris@17
|
32 protected function describeInputArgument(InputArgument $argument, array $options = [])
|
Chris@0
|
33 {
|
Chris@0
|
34 $this->writeData($this->getInputArgumentData($argument), $options);
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * {@inheritdoc}
|
Chris@0
|
39 */
|
Chris@17
|
40 protected function describeInputOption(InputOption $option, array $options = [])
|
Chris@0
|
41 {
|
Chris@0
|
42 $this->writeData($this->getInputOptionData($option), $options);
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * {@inheritdoc}
|
Chris@0
|
47 */
|
Chris@17
|
48 protected function describeInputDefinition(InputDefinition $definition, array $options = [])
|
Chris@0
|
49 {
|
Chris@0
|
50 $this->writeData($this->getInputDefinitionData($definition), $options);
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * {@inheritdoc}
|
Chris@0
|
55 */
|
Chris@17
|
56 protected function describeCommand(Command $command, array $options = [])
|
Chris@0
|
57 {
|
Chris@0
|
58 $this->writeData($this->getCommandData($command), $options);
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@17
|
64 protected function describeApplication(Application $application, array $options = [])
|
Chris@0
|
65 {
|
Chris@0
|
66 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
|
Chris@14
|
67 $description = new ApplicationDescription($application, $describedNamespace, true);
|
Chris@17
|
68 $commands = [];
|
Chris@0
|
69
|
Chris@0
|
70 foreach ($description->getCommands() as $command) {
|
Chris@0
|
71 $commands[] = $this->getCommandData($command);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@17
|
74 $data = [];
|
Chris@14
|
75 if ('UNKNOWN' !== $application->getName()) {
|
Chris@14
|
76 $data['application']['name'] = $application->getName();
|
Chris@14
|
77 if ('UNKNOWN' !== $application->getVersion()) {
|
Chris@14
|
78 $data['application']['version'] = $application->getVersion();
|
Chris@14
|
79 }
|
Chris@14
|
80 }
|
Chris@14
|
81
|
Chris@14
|
82 $data['commands'] = $commands;
|
Chris@14
|
83
|
Chris@14
|
84 if ($describedNamespace) {
|
Chris@14
|
85 $data['namespace'] = $describedNamespace;
|
Chris@14
|
86 } else {
|
Chris@14
|
87 $data['namespaces'] = array_values($description->getNamespaces());
|
Chris@14
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 $this->writeData($data, $options);
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Writes data as json.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return array|string
|
Chris@0
|
97 */
|
Chris@0
|
98 private function writeData(array $data, array $options)
|
Chris@0
|
99 {
|
Chris@0
|
100 $this->write(json_encode($data, isset($options['json_encoding']) ? $options['json_encoding'] : 0));
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * @return array
|
Chris@0
|
105 */
|
Chris@0
|
106 private function getInputArgumentData(InputArgument $argument)
|
Chris@0
|
107 {
|
Chris@17
|
108 return [
|
Chris@0
|
109 'name' => $argument->getName(),
|
Chris@0
|
110 'is_required' => $argument->isRequired(),
|
Chris@0
|
111 'is_array' => $argument->isArray(),
|
Chris@0
|
112 'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $argument->getDescription()),
|
Chris@12
|
113 'default' => INF === $argument->getDefault() ? 'INF' : $argument->getDefault(),
|
Chris@17
|
114 ];
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * @return array
|
Chris@0
|
119 */
|
Chris@0
|
120 private function getInputOptionData(InputOption $option)
|
Chris@0
|
121 {
|
Chris@17
|
122 return [
|
Chris@0
|
123 'name' => '--'.$option->getName(),
|
Chris@16
|
124 'shortcut' => $option->getShortcut() ? '-'.str_replace('|', '|-', $option->getShortcut()) : '',
|
Chris@0
|
125 'accept_value' => $option->acceptValue(),
|
Chris@0
|
126 'is_value_required' => $option->isValueRequired(),
|
Chris@0
|
127 'is_multiple' => $option->isArray(),
|
Chris@0
|
128 'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
|
Chris@12
|
129 'default' => INF === $option->getDefault() ? 'INF' : $option->getDefault(),
|
Chris@17
|
130 ];
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * @return array
|
Chris@0
|
135 */
|
Chris@0
|
136 private function getInputDefinitionData(InputDefinition $definition)
|
Chris@0
|
137 {
|
Chris@17
|
138 $inputArguments = [];
|
Chris@0
|
139 foreach ($definition->getArguments() as $name => $argument) {
|
Chris@0
|
140 $inputArguments[$name] = $this->getInputArgumentData($argument);
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@17
|
143 $inputOptions = [];
|
Chris@0
|
144 foreach ($definition->getOptions() as $name => $option) {
|
Chris@0
|
145 $inputOptions[$name] = $this->getInputOptionData($option);
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@17
|
148 return ['arguments' => $inputArguments, 'options' => $inputOptions];
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * @return array
|
Chris@0
|
153 */
|
Chris@0
|
154 private function getCommandData(Command $command)
|
Chris@0
|
155 {
|
Chris@0
|
156 $command->getSynopsis();
|
Chris@0
|
157 $command->mergeApplicationDefinition(false);
|
Chris@0
|
158
|
Chris@17
|
159 return [
|
Chris@0
|
160 'name' => $command->getName(),
|
Chris@17
|
161 'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
|
Chris@0
|
162 'description' => $command->getDescription(),
|
Chris@0
|
163 'help' => $command->getProcessedHelp(),
|
Chris@0
|
164 'definition' => $this->getInputDefinitionData($command->getNativeDefinition()),
|
Chris@14
|
165 'hidden' => $command->isHidden(),
|
Chris@17
|
166 ];
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|