Drupal investigation

DescriptorHelper.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Descriptor\DescriptorInterface;
  12. use Symfony\Component\Console\Descriptor\JsonDescriptor;
  13. use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
  14. use Symfony\Component\Console\Descriptor\TextDescriptor;
  15. use Symfony\Component\Console\Descriptor\XmlDescriptor;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Exception\InvalidArgumentException;
  18. /**
  19. * This class adds helper method to describe objects in various formats.
  20. *
  21. * @author Jean-François Simon <contact@jfsimon.fr>
  22. */
  23. class DescriptorHelper extends Helper
  24. {
  25. /**
  26. * @var DescriptorInterface[]
  27. */
  28. private $descriptors = array();
  29. /**
  30. * Constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this
  35. ->register('txt', new TextDescriptor())
  36. ->register('xml', new XmlDescriptor())
  37. ->register('json', new JsonDescriptor())
  38. ->register('md', new MarkdownDescriptor())
  39. ;
  40. }
  41. /**
  42. * Describes an object if supported.
  43. *
  44. * Available options are:
  45. * * format: string, the output format name
  46. * * raw_text: boolean, sets output type as raw
  47. *
  48. * @param OutputInterface $output
  49. * @param object $object
  50. * @param array $options
  51. *
  52. * @throws InvalidArgumentException when the given format is not supported
  53. */
  54. public function describe(OutputInterface $output, $object, array $options = array())
  55. {
  56. $options = array_merge(array(
  57. 'raw_text' => false,
  58. 'format' => 'txt',
  59. ), $options);
  60. if (!isset($this->descriptors[$options['format']])) {
  61. throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $options['format']));
  62. }
  63. $descriptor = $this->descriptors[$options['format']];
  64. $descriptor->describe($output, $object, $options);
  65. }
  66. /**
  67. * Registers a descriptor.
  68. *
  69. * @param string $format
  70. * @param DescriptorInterface $descriptor
  71. *
  72. * @return $this
  73. */
  74. public function register($format, DescriptorInterface $descriptor)
  75. {
  76. $this->descriptors[$format] = $descriptor;
  77. return $this;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getName()
  83. {
  84. return 'descriptor';
  85. }
  86. }