Interaction.php 1.7 KB

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