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