diff options
Diffstat (limited to 'vendor/symfony/console/Helper/DescriptorHelper.php')
-rw-r--r-- | vendor/symfony/console/Helper/DescriptorHelper.php | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/vendor/symfony/console/Helper/DescriptorHelper.php b/vendor/symfony/console/Helper/DescriptorHelper.php new file mode 100644 index 0000000..300c7b1 --- /dev/null +++ b/vendor/symfony/console/Helper/DescriptorHelper.php | |||
@@ -0,0 +1,91 @@ | |||
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\ReStructuredTextDescriptor; | ||
18 | use Symfony\Component\Console\Descriptor\TextDescriptor; | ||
19 | use Symfony\Component\Console\Descriptor\XmlDescriptor; | ||
20 | use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
21 | use Symfony\Component\Console\Output\OutputInterface; | ||
22 | |||
23 | /** | ||
24 | * This class adds helper method to describe objects in various formats. | ||
25 | * | ||
26 | * @author Jean-François Simon <contact@jfsimon.fr> | ||
27 | */ | ||
28 | class DescriptorHelper extends Helper | ||
29 | { | ||
30 | /** | ||
31 | * @var DescriptorInterface[] | ||
32 | */ | ||
33 | private array $descriptors = []; | ||
34 | |||
35 | public function __construct() | ||
36 | { | ||
37 | $this | ||
38 | ->register('txt', new TextDescriptor()) | ||
39 | ->register('xml', new XmlDescriptor()) | ||
40 | ->register('json', new JsonDescriptor()) | ||
41 | ->register('md', new MarkdownDescriptor()) | ||
42 | ->register('rst', new ReStructuredTextDescriptor()) | ||
43 | ; | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * Describes an object if supported. | ||
48 | * | ||
49 | * Available options are: | ||
50 | * * format: string, the output format name | ||
51 | * * raw_text: boolean, sets output type as raw | ||
52 | * | ||
53 | * @throws InvalidArgumentException when the given format is not supported | ||
54 | */ | ||
55 | public function describe(OutputInterface $output, ?object $object, array $options = []): void | ||
56 | { | ||
57 | $options = array_merge([ | ||
58 | 'raw_text' => false, | ||
59 | 'format' => 'txt', | ||
60 | ], $options); | ||
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 | $descriptor->describe($output, $object, $options); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Registers a descriptor. | ||
72 | * | ||
73 | * @return $this | ||
74 | */ | ||
75 | public function register(string $format, DescriptorInterface $descriptor): static | ||
76 | { | ||
77 | $this->descriptors[$format] = $descriptor; | ||
78 | |||
79 | return $this; | ||
80 | } | ||
81 | |||
82 | public function getName(): string | ||
83 | { | ||
84 | return 'descriptor'; | ||
85 | } | ||
86 | |||
87 | public function getFormats(): array | ||
88 | { | ||
89 | return array_keys($this->descriptors); | ||
90 | } | ||
91 | } | ||