PageRenderTime 67ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/console/Symfony/Component/Console/Descriptor/XmlDescriptor.php

https://bitbucket.org/larryg/powerhut
PHP | 212 lines | 133 code | 37 blank | 42 comment | 8 complexity | c5804cb1a266ff85ed52b0a161396194 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Descriptor;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputOption;
  16. /**
  17. * XML descriptor.
  18. *
  19. * @author Jean-François Simon <contact@jfsimon.fr>
  20. */
  21. class XmlDescriptor extends Descriptor
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function describeInputArgument(InputArgument $argument, array $options = array())
  27. {
  28. $dom = new \DOMDocument('1.0', 'UTF-8');
  29. $dom->appendChild($objectXML = $dom->createElement('argument'));
  30. $objectXML->setAttribute('name', $argument->getName());
  31. $objectXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
  32. $objectXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
  33. $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
  34. $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
  35. $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  36. $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : (is_bool($argument->getDefault()) ? array(var_export($argument->getDefault(), true)) : ($argument->getDefault() ? array($argument->getDefault()) : array()));
  37. foreach ($defaults as $default) {
  38. $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  39. $defaultXML->appendChild($dom->createTextNode($default));
  40. }
  41. return $this->output($dom, $options);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function describeInputOption(InputOption $option, array $options = array())
  47. {
  48. $dom = new \DOMDocument('1.0', 'UTF-8');
  49. $dom->appendChild($objectXML = $dom->createElement('option'));
  50. $objectXML->setAttribute('name', '--'.$option->getName());
  51. $pos = strpos($option->getShortcut(), '|');
  52. if (false !== $pos) {
  53. $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
  54. $objectXML->setAttribute('shortcuts', '-'.implode('|-', explode('|', $option->getShortcut())));
  55. } else {
  56. $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
  57. }
  58. $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
  59. $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
  60. $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
  61. $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
  62. $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
  63. if ($option->acceptValue()) {
  64. $defaults = is_array($option->getDefault()) ? $option->getDefault() : (is_bool($option->getDefault()) ? array(var_export($option->getDefault(), true)) : ($option->getDefault() ? array($option->getDefault()) : array()));
  65. $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  66. if (!empty($defaults)) {
  67. foreach ($defaults as $default) {
  68. $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  69. $defaultXML->appendChild($dom->createTextNode($default));
  70. }
  71. }
  72. }
  73. return $this->output($dom, $options);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function describeInputDefinition(InputDefinition $definition, array $options = array())
  79. {
  80. $dom = new \DOMDocument('1.0', 'UTF-8');
  81. $dom->appendChild($definitionXML = $dom->createElement('definition'));
  82. $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
  83. foreach ($definition->getArguments() as $argument) {
  84. $this->appendDocument($argumentsXML, $this->describeInputArgument($argument, array('as_dom' => true)));
  85. }
  86. $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
  87. foreach ($definition->getOptions() as $option) {
  88. $this->appendDocument($optionsXML, $this->describeInputOption($option, array('as_dom' => true)));
  89. }
  90. return $this->output($dom, $options);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function describeCommand(Command $command, array $options = array())
  96. {
  97. $dom = new \DOMDocument('1.0', 'UTF-8');
  98. $dom->appendChild($commandXML = $dom->createElement('command'));
  99. $command->getSynopsis();
  100. $command->mergeApplicationDefinition(false);
  101. $commandXML->setAttribute('id', $command->getName());
  102. $commandXML->setAttribute('name', $command->getName());
  103. $commandXML->appendChild($usageXML = $dom->createElement('usage'));
  104. $usageXML->appendChild($dom->createTextNode(sprintf($command->getSynopsis(), '')));
  105. $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
  106. $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
  107. $commandXML->appendChild($helpXML = $dom->createElement('help'));
  108. $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
  109. $commandXML->appendChild($aliasesXML = $dom->createElement('aliases'));
  110. foreach ($command->getAliases() as $alias) {
  111. $aliasesXML->appendChild($aliasXML = $dom->createElement('alias'));
  112. $aliasXML->appendChild($dom->createTextNode($alias));
  113. }
  114. $definitionXML = $this->describeInputDefinition($command->getNativeDefinition(), array('as_dom' => true));
  115. $this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
  116. return $this->output($dom, $options);
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. protected function describeApplication(Application $application, array $options = array())
  122. {
  123. $dom = new \DOMDocument('1.0', 'UTF-8');
  124. $dom->appendChild($rootXml = $dom->createElement('symfony'));
  125. $rootXml->appendChild($commandsXML = $dom->createElement('commands'));
  126. $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
  127. $description = new ApplicationDescription($application, $describedNamespace);
  128. if ($describedNamespace) {
  129. $commandsXML->setAttribute('namespace', $describedNamespace);
  130. }
  131. foreach ($description->getCommands() as $command) {
  132. $this->appendDocument($commandsXML, $this->describeCommand($command, array('as_dom' => true)));
  133. }
  134. if (!$describedNamespace) {
  135. $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));
  136. foreach ($description->getNamespaces() as $namespace) {
  137. $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
  138. $namespaceArrayXML->setAttribute('id', $namespace['id']);
  139. foreach ($namespace['commands'] as $name) {
  140. $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
  141. $commandXML->appendChild($dom->createTextNode($name));
  142. }
  143. }
  144. }
  145. return $this->output($dom, $options);
  146. }
  147. /**
  148. * Appends document children to parent node.
  149. *
  150. * @param \DOMNode $parentNode
  151. * @param \DOMNode $importedParent
  152. */
  153. private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
  154. {
  155. foreach ($importedParent->childNodes as $childNode) {
  156. $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
  157. }
  158. }
  159. /**
  160. * Outputs document as DOMDocument or string according to options.
  161. *
  162. * @param \DOMDocument $dom
  163. * @param array $options
  164. *
  165. * @return \DOMDocument|string
  166. */
  167. private function output(\DOMDocument $dom, array $options)
  168. {
  169. if (isset($options['as_dom']) && $options['as_dom']) {
  170. return $dom;
  171. }
  172. $dom->formatOutput = true;
  173. return $dom->saveXML();
  174. }
  175. }