You are here

class JsonDescriptor in Zircon Profile 8

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

JSON descriptor.

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

@internal

Hierarchy

Expanded class hierarchy of JsonDescriptor

2 files declare their use of JsonDescriptor
DescriptorHelper.php in vendor/symfony/console/Helper/DescriptorHelper.php
JsonDescriptorTest.php in vendor/symfony/console/Tests/Descriptor/JsonDescriptorTest.php

File

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

Namespace

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

  /**
   * {@inheritdoc}
   */
  protected function describeInputArgument(InputArgument $argument, array $options = array()) {
    $this
      ->writeData($this
      ->getInputArgumentData($argument), $options);
  }

  /**
   * {@inheritdoc}
   */
  protected function describeInputOption(InputOption $option, array $options = array()) {
    $this
      ->writeData($this
      ->getInputOptionData($option), $options);
  }

  /**
   * {@inheritdoc}
   */
  protected function describeInputDefinition(InputDefinition $definition, array $options = array()) {
    $this
      ->writeData($this
      ->getInputDefinitionData($definition), $options);
  }

  /**
   * {@inheritdoc}
   */
  protected function describeCommand(Command $command, array $options = array()) {
    $this
      ->writeData($this
      ->getCommandData($command), $options);
  }

  /**
   * {@inheritdoc}
   */
  protected function describeApplication(Application $application, array $options = array()) {
    $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
    $description = new ApplicationDescription($application, $describedNamespace);
    $commands = array();
    foreach ($description
      ->getCommands() as $command) {
      $commands[] = $this
        ->getCommandData($command);
    }
    $data = $describedNamespace ? array(
      'commands' => $commands,
      'namespace' => $describedNamespace,
    ) : array(
      'commands' => $commands,
      'namespaces' => array_values($description
        ->getNamespaces()),
    );
    $this
      ->writeData($data, $options);
  }

  /**
   * Writes data as json.
   *
   * @param array $data
   * @param array $options
   *
   * @return array|string
   */
  private function writeData(array $data, array $options) {
    $this
      ->write(json_encode($data, isset($options['json_encoding']) ? $options['json_encoding'] : 0));
  }

  /**
   * @param InputArgument $argument
   *
   * @return array
   */
  private function getInputArgumentData(InputArgument $argument) {
    return array(
      'name' => $argument
        ->getName(),
      'is_required' => $argument
        ->isRequired(),
      'is_array' => $argument
        ->isArray(),
      'description' => preg_replace('/\\s*[\\r\\n]\\s*/', ' ', $argument
        ->getDescription()),
      'default' => $argument
        ->getDefault(),
    );
  }

  /**
   * @param InputOption $option
   *
   * @return array
   */
  private function getInputOptionData(InputOption $option) {
    return array(
      'name' => '--' . $option
        ->getName(),
      'shortcut' => $option
        ->getShortcut() ? '-' . implode('|-', explode('|', $option
        ->getShortcut())) : '',
      'accept_value' => $option
        ->acceptValue(),
      'is_value_required' => $option
        ->isValueRequired(),
      'is_multiple' => $option
        ->isArray(),
      'description' => preg_replace('/\\s*[\\r\\n]\\s*/', ' ', $option
        ->getDescription()),
      'default' => $option
        ->getDefault(),
    );
  }

  /**
   * @param InputDefinition $definition
   *
   * @return array
   */
  private function getInputDefinitionData(InputDefinition $definition) {
    $inputArguments = array();
    foreach ($definition
      ->getArguments() as $name => $argument) {
      $inputArguments[$name] = $this
        ->getInputArgumentData($argument);
    }
    $inputOptions = array();
    foreach ($definition
      ->getOptions() as $name => $option) {
      $inputOptions[$name] = $this
        ->getInputOptionData($option);
    }
    return array(
      'arguments' => $inputArguments,
      'options' => $inputOptions,
    );
  }

  /**
   * @param Command $command
   *
   * @return array
   */
  private function getCommandData(Command $command) {
    $command
      ->getSynopsis();
    $command
      ->mergeApplicationDefinition(false);
    return array(
      'name' => $command
        ->getName(),
      'usage' => array_merge(array(
        $command
          ->getSynopsis(),
      ), $command
        ->getUsages(), $command
        ->getAliases()),
      'description' => $command
        ->getDescription(),
      'help' => $command
        ->getProcessedHelp(),
      'definition' => $this
        ->getInputDefinitionData($command
        ->getNativeDefinition()),
    );
  }

}

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.
JsonDescriptor::describeApplication protected function Describes an Application instance. Overrides Descriptor::describeApplication
JsonDescriptor::describeCommand protected function Describes a Command instance. Overrides Descriptor::describeCommand
JsonDescriptor::describeInputArgument protected function Describes an InputArgument instance. Overrides Descriptor::describeInputArgument
JsonDescriptor::describeInputDefinition protected function Describes an InputDefinition instance. Overrides Descriptor::describeInputDefinition
JsonDescriptor::describeInputOption protected function Describes an InputOption instance. Overrides Descriptor::describeInputOption
JsonDescriptor::getCommandData private function
JsonDescriptor::getInputArgumentData private function
JsonDescriptor::getInputDefinitionData private function
JsonDescriptor::getInputOptionData private function
JsonDescriptor::writeData private function Writes data as json.