[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/console/Descriptor/ -> JsonDescriptor.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   * JSON descriptor.
  22   *
  23   * @author Jean-François Simon <[email protected]>
  24   *
  25   * @internal
  26   */
  27  class JsonDescriptor extends Descriptor
  28  {
  29      /**
  30       * {@inheritdoc}
  31       */
  32      protected function describeInputArgument(InputArgument $argument, array $options = [])
  33      {
  34          $this->writeData($this->getInputArgumentData($argument), $options);
  35      }
  36  
  37      /**
  38       * {@inheritdoc}
  39       */
  40      protected function describeInputOption(InputOption $option, array $options = [])
  41      {
  42          $this->writeData($this->getInputOptionData($option), $options);
  43          if ($option->isNegatable()) {
  44              $this->writeData($this->getInputOptionData($option, true), $options);
  45          }
  46      }
  47  
  48      /**
  49       * {@inheritdoc}
  50       */
  51      protected function describeInputDefinition(InputDefinition $definition, array $options = [])
  52      {
  53          $this->writeData($this->getInputDefinitionData($definition), $options);
  54      }
  55  
  56      /**
  57       * {@inheritdoc}
  58       */
  59      protected function describeCommand(Command $command, array $options = [])
  60      {
  61          $this->writeData($this->getCommandData($command, $options['short'] ?? false), $options);
  62      }
  63  
  64      /**
  65       * {@inheritdoc}
  66       */
  67      protected function describeApplication(Application $application, array $options = [])
  68      {
  69          $describedNamespace = $options['namespace'] ?? null;
  70          $description = new ApplicationDescription($application, $describedNamespace, true);
  71          $commands = [];
  72  
  73          foreach ($description->getCommands() as $command) {
  74              $commands[] = $this->getCommandData($command, $options['short'] ?? false);
  75          }
  76  
  77          $data = [];
  78          if ('UNKNOWN' !== $application->getName()) {
  79              $data['application']['name'] = $application->getName();
  80              if ('UNKNOWN' !== $application->getVersion()) {
  81                  $data['application']['version'] = $application->getVersion();
  82              }
  83          }
  84  
  85          $data['commands'] = $commands;
  86  
  87          if ($describedNamespace) {
  88              $data['namespace'] = $describedNamespace;
  89          } else {
  90              $data['namespaces'] = array_values($description->getNamespaces());
  91          }
  92  
  93          $this->writeData($data, $options);
  94      }
  95  
  96      /**
  97       * Writes data as json.
  98       */
  99      private function writeData(array $data, array $options)
 100      {
 101          $flags = $options['json_encoding'] ?? 0;
 102  
 103          $this->write(json_encode($data, $flags));
 104      }
 105  
 106      private function getInputArgumentData(InputArgument $argument): array
 107      {
 108          return [
 109              'name' => $argument->getName(),
 110              'is_required' => $argument->isRequired(),
 111              'is_array' => $argument->isArray(),
 112              'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $argument->getDescription()),
 113              'default' => \INF === $argument->getDefault() ? 'INF' : $argument->getDefault(),
 114          ];
 115      }
 116  
 117      private function getInputOptionData(InputOption $option, bool $negated = false): array
 118      {
 119          return $negated ? [
 120              'name' => '--no-'.$option->getName(),
 121              'shortcut' => '',
 122              'accept_value' => false,
 123              'is_value_required' => false,
 124              'is_multiple' => false,
 125              'description' => 'Negate the "--'.$option->getName().'" option',
 126              'default' => false,
 127          ] : [
 128              'name' => '--'.$option->getName(),
 129              'shortcut' => $option->getShortcut() ? '-'.str_replace('|', '|-', $option->getShortcut()) : '',
 130              'accept_value' => $option->acceptValue(),
 131              'is_value_required' => $option->isValueRequired(),
 132              'is_multiple' => $option->isArray(),
 133              'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
 134              'default' => \INF === $option->getDefault() ? 'INF' : $option->getDefault(),
 135          ];
 136      }
 137  
 138      private function getInputDefinitionData(InputDefinition $definition): array
 139      {
 140          $inputArguments = [];
 141          foreach ($definition->getArguments() as $name => $argument) {
 142              $inputArguments[$name] = $this->getInputArgumentData($argument);
 143          }
 144  
 145          $inputOptions = [];
 146          foreach ($definition->getOptions() as $name => $option) {
 147              $inputOptions[$name] = $this->getInputOptionData($option);
 148              if ($option->isNegatable()) {
 149                  $inputOptions['no-'.$name] = $this->getInputOptionData($option, true);
 150              }
 151          }
 152  
 153          return ['arguments' => $inputArguments, 'options' => $inputOptions];
 154      }
 155  
 156      private function getCommandData(Command $command, bool $short = false): array
 157      {
 158          $data = [
 159              'name' => $command->getName(),
 160              'description' => $command->getDescription(),
 161          ];
 162  
 163          if ($short) {
 164              $data += [
 165                  'usage' => $command->getAliases(),
 166              ];
 167          } else {
 168              $command->mergeApplicationDefinition(false);
 169  
 170              $data += [
 171                  'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
 172                  'help' => $command->getProcessedHelp(),
 173                  'definition' => $this->getInputDefinitionData($command->getDefinition()),
 174              ];
 175          }
 176  
 177          $data['hidden'] = $command->isHidden();
 178  
 179          return $data;
 180      }
 181  }


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