[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer