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