Debian Codegraph @

vendor/symfony/console/Attribute/MapInput.php

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