[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/console/Descriptor/ -> XmlDescriptor.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1