[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/console/Symfony/Component/Console/Descriptor/ -> TextDescriptor.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   * Text descriptor.
  22   *
  23   * @author Jean-François Simon <contact@jfsimon.fr>
  24   */
  25  class TextDescriptor extends Descriptor
  26  {
  27      /**
  28       * {@inheritdoc}
  29       */
  30      protected function describeInputArgument(InputArgument $argument, array $options = array())
  31      {
  32          if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
  33              $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($argument->getDefault()));
  34          } else {
  35              $default = '';
  36          }
  37  
  38          $nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($argument->getName());
  39          $output = str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $argument->getDescription());
  40          $output = sprintf(" <info>%-{$nameWidth}s</info> %s%s", $argument->getName(), $output, $default);
  41  
  42          return isset($options['raw_text']) && $options['raw_text'] ? strip_tags($output) : $output;
  43      }
  44  
  45      /**
  46       * {@inheritdoc}
  47       */
  48      protected function describeInputOption(InputOption $option, array $options = array())
  49      {
  50          if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
  51              $default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($option->getDefault()));
  52          } else {
  53              $default = '';
  54          }
  55  
  56          $nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($option->getName());
  57          $nameWithShortcutWidth = $nameWidth - strlen($option->getName()) - 2;
  58  
  59          $output = sprintf(" <info>%s</info> %-{$nameWithShortcutWidth}s%s%s%s",
  60              '--'.$option->getName(),
  61              $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '',
  62              str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $option->getDescription()),
  63              $default,
  64              $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
  65          );
  66  
  67          return isset($options['raw_text']) && $options['raw_text'] ? strip_tags($output) : $output;
  68      }
  69  
  70      /**
  71       * {@inheritdoc}
  72       */
  73      protected function describeInputDefinition(InputDefinition $definition, array $options = array())
  74      {
  75          $nameWidth = 0;
  76          foreach ($definition->getOptions() as $option) {
  77              $nameLength = strlen($option->getName()) + 2;
  78              if ($option->getShortcut()) {
  79                  $nameLength += strlen($option->getShortcut()) + 3;
  80              }
  81              $nameWidth = max($nameWidth, $nameLength);
  82          }
  83          foreach ($definition->getArguments() as $argument) {
  84              $nameWidth = max($nameWidth, strlen($argument->getName()));
  85          }
  86          ++$nameWidth;
  87  
  88          $messages = array();
  89  
  90          if ($definition->getArguments()) {
  91              $messages[] = '<comment>Arguments:</comment>';
  92              foreach ($definition->getArguments() as $argument) {
  93                  $messages[] = $this->describeInputArgument($argument, array('name_width' => $nameWidth));
  94              }
  95              $messages[] = '';
  96          }
  97  
  98          if ($definition->getOptions()) {
  99              $messages[] = '<comment>Options:</comment>';
 100              foreach ($definition->getOptions() as $option) {
 101                  $messages[] = $this->describeInputOption($option, array('name_width' => $nameWidth));
 102              }
 103              $messages[] = '';
 104          }
 105  
 106          $output = implode("\n", $messages);
 107  
 108          return isset($options['raw_text']) && $options['raw_text'] ? strip_tags($output) : $output;
 109      }
 110  
 111      /**
 112       * {@inheritdoc}
 113       */
 114      protected function describeCommand(Command $command, array $options = array())
 115      {
 116          $command->getSynopsis();
 117          $command->mergeApplicationDefinition(false);
 118          $messages = array('<comment>Usage:</comment>', ' '.$command->getSynopsis(), '');
 119  
 120          if ($command->getAliases()) {
 121              $messages[] = '<comment>Aliases:</comment> <info>'.implode(', ', $command->getAliases()).'</info>';
 122          }
 123  
 124          $messages[] = $this->describeInputDefinition($command->getNativeDefinition());
 125  
 126          if ($help = $command->getProcessedHelp()) {
 127              $messages[] = '<comment>Help:</comment>';
 128              $messages[] = ' '.str_replace("\n", "\n ", $help)."\n";
 129          }
 130  
 131          $output = implode("\n", $messages);
 132  
 133          return isset($options['raw_text']) && $options['raw_text'] ? strip_tags($output) : $output;
 134      }
 135  
 136      /**
 137       * {@inheritdoc}
 138       */
 139      protected function describeApplication(Application $application, array $options = array())
 140      {
 141          $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
 142          $description = new ApplicationDescription($application, $describedNamespace);
 143          $messages = array();
 144  
 145          if (isset($options['raw_text']) && $options['raw_text']) {
 146              $width = $this->getColumnWidth($description->getCommands());
 147  
 148              foreach ($description->getCommands() as $command) {
 149                  $messages[] = sprintf("%-{$width}s %s", $command->getName(), $command->getDescription());
 150              }
 151          } else {
 152              $width = $this->getColumnWidth($description->getCommands());
 153  
 154              $messages[] = $application->getHelp();
 155              $messages[] = '';
 156  
 157              if ($describedNamespace) {
 158                  $messages[] = sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace);
 159              } else {
 160                  $messages[] = '<comment>Available commands:</comment>';
 161              }
 162  
 163              // add commands by namespace
 164              foreach ($description->getNamespaces() as $namespace) {
 165                  if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
 166                      $messages[] = '<comment>'.$namespace['id'].'</comment>';
 167                  }
 168  
 169                  foreach ($namespace['commands'] as $name) {
 170                      $messages[] = sprintf("  <info>%-{$width}s</info> %s", $name, $description->getCommand($name)->getDescription());
 171                  }
 172              }
 173          }
 174  
 175          $output = implode("\n", $messages);
 176  
 177          return isset($options['raw_text']) && $options['raw_text'] ? strip_tags($output) : $output;
 178      }
 179  
 180      /**
 181       * Formats input option/argument default value.
 182       *
 183       * @param mixed $default
 184       *
 185       * @return string
 186       */
 187      private function formatDefaultValue($default)
 188      {
 189          if (PHP_VERSION_ID < 50400) {
 190              return str_replace(array('\/', '\\\\'), array('/', '\\'), json_encode($default));
 191          }
 192  
 193          return str_replace('\\\\', '\\', json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
 194      }
 195  
 196      /**
 197       * @param Command[] $commands
 198       *
 199       * @return int
 200       */
 201      private function getColumnWidth(array $commands)
 202      {
 203          $width = 0;
 204          foreach ($commands as $command) {
 205              $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
 206          }
 207  
 208          return $width + 2;
 209      }
 210  }


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