Debian Codegraph @

vendor/symfony/console/Interaction/Interaction.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\Interaction;
13
14 use Symfony\Component\Console\Attribute\InteractiveAttributeInterface;
15 use Symfony\Component\Console\Attribute\MapInput;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Output\OutputInterface;
18
19 /**
20 * @internal
21 */
22 final class Interaction
23 {
24 public function __construct(
25 private readonly object $owner,
26 private readonly InteractiveAttributeInterface $attribute,
27 ) {
28 }
29
30 /**
31 * @param \Closure(\ReflectionFunction $function, InputInterface $input, OutputInterface $output): array $parameterResolver
32 */
33 public function interact(InputInterface $input, OutputInterface $output, \Closure $parameterResolver): void
34 {
35 if ($this->owner instanceof MapInput) {
36 $function = $this->attribute->getFunction($this->owner->resolveValue($input));
37 $function->invoke(...$parameterResolver($function, $input, $output));
38 $this->owner->setValue($input, $function->getClosureThis());
39
40 return;
41 }
42
43 $function = $this->attribute->getFunction($this->owner);
44 $function->invoke(...$args = $parameterResolver($function, $input, $output));
45 foreach ($function->getParameters() as $i => $parameter) {
46 if (\is_object($args[$i]) && $spec = MapInput::tryFrom($parameter)) {
47 $spec->setValue($input, $args[$i]);
48 }
49 }
50 }
51 }