annotate vendor/symfony/console/Helper/DescriptorHelper.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
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@0 19 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 20 use Symfony\Component\Console\Exception\InvalidArgumentException;
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@0 32 private $descriptors = array();
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Constructor.
Chris@0 36 */
Chris@0 37 public function __construct()
Chris@0 38 {
Chris@0 39 $this
Chris@0 40 ->register('txt', new TextDescriptor())
Chris@0 41 ->register('xml', new XmlDescriptor())
Chris@0 42 ->register('json', new JsonDescriptor())
Chris@0 43 ->register('md', new MarkdownDescriptor())
Chris@0 44 ;
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Describes an object if supported.
Chris@0 49 *
Chris@0 50 * Available options are:
Chris@0 51 * * format: string, the output format name
Chris@0 52 * * raw_text: boolean, sets output type as raw
Chris@0 53 *
Chris@0 54 * @param OutputInterface $output
Chris@0 55 * @param object $object
Chris@0 56 * @param array $options
Chris@0 57 *
Chris@0 58 * @throws InvalidArgumentException when the given format is not supported
Chris@0 59 */
Chris@0 60 public function describe(OutputInterface $output, $object, array $options = array())
Chris@0 61 {
Chris@0 62 $options = array_merge(array(
Chris@0 63 'raw_text' => false,
Chris@0 64 'format' => 'txt',
Chris@0 65 ), $options);
Chris@0 66
Chris@0 67 if (!isset($this->descriptors[$options['format']])) {
Chris@0 68 throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
Chris@0 69 }
Chris@0 70
Chris@0 71 $descriptor = $this->descriptors[$options['format']];
Chris@0 72 $descriptor->describe($output, $object, $options);
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * Registers a descriptor.
Chris@0 77 *
Chris@0 78 * @param string $format
Chris@0 79 * @param DescriptorInterface $descriptor
Chris@0 80 *
Chris@0 81 * @return $this
Chris@0 82 */
Chris@0 83 public function register($format, DescriptorInterface $descriptor)
Chris@0 84 {
Chris@0 85 $this->descriptors[$format] = $descriptor;
Chris@0 86
Chris@0 87 return $this;
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * {@inheritdoc}
Chris@0 92 */
Chris@0 93 public function getName()
Chris@0 94 {
Chris@0 95 return 'descriptor';
Chris@0 96 }
Chris@0 97 }