Descriptor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Descriptor;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Exception\InvalidArgumentException;
  18. /**
  19. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  20. *
  21. * @internal
  22. */
  23. abstract class Descriptor implements DescriptorInterface
  24. {
  25. /**
  26. * @var OutputInterface
  27. */
  28. protected $output;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function describe(OutputInterface $output, $object, array $options = array())
  33. {
  34. $this->output = $output;
  35. switch (true) {
  36. case $object instanceof InputArgument:
  37. $this->describeInputArgument($object, $options);
  38. break;
  39. case $object instanceof InputOption:
  40. $this->describeInputOption($object, $options);
  41. break;
  42. case $object instanceof InputDefinition:
  43. $this->describeInputDefinition($object, $options);
  44. break;
  45. case $object instanceof Command:
  46. $this->describeCommand($object, $options);
  47. break;
  48. case $object instanceof Application:
  49. $this->describeApplication($object, $options);
  50. break;
  51. default:
  52. throw new InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
  53. }
  54. }
  55. /**
  56. * Writes content to output.
  57. *
  58. * @param string $content
  59. * @param bool $decorated
  60. */
  61. protected function write($content, $decorated = false)
  62. {
  63. $this->output->write($content, false, $decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW);
  64. }
  65. /**
  66. * Describes an InputArgument instance.
  67. *
  68. * @param InputArgument $argument
  69. * @param array $options
  70. *
  71. * @return string|mixed
  72. */
  73. abstract protected function describeInputArgument(InputArgument $argument, array $options = array());
  74. /**
  75. * Describes an InputOption instance.
  76. *
  77. * @param InputOption $option
  78. * @param array $options
  79. *
  80. * @return string|mixed
  81. */
  82. abstract protected function describeInputOption(InputOption $option, array $options = array());
  83. /**
  84. * Describes an InputDefinition instance.
  85. *
  86. * @param InputDefinition $definition
  87. * @param array $options
  88. *
  89. * @return string|mixed
  90. */
  91. abstract protected function describeInputDefinition(InputDefinition $definition, array $options = array());
  92. /**
  93. * Describes a Command instance.
  94. *
  95. * @param Command $command
  96. * @param array $options
  97. *
  98. * @return string|mixed
  99. */
  100. abstract protected function describeCommand(Command $command, array $options = array());
  101. /**
  102. * Describes an Application instance.
  103. *
  104. * @param Application $application
  105. * @param array $options
  106. *
  107. * @return string|mixed
  108. */
  109. abstract protected function describeApplication(Application $application, array $options = array());
  110. }