3 * This file is part of the Drupal Console.
5 namespace Drupal\Console\Core\Helper;
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;
16 * This class adds helper method to describe objects in various formats.
18 * @author Jean-François Simon <contact@jfsimon.fr>
20 class DescriptorHelper extends BaseHelper
23 * @var DescriptorInterface[]
25 private $descriptors = [];
29 public function __construct()
32 ->register('txt', new TextDescriptor())
33 ->register('xml', new XmlDescriptor())
34 ->register('json', new JsonDescriptor())
35 ->register('md', new MarkdownDescriptor());
38 * Describes an object if supported.
40 * Available options are:
41 * * format: string, the output format name
42 * * raw_text: boolean, sets output type as raw
44 * @param OutputInterface $output
45 * @param object $object
46 * @param array $options
48 * @throws \InvalidArgumentException when the given format is not supported
50 public function describe(OutputInterface $output, $object, array $options = [])
52 $options = array_merge(
58 if (!isset($this->descriptors[$options['format']])) {
59 throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
61 $descriptor = $this->descriptors[$options['format']];
62 $descriptor->describe($output, $object, $options);
65 * Registers a descriptor.
67 * @param string $format
68 * @param DescriptorInterface $descriptor
70 * @return DescriptorHelper
72 public function register($format, DescriptorInterface $descriptor)
74 $this->descriptors[$format] = $descriptor;
80 public function getName()