MapInput.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Attribute;
  11. use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Interaction\Interaction;
  15. /**
  16. * Maps a command input into an object (DTO).
  17. */
  18. #[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)]
  19. final class MapInput
  20. {
  21. /**
  22. * @var array<string, Argument|Option|self>
  23. */
  24. private array $definition = [];
  25. private \ReflectionClass $class;
  26. /**
  27. * @var list<Interact>
  28. */
  29. private array $interactiveAttributes = [];
  30. /**
  31. * @internal
  32. */
  33. public static function tryFrom(\ReflectionParameter|\ReflectionProperty $member): ?self
  34. {
  35. $reflection = new ReflectionMember($member);
  36. if (!$self = $reflection->getAttribute(self::class)) {
  37. return null;
  38. }
  39. $type = $reflection->getType();
  40. if (!$type instanceof \ReflectionNamedType) {
  41. throw new LogicException(\sprintf('The input %s "%s" must have a named type.', $reflection->getMemberName(), $member->name));
  42. }
  43. if (!class_exists($class = $type->getName())) {
  44. throw new LogicException(\sprintf('The input class "%s" does not exist.', $type->getName()));
  45. }
  46. $self->class = new \ReflectionClass($class);
  47. foreach ($self->class->getProperties() as $property) {
  48. if ($argument = Argument::tryFrom($property)) {
  49. $self->definition[$property->name] = $argument;
  50. } elseif ($option = Option::tryFrom($property)) {
  51. $self->definition[$property->name] = $option;
  52. } elseif ($input = self::tryFrom($property)) {
  53. $self->definition[$property->name] = $input;
  54. }
  55. if (isset($self->definition[$property->name]) && (!$property->isPublic() || $property->isStatic())) {
  56. throw new LogicException(\sprintf('The input property "%s::$%s" must be public and non-static.', $self->class->name, $property->name));
  57. }
  58. }
  59. if (!$self->definition) {
  60. throw new LogicException(\sprintf('The input class "%s" must have at least one argument or option.', $self->class->name));
  61. }
  62. foreach ($self->class->getMethods() as $method) {
  63. if ($attribute = Interact::tryFrom($method)) {
  64. $self->interactiveAttributes[] = $attribute;
  65. }
  66. }
  67. return $self;
  68. }
  69. /**
  70. * @internal
  71. */
  72. public function resolveValue(InputInterface $input): object
  73. {
  74. $instance = $this->class->newInstanceWithoutConstructor();
  75. foreach ($this->definition as $name => $spec) {
  76. // ignore required arguments that are not set yet (may happen in interactive mode)
  77. if ($spec instanceof Argument && $spec->isRequired() && \in_array($input->getArgument($spec->name), [null, []], true)) {
  78. continue;
  79. }
  80. $instance->$name = $spec->resolveValue($input);
  81. }
  82. return $instance;
  83. }
  84. /**
  85. * @internal
  86. */
  87. public function setValue(InputInterface $input, object $object): void
  88. {
  89. foreach ($this->definition as $name => $spec) {
  90. $property = $this->class->getProperty($name);
  91. if (!$property->isInitialized($object) || \in_array($value = $property->getValue($object), [null, []], true)) {
  92. continue;
  93. }
  94. match (true) {
  95. $spec instanceof Argument => $input->setArgument($spec->name, $value),
  96. $spec instanceof Option => $input->setOption($spec->name, $value),
  97. $spec instanceof self => $spec->setValue($input, $value),
  98. default => throw new LogicException('Unexpected specification type.'),
  99. };
  100. }
  101. }
  102. /**
  103. * @return iterable<Argument>
  104. */
  105. public function getArguments(): iterable
  106. {
  107. foreach ($this->definition as $spec) {
  108. if ($spec instanceof Argument) {
  109. yield $spec;
  110. } elseif ($spec instanceof self) {
  111. yield from $spec->getArguments();
  112. }
  113. }
  114. }
  115. /**
  116. * @return iterable<Option>
  117. */
  118. public function getOptions(): iterable
  119. {
  120. foreach ($this->definition as $spec) {
  121. if ($spec instanceof Option) {
  122. yield $spec;
  123. } elseif ($spec instanceof self) {
  124. yield from $spec->getOptions();
  125. }
  126. }
  127. }
  128. /**
  129. * @internal
  130. *
  131. * @return iterable<Interaction>
  132. */
  133. public function getPropertyInteractions(): iterable
  134. {
  135. foreach ($this->definition as $spec) {
  136. if ($spec instanceof self) {
  137. yield from $spec->getPropertyInteractions();
  138. } elseif ($spec instanceof Argument && $attribute = $spec->getInteractiveAttribute()) {
  139. yield new Interaction($this, $attribute);
  140. }
  141. }
  142. }
  143. /**
  144. * @internal
  145. *
  146. * @return iterable<Interaction>
  147. */
  148. public function getMethodInteractions(): iterable
  149. {
  150. foreach ($this->definition as $spec) {
  151. if ($spec instanceof self) {
  152. yield from $spec->getMethodInteractions();
  153. }
  154. }
  155. foreach ($this->interactiveAttributes as $attribute) {
  156. yield new Interaction($this, $attribute);
  157. }
  158. }
  159. }