Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Helper; Chris@0: Chris@0: use Symfony\Component\Console\Descriptor\DescriptorInterface; Chris@0: use Symfony\Component\Console\Descriptor\JsonDescriptor; Chris@0: use Symfony\Component\Console\Descriptor\MarkdownDescriptor; Chris@0: use Symfony\Component\Console\Descriptor\TextDescriptor; Chris@0: use Symfony\Component\Console\Descriptor\XmlDescriptor; Chris@17: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\Console\Output\OutputInterface; Chris@0: Chris@0: /** Chris@0: * This class adds helper method to describe objects in various formats. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: */ Chris@0: class DescriptorHelper extends Helper Chris@0: { Chris@0: /** Chris@0: * @var DescriptorInterface[] Chris@0: */ Chris@17: private $descriptors = []; Chris@0: Chris@0: public function __construct() Chris@0: { Chris@0: $this Chris@0: ->register('txt', new TextDescriptor()) Chris@0: ->register('xml', new XmlDescriptor()) Chris@0: ->register('json', new JsonDescriptor()) Chris@0: ->register('md', new MarkdownDescriptor()) Chris@0: ; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Describes an object if supported. Chris@0: * Chris@0: * Available options are: Chris@0: * * format: string, the output format name Chris@0: * * raw_text: boolean, sets output type as raw Chris@0: * Chris@0: * @param OutputInterface $output Chris@0: * @param object $object Chris@0: * @param array $options Chris@0: * Chris@0: * @throws InvalidArgumentException when the given format is not supported Chris@0: */ Chris@17: public function describe(OutputInterface $output, $object, array $options = []) Chris@0: { Chris@17: $options = array_merge([ Chris@0: 'raw_text' => false, Chris@0: 'format' => 'txt', Chris@17: ], $options); Chris@0: Chris@0: if (!isset($this->descriptors[$options['format']])) { Chris@0: throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format'])); Chris@0: } Chris@0: Chris@0: $descriptor = $this->descriptors[$options['format']]; Chris@0: $descriptor->describe($output, $object, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers a descriptor. Chris@0: * Chris@0: * @param string $format Chris@0: * @param DescriptorInterface $descriptor Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function register($format, DescriptorInterface $descriptor) Chris@0: { Chris@0: $this->descriptors[$format] = $descriptor; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return 'descriptor'; Chris@0: } Chris@0: }