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