You are here

class TextDescriptor in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Descriptor/TextDescriptor.php \Symfony\Component\Console\Descriptor\TextDescriptor

Text descriptor.

@author Jean-François Simon <contact@jfsimon.fr>

@internal

Hierarchy

Expanded class hierarchy of TextDescriptor

5 files declare their use of TextDescriptor
Application.php in vendor/symfony/console/Application.php
Command.php in vendor/symfony/console/Command/Command.php
DescriptorHelper.php in vendor/symfony/console/Helper/DescriptorHelper.php
InputDefinition.php in vendor/symfony/console/Input/InputDefinition.php
TextDescriptorTest.php in vendor/symfony/console/Tests/Descriptor/TextDescriptorTest.php

File

vendor/symfony/console/Descriptor/TextDescriptor.php, line 27

Namespace

Symfony\Component\Console\Descriptor
View source
class TextDescriptor extends Descriptor {

  /**
   * {@inheritdoc}
   */
  protected function describeInputArgument(InputArgument $argument, array $options = array()) {
    if (null !== $argument
      ->getDefault() && (!is_array($argument
      ->getDefault()) || count($argument
      ->getDefault()))) {
      $default = sprintf('<comment> [default: %s]</comment>', $this
        ->formatDefaultValue($argument
        ->getDefault()));
    }
    else {
      $default = '';
    }
    $totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument
      ->getName());
    $spacingWidth = $totalWidth - strlen($argument
      ->getName()) + 2;
    $this
      ->writeText(sprintf('  <info>%s</info>%s%s%s', $argument
      ->getName(), str_repeat(' ', $spacingWidth), preg_replace('/\\s*[\\r\\n]\\s*/', "\n" . str_repeat(' ', $totalWidth + 17), $argument
      ->getDescription()), $default), $options);
  }

  /**
   * {@inheritdoc}
   */
  protected function describeInputOption(InputOption $option, array $options = array()) {
    if ($option
      ->acceptValue() && null !== $option
      ->getDefault() && (!is_array($option
      ->getDefault()) || count($option
      ->getDefault()))) {
      $default = sprintf('<comment> [default: %s]</comment>', $this
        ->formatDefaultValue($option
        ->getDefault()));
    }
    else {
      $default = '';
    }
    $value = '';
    if ($option
      ->acceptValue()) {
      $value = '=' . strtoupper($option
        ->getName());
      if ($option
        ->isValueOptional()) {
        $value = '[' . $value . ']';
      }
    }
    $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this
      ->calculateTotalWidthForOptions(array(
      $option,
    ));
    $synopsis = sprintf('%s%s', $option
      ->getShortcut() ? sprintf('-%s, ', $option
      ->getShortcut()) : '    ', sprintf('--%s%s', $option
      ->getName(), $value));
    $spacingWidth = $totalWidth - strlen($synopsis) + 2;
    $this
      ->writeText(sprintf('  <info>%s</info>%s%s%s%s', $synopsis, str_repeat(' ', $spacingWidth), preg_replace('/\\s*[\\r\\n]\\s*/', "\n" . str_repeat(' ', $totalWidth + 17), $option
      ->getDescription()), $default, $option
      ->isArray() ? '<comment> (multiple values allowed)</comment>' : ''), $options);
  }

  /**
   * {@inheritdoc}
   */
  protected function describeInputDefinition(InputDefinition $definition, array $options = array()) {
    $totalWidth = $this
      ->calculateTotalWidthForOptions($definition
      ->getOptions());
    foreach ($definition
      ->getArguments() as $argument) {
      $totalWidth = max($totalWidth, strlen($argument
        ->getName()));
    }
    if ($definition
      ->getArguments()) {
      $this
        ->writeText('<comment>Arguments:</comment>', $options);
      $this
        ->writeText("\n");
      foreach ($definition
        ->getArguments() as $argument) {
        $this
          ->describeInputArgument($argument, array_merge($options, array(
          'total_width' => $totalWidth,
        )));
        $this
          ->writeText("\n");
      }
    }
    if ($definition
      ->getArguments() && $definition
      ->getOptions()) {
      $this
        ->writeText("\n");
    }
    if ($definition
      ->getOptions()) {
      $laterOptions = array();
      $this
        ->writeText('<comment>Options:</comment>', $options);
      foreach ($definition
        ->getOptions() as $option) {
        if (strlen($option
          ->getShortcut()) > 1) {
          $laterOptions[] = $option;
          continue;
        }
        $this
          ->writeText("\n");
        $this
          ->describeInputOption($option, array_merge($options, array(
          'total_width' => $totalWidth,
        )));
      }
      foreach ($laterOptions as $option) {
        $this
          ->writeText("\n");
        $this
          ->describeInputOption($option, array_merge($options, array(
          'total_width' => $totalWidth,
        )));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function describeCommand(Command $command, array $options = array()) {
    $command
      ->getSynopsis(true);
    $command
      ->getSynopsis(false);
    $command
      ->mergeApplicationDefinition(false);
    $this
      ->writeText('<comment>Usage:</comment>', $options);
    foreach (array_merge(array(
      $command
        ->getSynopsis(true),
    ), $command
      ->getAliases(), $command
      ->getUsages()) as $usage) {
      $this
        ->writeText("\n");
      $this
        ->writeText('  ' . $usage, $options);
    }
    $this
      ->writeText("\n");
    $definition = $command
      ->getNativeDefinition();
    if ($definition
      ->getOptions() || $definition
      ->getArguments()) {
      $this
        ->writeText("\n");
      $this
        ->describeInputDefinition($definition, $options);
      $this
        ->writeText("\n");
    }
    if ($help = $command
      ->getProcessedHelp()) {
      $this
        ->writeText("\n");
      $this
        ->writeText('<comment>Help:</comment>', $options);
      $this
        ->writeText("\n");
      $this
        ->writeText(' ' . str_replace("\n", "\n ", $help), $options);
      $this
        ->writeText("\n");
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function describeApplication(Application $application, array $options = array()) {
    $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
    $description = new ApplicationDescription($application, $describedNamespace);
    if (isset($options['raw_text']) && $options['raw_text']) {
      $width = $this
        ->getColumnWidth($description
        ->getCommands());
      foreach ($description
        ->getCommands() as $command) {
        $this
          ->writeText(sprintf("%-{$width}s %s", $command
          ->getName(), $command
          ->getDescription()), $options);
        $this
          ->writeText("\n");
      }
    }
    else {
      if ('' != ($help = $application
        ->getHelp())) {
        $this
          ->writeText("{$help}\n\n", $options);
      }
      $this
        ->writeText("<comment>Usage:</comment>\n", $options);
      $this
        ->writeText("  command [options] [arguments]\n\n", $options);
      $this
        ->describeInputDefinition(new InputDefinition($application
        ->getDefinition()
        ->getOptions()), $options);
      $this
        ->writeText("\n");
      $this
        ->writeText("\n");
      $width = $this
        ->getColumnWidth($description
        ->getCommands());
      if ($describedNamespace) {
        $this
          ->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
      }
      else {
        $this
          ->writeText('<comment>Available commands:</comment>', $options);
      }

      // add commands by namespace
      foreach ($description
        ->getNamespaces() as $namespace) {
        if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
          $this
            ->writeText("\n");
          $this
            ->writeText(' <comment>' . $namespace['id'] . '</comment>', $options);
        }
        foreach ($namespace['commands'] as $name) {
          $this
            ->writeText("\n");
          $spacingWidth = $width - strlen($name);
          $this
            ->writeText(sprintf('  <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $description
            ->getCommand($name)
            ->getDescription()), $options);
        }
      }
      $this
        ->writeText("\n");
    }
  }

  /**
   * {@inheritdoc}
   */
  private function writeText($content, array $options = array()) {
    $this
      ->write(isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content, isset($options['raw_output']) ? !$options['raw_output'] : true);
  }

  /**
   * Formats input option/argument default value.
   *
   * @param mixed $default
   *
   * @return string
   */
  private function formatDefaultValue($default) {
    if (PHP_VERSION_ID < 50400) {
      return str_replace('\\/', '/', json_encode($default));
    }
    return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  }

  /**
   * @param Command[] $commands
   *
   * @return int
   */
  private function getColumnWidth(array $commands) {
    $widths = array();
    foreach ($commands as $command) {
      $widths[] = strlen($command
        ->getName());
      foreach ($command
        ->getAliases() as $alias) {
        $widths[] = strlen($alias);
      }
    }
    return max($widths) + 2;
  }

  /**
   * @param InputOption[] $options
   *
   * @return int
   */
  private function calculateTotalWidthForOptions($options) {
    $totalWidth = 0;
    foreach ($options as $option) {

      // "-" + shortcut + ", --" + name
      $nameLength = 1 + max(strlen($option
        ->getShortcut()), 1) + 4 + strlen($option
        ->getName());
      if ($option
        ->acceptValue()) {
        $valueLength = 1 + strlen($option
          ->getName());

        // = + value
        $valueLength += $option
          ->isValueOptional() ? 2 : 0;

        // [ + ]
        $nameLength += $valueLength;
      }
      $totalWidth = max($totalWidth, $nameLength);
    }
    return $totalWidth;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Descriptor::$output private property
Descriptor::describe public function Describes an InputArgument instance. Overrides DescriptorInterface::describe
Descriptor::write protected function Writes content to output.
TextDescriptor::calculateTotalWidthForOptions private function
TextDescriptor::describeApplication protected function Describes an Application instance. Overrides Descriptor::describeApplication
TextDescriptor::describeCommand protected function Describes a Command instance. Overrides Descriptor::describeCommand
TextDescriptor::describeInputArgument protected function Describes an InputArgument instance. Overrides Descriptor::describeInputArgument
TextDescriptor::describeInputDefinition protected function Describes an InputDefinition instance. Overrides Descriptor::describeInputDefinition
TextDescriptor::describeInputOption protected function Describes an InputOption instance. Overrides Descriptor::describeInputOption
TextDescriptor::formatDefaultValue private function Formats input option/argument default value.
TextDescriptor::getColumnWidth private function
TextDescriptor::writeText private function