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