comparison vendor/symfony/console/Helper/DescriptorHelper.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 use Symfony\Component\Console\Exception\InvalidArgumentException;
21
22 /**
23 * This class adds helper method to describe objects in various formats.
24 *
25 * @author Jean-François Simon <contact@jfsimon.fr>
26 */
27 class DescriptorHelper extends Helper
28 {
29 /**
30 * @var DescriptorInterface[]
31 */
32 private $descriptors = array();
33
34 /**
35 * Constructor.
36 */
37 public function __construct()
38 {
39 $this
40 ->register('txt', new TextDescriptor())
41 ->register('xml', new XmlDescriptor())
42 ->register('json', new JsonDescriptor())
43 ->register('md', new MarkdownDescriptor())
44 ;
45 }
46
47 /**
48 * Describes an object if supported.
49 *
50 * Available options are:
51 * * format: string, the output format name
52 * * raw_text: boolean, sets output type as raw
53 *
54 * @param OutputInterface $output
55 * @param object $object
56 * @param array $options
57 *
58 * @throws InvalidArgumentException when the given format is not supported
59 */
60 public function describe(OutputInterface $output, $object, array $options = array())
61 {
62 $options = array_merge(array(
63 'raw_text' => false,
64 'format' => 'txt',
65 ), $options);
66
67 if (!isset($this->descriptors[$options['format']])) {
68 throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
69 }
70
71 $descriptor = $this->descriptors[$options['format']];
72 $descriptor->describe($output, $object, $options);
73 }
74
75 /**
76 * Registers a descriptor.
77 *
78 * @param string $format
79 * @param DescriptorInterface $descriptor
80 *
81 * @return $this
82 */
83 public function register($format, DescriptorInterface $descriptor)
84 {
85 $this->descriptors[$format] = $descriptor;
86
87 return $this;
88 }
89
90 /**
91 * {@inheritdoc}
92 */
93 public function getName()
94 {
95 return 'descriptor';
96 }
97 }