4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
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;
23 * This class adds helper method to describe objects in various formats.
25 * @author Jean-François Simon <contact@jfsimon.fr>
27 class DescriptorHelper extends Helper
30 * @var DescriptorInterface[]
32 private $descriptors = array();
37 public function __construct()
40 ->register('txt', new TextDescriptor())
41 ->register('xml', new XmlDescriptor())
42 ->register('json', new JsonDescriptor())
43 ->register('md', new MarkdownDescriptor())
48 * Describes an object if supported.
50 * Available options are:
51 * * format: string, the output format name
52 * * raw_text: boolean, sets output type as raw
54 * @param OutputInterface $output
55 * @param object $object
56 * @param array $options
58 * @throws InvalidArgumentException when the given format is not supported
60 public function describe(OutputInterface $output, $object, array $options = array())
62 $options = array_merge(array(
67 if (!isset($this->descriptors[$options['format']])) {
68 throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
71 $descriptor = $this->descriptors[$options['format']];
72 $descriptor->describe($output, $object, $options);
76 * Registers a descriptor.
78 * @param string $format
79 * @param DescriptorInterface $descriptor
83 public function register($format, DescriptorInterface $descriptor)
85 $this->descriptors[$format] = $descriptor;
93 public function getName()