Descriptor.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\Exception\InvalidArgumentException;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputDefinition;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  20. *
  21. * @internal
  22. */
  23. abstract class Descriptor implements DescriptorInterface
  24. {
  25. protected OutputInterface $output;
  26. public function describe(OutputInterface $output, object $object, array $options = []): void
  27. {
  28. $this->output = $output;
  29. match (true) {
  30. $object instanceof InputArgument => $this->describeInputArgument($object, $options),
  31. $object instanceof InputOption => $this->describeInputOption($object, $options),
  32. $object instanceof InputDefinition => $this->describeInputDefinition($object, $options),
  33. $object instanceof Command => $this->describeCommand($object, $options),
  34. $object instanceof Application => $this->describeApplication($object, $options),
  35. default => throw new InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_debug_type($object))),
  36. };
  37. }
  38. protected function write(string $content, bool $decorated = false): void
  39. {
  40. $this->output->write($content, false, $decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW);
  41. }
  42. /**
  43. * Describes an InputArgument instance.
  44. */
  45. abstract protected function describeInputArgument(InputArgument $argument, array $options = []): void;
  46. /**
  47. * Describes an InputOption instance.
  48. */
  49. abstract protected function describeInputOption(InputOption $option, array $options = []): void;
  50. /**
  51. * Describes an InputDefinition instance.
  52. */
  53. abstract protected function describeInputDefinition(InputDefinition $definition, array $options = []): void;
  54. /**
  55. * Describes a Command instance.
  56. */
  57. abstract protected function describeCommand(Command $command, array $options = []): void;
  58. /**
  59. * Describes an Application instance.
  60. */
  61. abstract protected function describeApplication(Application $application, array $options = []): void;
  62. }