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 * XML 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 XmlDescriptor extends Descriptor
|
Chris@0
|
28 {
|
Chris@0
|
29 /**
|
Chris@0
|
30 * @return \DOMDocument
|
Chris@0
|
31 */
|
Chris@0
|
32 public function getInputDefinitionDocument(InputDefinition $definition)
|
Chris@0
|
33 {
|
Chris@0
|
34 $dom = new \DOMDocument('1.0', 'UTF-8');
|
Chris@0
|
35 $dom->appendChild($definitionXML = $dom->createElement('definition'));
|
Chris@0
|
36
|
Chris@0
|
37 $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
|
Chris@0
|
38 foreach ($definition->getArguments() as $argument) {
|
Chris@0
|
39 $this->appendDocument($argumentsXML, $this->getInputArgumentDocument($argument));
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
|
Chris@0
|
43 foreach ($definition->getOptions() as $option) {
|
Chris@0
|
44 $this->appendDocument($optionsXML, $this->getInputOptionDocument($option));
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 return $dom;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * @return \DOMDocument
|
Chris@0
|
52 */
|
Chris@0
|
53 public function getCommandDocument(Command $command)
|
Chris@0
|
54 {
|
Chris@0
|
55 $dom = new \DOMDocument('1.0', 'UTF-8');
|
Chris@0
|
56 $dom->appendChild($commandXML = $dom->createElement('command'));
|
Chris@0
|
57
|
Chris@0
|
58 $command->getSynopsis();
|
Chris@0
|
59 $command->mergeApplicationDefinition(false);
|
Chris@0
|
60
|
Chris@0
|
61 $commandXML->setAttribute('id', $command->getName());
|
Chris@0
|
62 $commandXML->setAttribute('name', $command->getName());
|
Chris@14
|
63 $commandXML->setAttribute('hidden', $command->isHidden() ? 1 : 0);
|
Chris@0
|
64
|
Chris@0
|
65 $commandXML->appendChild($usagesXML = $dom->createElement('usages'));
|
Chris@0
|
66
|
Chris@17
|
67 foreach (array_merge([$command->getSynopsis()], $command->getAliases(), $command->getUsages()) as $usage) {
|
Chris@0
|
68 $usagesXML->appendChild($dom->createElement('usage', $usage));
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
|
Chris@0
|
72 $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
|
Chris@0
|
73
|
Chris@0
|
74 $commandXML->appendChild($helpXML = $dom->createElement('help'));
|
Chris@0
|
75 $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
|
Chris@0
|
76
|
Chris@0
|
77 $definitionXML = $this->getInputDefinitionDocument($command->getNativeDefinition());
|
Chris@0
|
78 $this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
|
Chris@0
|
79
|
Chris@0
|
80 return $dom;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * @param Application $application
|
Chris@0
|
85 * @param string|null $namespace
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return \DOMDocument
|
Chris@0
|
88 */
|
Chris@0
|
89 public function getApplicationDocument(Application $application, $namespace = null)
|
Chris@0
|
90 {
|
Chris@0
|
91 $dom = new \DOMDocument('1.0', 'UTF-8');
|
Chris@0
|
92 $dom->appendChild($rootXml = $dom->createElement('symfony'));
|
Chris@0
|
93
|
Chris@14
|
94 if ('UNKNOWN' !== $application->getName()) {
|
Chris@0
|
95 $rootXml->setAttribute('name', $application->getName());
|
Chris@14
|
96 if ('UNKNOWN' !== $application->getVersion()) {
|
Chris@0
|
97 $rootXml->setAttribute('version', $application->getVersion());
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 $rootXml->appendChild($commandsXML = $dom->createElement('commands'));
|
Chris@0
|
102
|
Chris@14
|
103 $description = new ApplicationDescription($application, $namespace, true);
|
Chris@0
|
104
|
Chris@0
|
105 if ($namespace) {
|
Chris@0
|
106 $commandsXML->setAttribute('namespace', $namespace);
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 foreach ($description->getCommands() as $command) {
|
Chris@0
|
110 $this->appendDocument($commandsXML, $this->getCommandDocument($command));
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 if (!$namespace) {
|
Chris@0
|
114 $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));
|
Chris@0
|
115
|
Chris@0
|
116 foreach ($description->getNamespaces() as $namespaceDescription) {
|
Chris@0
|
117 $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
|
Chris@0
|
118 $namespaceArrayXML->setAttribute('id', $namespaceDescription['id']);
|
Chris@0
|
119
|
Chris@0
|
120 foreach ($namespaceDescription['commands'] as $name) {
|
Chris@0
|
121 $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
|
Chris@0
|
122 $commandXML->appendChild($dom->createTextNode($name));
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 return $dom;
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 /**
|
Chris@0
|
131 * {@inheritdoc}
|
Chris@0
|
132 */
|
Chris@17
|
133 protected function describeInputArgument(InputArgument $argument, array $options = [])
|
Chris@0
|
134 {
|
Chris@0
|
135 $this->writeDocument($this->getInputArgumentDocument($argument));
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * {@inheritdoc}
|
Chris@0
|
140 */
|
Chris@17
|
141 protected function describeInputOption(InputOption $option, array $options = [])
|
Chris@0
|
142 {
|
Chris@0
|
143 $this->writeDocument($this->getInputOptionDocument($option));
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * {@inheritdoc}
|
Chris@0
|
148 */
|
Chris@17
|
149 protected function describeInputDefinition(InputDefinition $definition, array $options = [])
|
Chris@0
|
150 {
|
Chris@0
|
151 $this->writeDocument($this->getInputDefinitionDocument($definition));
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 /**
|
Chris@0
|
155 * {@inheritdoc}
|
Chris@0
|
156 */
|
Chris@17
|
157 protected function describeCommand(Command $command, array $options = [])
|
Chris@0
|
158 {
|
Chris@0
|
159 $this->writeDocument($this->getCommandDocument($command));
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * {@inheritdoc}
|
Chris@0
|
164 */
|
Chris@17
|
165 protected function describeApplication(Application $application, array $options = [])
|
Chris@0
|
166 {
|
Chris@0
|
167 $this->writeDocument($this->getApplicationDocument($application, isset($options['namespace']) ? $options['namespace'] : null));
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * Appends document children to parent node.
|
Chris@0
|
172 */
|
Chris@0
|
173 private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
|
Chris@0
|
174 {
|
Chris@0
|
175 foreach ($importedParent->childNodes as $childNode) {
|
Chris@0
|
176 $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
|
Chris@0
|
177 }
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * Writes DOM document.
|
Chris@0
|
182 *
|
Chris@0
|
183 * @return \DOMDocument|string
|
Chris@0
|
184 */
|
Chris@0
|
185 private function writeDocument(\DOMDocument $dom)
|
Chris@0
|
186 {
|
Chris@0
|
187 $dom->formatOutput = true;
|
Chris@0
|
188 $this->write($dom->saveXML());
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * @return \DOMDocument
|
Chris@0
|
193 */
|
Chris@0
|
194 private function getInputArgumentDocument(InputArgument $argument)
|
Chris@0
|
195 {
|
Chris@0
|
196 $dom = new \DOMDocument('1.0', 'UTF-8');
|
Chris@0
|
197
|
Chris@0
|
198 $dom->appendChild($objectXML = $dom->createElement('argument'));
|
Chris@0
|
199 $objectXML->setAttribute('name', $argument->getName());
|
Chris@0
|
200 $objectXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
|
Chris@0
|
201 $objectXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
|
Chris@0
|
202 $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
|
Chris@0
|
203 $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
|
Chris@0
|
204
|
Chris@0
|
205 $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
|
Chris@17
|
206 $defaults = \is_array($argument->getDefault()) ? $argument->getDefault() : (\is_bool($argument->getDefault()) ? [var_export($argument->getDefault(), true)] : ($argument->getDefault() ? [$argument->getDefault()] : []));
|
Chris@0
|
207 foreach ($defaults as $default) {
|
Chris@0
|
208 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
|
Chris@0
|
209 $defaultXML->appendChild($dom->createTextNode($default));
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 return $dom;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * @return \DOMDocument
|
Chris@0
|
217 */
|
Chris@0
|
218 private function getInputOptionDocument(InputOption $option)
|
Chris@0
|
219 {
|
Chris@0
|
220 $dom = new \DOMDocument('1.0', 'UTF-8');
|
Chris@0
|
221
|
Chris@0
|
222 $dom->appendChild($objectXML = $dom->createElement('option'));
|
Chris@0
|
223 $objectXML->setAttribute('name', '--'.$option->getName());
|
Chris@0
|
224 $pos = strpos($option->getShortcut(), '|');
|
Chris@0
|
225 if (false !== $pos) {
|
Chris@0
|
226 $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
|
Chris@16
|
227 $objectXML->setAttribute('shortcuts', '-'.str_replace('|', '|-', $option->getShortcut()));
|
Chris@0
|
228 } else {
|
Chris@0
|
229 $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
|
Chris@0
|
230 }
|
Chris@0
|
231 $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
|
Chris@0
|
232 $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
|
Chris@0
|
233 $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
|
Chris@0
|
234 $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
|
Chris@0
|
235 $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
|
Chris@0
|
236
|
Chris@0
|
237 if ($option->acceptValue()) {
|
Chris@17
|
238 $defaults = \is_array($option->getDefault()) ? $option->getDefault() : (\is_bool($option->getDefault()) ? [var_export($option->getDefault(), true)] : ($option->getDefault() ? [$option->getDefault()] : []));
|
Chris@0
|
239 $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
|
Chris@0
|
240
|
Chris@0
|
241 if (!empty($defaults)) {
|
Chris@0
|
242 foreach ($defaults as $default) {
|
Chris@0
|
243 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
|
Chris@0
|
244 $defaultXML->appendChild($dom->createTextNode($default));
|
Chris@0
|
245 }
|
Chris@0
|
246 }
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 return $dom;
|
Chris@0
|
250 }
|
Chris@0
|
251 }
|