Descriptor.php 3.4 KB

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