[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/console/Symfony/Component/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  class XmlDescriptor extends Descriptor
  26  {
  27      /**
  28       * {@inheritdoc}
  29       */
  30      protected function describeInputArgument(InputArgument $argument, array $options = array())
  31      {
  32          $dom = new \DOMDocument('1.0', 'UTF-8');
  33  
  34          $dom->appendChild($objectXML = $dom->createElement('argument'));
  35          $objectXML->setAttribute('name', $argument->getName());
  36          $objectXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
  37          $objectXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
  38          $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
  39          $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
  40  
  41          $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  42          $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : (is_bool($argument->getDefault()) ? array(var_export($argument->getDefault(), true)) : ($argument->getDefault() ? array($argument->getDefault()) : array()));
  43          foreach ($defaults as $default) {
  44              $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  45              $defaultXML->appendChild($dom->createTextNode($default));
  46          }
  47  
  48          return $this->output($dom, $options);
  49      }
  50  
  51      /**
  52       * {@inheritdoc}
  53       */
  54      protected function describeInputOption(InputOption $option, array $options = array())
  55      {
  56          $dom = new \DOMDocument('1.0', 'UTF-8');
  57  
  58          $dom->appendChild($objectXML = $dom->createElement('option'));
  59          $objectXML->setAttribute('name', '--'.$option->getName());
  60          $pos = strpos($option->getShortcut(), '|');
  61          if (false !== $pos) {
  62              $objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
  63              $objectXML->setAttribute('shortcuts', '-'.implode('|-', explode('|', $option->getShortcut())));
  64          } else {
  65              $objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
  66          }
  67          $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
  68          $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
  69          $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
  70          $objectXML->appendChild($descriptionXML = $dom->createElement('description'));
  71          $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
  72  
  73          if ($option->acceptValue()) {
  74              $defaults = is_array($option->getDefault()) ? $option->getDefault() : (is_bool($option->getDefault()) ? array(var_export($option->getDefault(), true)) : ($option->getDefault() ? array($option->getDefault()) : array()));
  75              $objectXML->appendChild($defaultsXML = $dom->createElement('defaults'));
  76  
  77              if (!empty($defaults)) {
  78                  foreach ($defaults as $default) {
  79                      $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
  80                      $defaultXML->appendChild($dom->createTextNode($default));
  81                  }
  82              }
  83          }
  84  
  85          return $this->output($dom, $options);
  86      }
  87  
  88      /**
  89       * {@inheritdoc}
  90       */
  91      protected function describeInputDefinition(InputDefinition $definition, array $options = array())
  92      {
  93          $dom = new \DOMDocument('1.0', 'UTF-8');
  94          $dom->appendChild($definitionXML = $dom->createElement('definition'));
  95  
  96          $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
  97          foreach ($definition->getArguments() as $argument) {
  98              $this->appendDocument($argumentsXML, $this->describeInputArgument($argument, array('as_dom' => true)));
  99          }
 100  
 101          $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
 102          foreach ($definition->getOptions() as $option) {
 103              $this->appendDocument($optionsXML, $this->describeInputOption($option, array('as_dom' => true)));
 104          }
 105  
 106          return $this->output($dom, $options);
 107      }
 108  
 109      /**
 110       * {@inheritdoc}
 111       */
 112      protected function describeCommand(Command $command, array $options = array())
 113      {
 114          $dom = new \DOMDocument('1.0', 'UTF-8');
 115          $dom->appendChild($commandXML = $dom->createElement('command'));
 116  
 117          $command->getSynopsis();
 118          $command->mergeApplicationDefinition(false);
 119  
 120          $commandXML->setAttribute('id', $command->getName());
 121          $commandXML->setAttribute('name', $command->getName());
 122  
 123          $commandXML->appendChild($usageXML = $dom->createElement('usage'));
 124          $usageXML->appendChild($dom->createTextNode(sprintf($command->getSynopsis(), '')));
 125  
 126          $commandXML->appendChild($descriptionXML = $dom->createElement('description'));
 127          $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getDescription())));
 128  
 129          $commandXML->appendChild($helpXML = $dom->createElement('help'));
 130          $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));
 131  
 132          $commandXML->appendChild($aliasesXML = $dom->createElement('aliases'));
 133          foreach ($command->getAliases() as $alias) {
 134              $aliasesXML->appendChild($aliasXML = $dom->createElement('alias'));
 135              $aliasXML->appendChild($dom->createTextNode($alias));
 136          }
 137  
 138          $definitionXML = $this->describeInputDefinition($command->getNativeDefinition(), array('as_dom' => true));
 139          $this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));
 140  
 141          return $this->output($dom, $options);
 142      }
 143  
 144      /**
 145       * {@inheritdoc}
 146       */
 147      protected function describeApplication(Application $application, array $options = array())
 148      {
 149          $dom = new \DOMDocument('1.0', 'UTF-8');
 150          $dom->appendChild($rootXml = $dom->createElement('symfony'));
 151          $rootXml->appendChild($commandsXML = $dom->createElement('commands'));
 152  
 153          $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
 154          $description = new ApplicationDescription($application, $describedNamespace);
 155  
 156          if ($describedNamespace) {
 157              $commandsXML->setAttribute('namespace', $describedNamespace);
 158          }
 159  
 160          foreach ($description->getCommands() as $command) {
 161              $this->appendDocument($commandsXML, $this->describeCommand($command, array('as_dom' => true)));
 162          }
 163  
 164          if (!$describedNamespace) {
 165              $rootXml->appendChild($namespacesXML = $dom->createElement('namespaces'));
 166  
 167              foreach ($description->getNamespaces() as $namespace) {
 168                  $namespacesXML->appendChild($namespaceArrayXML = $dom->createElement('namespace'));
 169                  $namespaceArrayXML->setAttribute('id', $namespace['id']);
 170  
 171                  foreach ($namespace['commands'] as $name) {
 172                      $namespaceArrayXML->appendChild($commandXML = $dom->createElement('command'));
 173                      $commandXML->appendChild($dom->createTextNode($name));
 174                  }
 175              }
 176          }
 177  
 178          return $this->output($dom, $options);
 179      }
 180  
 181      /**
 182       * Appends document children to parent node.
 183       *
 184       * @param \DOMNode $parentNode
 185       * @param \DOMNode $importedParent
 186       */
 187      private function appendDocument(\DOMNode $parentNode, \DOMNode $importedParent)
 188      {
 189          foreach ($importedParent->childNodes as $childNode) {
 190              $parentNode->appendChild($parentNode->ownerDocument->importNode($childNode, true));
 191          }
 192      }
 193  
 194      /**
 195       * Outputs document as DOMDocument or string according to options.
 196       *
 197       * @param \DOMDocument $dom
 198       * @param array        $options
 199       *
 200       * @return \DOMDocument|string
 201       */
 202      private function output(\DOMDocument $dom, array $options)
 203      {
 204          if (isset($options['as_dom']) && $options['as_dom']) {
 205              return $dom;
 206          }
 207  
 208          $dom->formatOutput = true;
 209  
 210          return $dom->saveXML();
 211      }
 212  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1