123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace Symfony\Component\Console\Helper;
- use Symfony\Component\Console\Descriptor\DescriptorInterface;
- use Symfony\Component\Console\Descriptor\JsonDescriptor;
- use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
- use Symfony\Component\Console\Descriptor\ReStructuredTextDescriptor;
- use Symfony\Component\Console\Descriptor\TextDescriptor;
- use Symfony\Component\Console\Descriptor\XmlDescriptor;
- use Symfony\Component\Console\Exception\InvalidArgumentException;
- use Symfony\Component\Console\Output\OutputInterface;
- class DescriptorHelper extends Helper
- {
-
- private array $descriptors = [];
- public function __construct()
- {
- $this
- ->register('txt', new TextDescriptor())
- ->register('xml', new XmlDescriptor())
- ->register('json', new JsonDescriptor())
- ->register('md', new MarkdownDescriptor())
- ->register('rst', new ReStructuredTextDescriptor())
- ;
- }
-
- public function describe(OutputInterface $output, ?object $object, array $options = [])
- {
- $options = array_merge([
- 'raw_text' => false,
- 'format' => 'txt',
- ], $options);
- if (!isset($this->descriptors[$options['format']])) {
- throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
- }
- $descriptor = $this->descriptors[$options['format']];
- $descriptor->describe($output, $object, $options);
- }
-
- public function register(string $format, DescriptorInterface $descriptor): static
- {
- $this->descriptors[$format] = $descriptor;
- return $this;
- }
- public function getName(): string
- {
- return 'descriptor';
- }
- public function getFormats(): array
- {
- return array_keys($this->descriptors);
- }
- }
|