annotate vendor/symfony/console/Helper/DescriptorHelper.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Helper;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Descriptor\DescriptorInterface;
Chris@0 15 use Symfony\Component\Console\Descriptor\JsonDescriptor;
Chris@0 16 use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
Chris@0 17 use Symfony\Component\Console\Descriptor\TextDescriptor;
Chris@0 18 use Symfony\Component\Console\Descriptor\XmlDescriptor;
Chris@17 19 use Symfony\Component\Console\Exception\InvalidArgumentException;
Chris@0 20 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * This class adds helper method to describe objects in various formats.
Chris@0 24 *
Chris@0 25 * @author Jean-François Simon <contact@jfsimon.fr>
Chris@0 26 */
Chris@0 27 class DescriptorHelper extends Helper
Chris@0 28 {
Chris@0 29 /**
Chris@0 30 * @var DescriptorInterface[]
Chris@0 31 */
Chris@17 32 private $descriptors = [];
Chris@0 33
Chris@0 34 public function __construct()
Chris@0 35 {
Chris@0 36 $this
Chris@0 37 ->register('txt', new TextDescriptor())
Chris@0 38 ->register('xml', new XmlDescriptor())
Chris@0 39 ->register('json', new JsonDescriptor())
Chris@0 40 ->register('md', new MarkdownDescriptor())
Chris@0 41 ;
Chris@0 42 }
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Describes an object if supported.
Chris@0 46 *
Chris@0 47 * Available options are:
Chris@0 48 * * format: string, the output format name
Chris@0 49 * * raw_text: boolean, sets output type as raw
Chris@0 50 *
Chris@0 51 * @param OutputInterface $output
Chris@0 52 * @param object $object
Chris@0 53 * @param array $options
Chris@0 54 *
Chris@0 55 * @throws InvalidArgumentException when the given format is not supported
Chris@0 56 */
Chris@17 57 public function describe(OutputInterface $output, $object, array $options = [])
Chris@0 58 {
Chris@17 59 $options = array_merge([
Chris@0 60 'raw_text' => false,
Chris@0 61 'format' => 'txt',
Chris@17 62 ], $options);
Chris@0 63
Chris@0 64 if (!isset($this->descriptors[$options['format']])) {
Chris@0 65 throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
Chris@0 66 }
Chris@0 67
Chris@0 68 $descriptor = $this->descriptors[$options['format']];
Chris@0 69 $descriptor->describe($output, $object, $options);
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * Registers a descriptor.
Chris@0 74 *
Chris@0 75 * @param string $format
Chris@0 76 * @param DescriptorInterface $descriptor
Chris@0 77 *
Chris@0 78 * @return $this
Chris@0 79 */
Chris@0 80 public function register($format, DescriptorInterface $descriptor)
Chris@0 81 {
Chris@0 82 $this->descriptors[$format] = $descriptor;
Chris@0 83
Chris@0 84 return $this;
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * {@inheritdoc}
Chris@0 89 */
Chris@0 90 public function getName()
Chris@0 91 {
Chris@0 92 return 'descriptor';
Chris@0 93 }
Chris@0 94 }