[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/console/Symfony/Component/Console/Helper/ -> DescriptorHelper.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\Helper;
  13  
  14  use Symfony\Component\Console\Descriptor\DescriptorInterface;
  15  use Symfony\Component\Console\Descriptor\JsonDescriptor;
  16  use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
  17  use Symfony\Component\Console\Descriptor\TextDescriptor;
  18  use Symfony\Component\Console\Descriptor\XmlDescriptor;
  19  use Symfony\Component\Console\Output\OutputInterface;
  20  
  21  /**
  22   * This class adds helper method to describe objects in various formats.
  23   *
  24   * @author Jean-François Simon <contact@jfsimon.fr>
  25   */
  26  class DescriptorHelper extends Helper
  27  {
  28      /**
  29       * @var DescriptorInterface[]
  30       */
  31      private $descriptors = array();
  32  
  33      /**
  34       * Constructor.
  35       */
  36      public function __construct()
  37      {
  38          $this
  39              ->register('txt', new TextDescriptor())
  40              ->register('xml', new XmlDescriptor())
  41              ->register('json', new JsonDescriptor())
  42              ->register('md', new MarkdownDescriptor())
  43          ;
  44      }
  45  
  46      /**
  47       * Describes an object if supported.
  48       *
  49       * @param OutputInterface $output
  50       * @param object          $object
  51       * @param string|null     $format
  52       * @param bool            $raw
  53       * @param string|null     $namespace
  54       *
  55       * @throws \InvalidArgumentException when the given format is not supported
  56       */
  57      public function describe(OutputInterface $output, $object, $format = null, $raw = false, $namespace = null)
  58      {
  59          $options = array('raw_text' => $raw, 'format' => $format ?: 'txt', 'namespace' => $namespace);
  60          $type = !$raw && 'txt' === $options['format'] ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW;
  61  
  62          if (!isset($this->descriptors[$options['format']])) {
  63              throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
  64          }
  65  
  66          $descriptor = $this->descriptors[$options['format']];
  67  
  68          $output->writeln($descriptor->describe($object, $options), $type);
  69      }
  70  
  71      /**
  72       * Registers a descriptor.
  73       *
  74       * @param string              $format
  75       * @param DescriptorInterface $descriptor
  76       *
  77       * @return DescriptorHelper
  78       */
  79      public function register($format, DescriptorInterface $descriptor)
  80      {
  81          $this->descriptors[$format] = $descriptor;
  82  
  83          return $this;
  84      }
  85  
  86      /**
  87       * {@inheritdoc}
  88       */
  89      public function getName()
  90      {
  91          return 'descriptor';
  92      }
  93  }


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