40e7f0ceed2832e079e2ef67814e13e005451858
[yaffs-website] / vendor / drupal / console-core / src / Helper / DescriptorHelper.php
1 <?php
2 /*
3  * This file is part of the Drupal Console.
4  */
5 namespace Drupal\Console\Core\Helper;
6
7 use Symfony\Component\Console\Helper\Helper as BaseHelper;
8 use Symfony\Component\Console\Descriptor\DescriptorInterface;
9 use Symfony\Component\Console\Descriptor\JsonDescriptor;
10 use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
11 use Drupal\Console\Core\Descriptor\TextDescriptor;
12 use Symfony\Component\Console\Descriptor\XmlDescriptor;
13 use Symfony\Component\Console\Output\OutputInterface;
14
15 /**
16  * This class adds helper method to describe objects in various formats.
17  *
18  * @author Jean-François Simon <contact@jfsimon.fr>
19  */
20 class DescriptorHelper extends BaseHelper
21 {
22     /**
23      * @var DescriptorInterface[]
24      */
25     private $descriptors = array();
26     /**
27      * Constructor.
28      */
29     public function __construct()
30     {
31         $this
32             ->register('txt', new TextDescriptor())
33             ->register('xml', new XmlDescriptor())
34             ->register('json', new JsonDescriptor())
35             ->register('md', new MarkdownDescriptor());
36     }
37     /**
38      * Describes an object if supported.
39      *
40      * Available options are:
41      * * format: string, the output format name
42      * * raw_text: boolean, sets output type as raw
43      *
44      * @param OutputInterface $output
45      * @param object          $object
46      * @param array           $options
47      *
48      * @throws \InvalidArgumentException when the given format is not supported
49      */
50     public function describe(OutputInterface $output, $object, array $options = array())
51     {
52         $options = array_merge(
53             array(
54             'raw_text' => false,
55             'format' => 'txt',
56             ), $options
57         );
58         if (!isset($this->descriptors[$options['format']])) {
59             throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
60         }
61         $descriptor = $this->descriptors[$options['format']];
62         $descriptor->describe($output, $object, $options);
63     }
64     /**
65      * Registers a descriptor.
66      *
67      * @param string              $format
68      * @param DescriptorInterface $descriptor
69      *
70      * @return DescriptorHelper
71      */
72     public function register($format, DescriptorInterface $descriptor)
73     {
74         $this->descriptors[$format] = $descriptor;
75         return $this;
76     }
77     /**
78      * {@inheritdoc}
79      */
80     public function getName()
81     {
82         return 'descriptor';
83     }
84 }