First Commit

This commit is contained in:
Esteban
2026-07-03 16:27:34 +02:00
commit 7a7440daab
8955 changed files with 1117958 additions and 0 deletions
@@ -0,0 +1,163 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\ArgumentResolver\Exception\NearMissValueResolverException;
use Symfony\Component\Console\ArgumentResolver\Exception\ResolverNotFoundException;
use Symfony\Component\Console\ArgumentResolver\ValueResolver as Resolver;
use Symfony\Component\Console\ArgumentResolver\ValueResolver\ValueResolverInterface;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Attribute\ValueResolver;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Cursor;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\RawInputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Contracts\Service\ServiceProviderInterface;
/**
* Resolves the arguments passed to a console command.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class ArgumentResolver implements ArgumentResolverInterface
{
/**
* @param iterable<mixed, ValueResolverInterface> $argumentValueResolvers
*/
public function __construct(
private iterable $argumentValueResolvers = [],
private ?ContainerInterface $namedResolvers = null,
) {
}
public function getArguments(InputInterface $input, callable $command, ?\ReflectionFunctionAbstract $reflector = null): array
{
$reflector ??= new \ReflectionFunction($command(...));
$argumentReflectors = [];
foreach ($reflector->getParameters() as $param) {
$argumentReflectors[$param->getName()] = new ReflectionMember($param);
}
$arguments = [];
foreach ($argumentReflectors as $argumentName => $member) {
$argumentValueResolvers = $this->argumentValueResolvers;
$disabledResolvers = [];
if ($this->namedResolvers && $attributes = $member->getAttributes(ValueResolver::class)) {
$resolverName = null;
foreach ($attributes as $attribute) {
if ($attribute->disabled) {
$disabledResolvers[$attribute->resolver] = true;
} elseif ($resolverName) {
throw new \LogicException(\sprintf('You can only pin one resolver per argument, but argument "$%s" of "%s()" has more.', $member->getName(), $member->getSourceName()));
} else {
$resolverName = $attribute->resolver;
}
}
if ($resolverName) {
if (!$this->namedResolvers->has($resolverName)) {
throw new ResolverNotFoundException($resolverName, $this->namedResolvers instanceof ServiceProviderInterface ? array_keys($this->namedResolvers->getProvidedServices()) : []);
}
$argumentValueResolvers = [
$this->namedResolvers->get($resolverName),
];
}
}
$valueResolverExceptions = [];
foreach ($argumentValueResolvers as $name => $resolver) {
if (isset($disabledResolvers[\is_int($name) ? $resolver::class : $name])) {
continue;
}
try {
$count = 0;
foreach ($resolver->resolve($argumentName, $input, $member) as $argument) {
++$count;
$arguments[] = $argument;
}
} catch (NearMissValueResolverException $e) {
$valueResolverExceptions[] = $e;
}
if (1 < $count && !$member->isVariadic()) {
throw new \InvalidArgumentException(\sprintf('"%s::resolve()" must yield at most one value for non-variadic arguments.', get_debug_type($resolver)));
}
if ($count) {
continue 2;
}
}
// For variadic parameters with explicit input mapping, 0 values is valid
if ($member->isVariadic() && (Argument::tryFrom($member->getMember()) || Option::tryFrom($member->getMember()))) {
continue;
}
$type = $member->getType();
$typeName = $type instanceof \ReflectionNamedType ? $type->getName() : null;
if ($typeName && \in_array($typeName, [
InputInterface::class,
RawInputInterface::class,
OutputInterface::class,
SymfonyStyle::class,
Cursor::class,
\Symfony\Component\Console\Application::class,
Command::class,
], true)) {
continue;
}
$reasons = array_map(static fn (NearMissValueResolverException $e) => $e->getMessage(), $valueResolverExceptions);
if (!$reasons) {
$reasons[] = \sprintf('The parameter has no #[Argument], #[Option], or #[MapInput] attribute, and its type "%s" cannot be auto-resolved.', $typeName ?? 'unknown');
$reasons[] = 'Add an attribute to map this parameter to command input.';
}
throw new \RuntimeException(\sprintf('Could not resolve parameter "$%s" of command "%s".'."\n\n".'Possible reasons:'."\n".' • '.implode("\n", $reasons), $member->getName(), $member->getSourceName()));
}
return $arguments;
}
/**
* @return iterable<int, ValueResolverInterface>
*/
public static function getDefaultArgumentValueResolvers(): iterable
{
$builtinTypeResolver = new Resolver\BuiltinTypeValueResolver();
$backedEnumResolver = new Resolver\BackedEnumValueResolver();
$dateTimeResolver = new Resolver\DateTimeValueResolver();
$inputFileResolver = new Resolver\InputFileValueResolver();
return [
$backedEnumResolver,
new Resolver\UidValueResolver(),
$inputFileResolver,
$builtinTypeResolver,
new Resolver\MapInputValueResolver($builtinTypeResolver, $backedEnumResolver, $dateTimeResolver),
$dateTimeResolver,
new Resolver\DefaultValueResolver(),
new Resolver\VariadicValueResolver(),
];
}
}
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver;
use Symfony\Component\Console\ArgumentResolver\Exception\ResolverNotFoundException;
use Symfony\Component\Console\Input\InputInterface;
/**
* Determines the arguments for a specific Console Command.
*
* @author Robin Chalas <robin.chalas@gmail.com>
* @author Fabien Potencier <fabien@symfony.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
interface ArgumentResolverInterface
{
/**
* Returns the arguments to pass to the Console Command after resolution.
*
* @throws \RuntimeException When no value could be provided for a required argument
* @throws ResolverNotFoundException
*/
public function getArguments(InputInterface $input, callable $command, ?\ReflectionFunctionAbstract $reflector = null): array;
}
@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\Exception;
/**
* Lets value resolvers tell when an argument could be under their watch but failed to be resolved.
*
* Throwing this exception inside `ValueResolverInterface::resolve` does not interrupt the value resolvers chain.
*/
final class NearMissValueResolverException extends \RuntimeException
{
}
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\Exception;
final class ResolverNotFoundException extends \RuntimeException
{
/**
* @param string[] $alternatives
*/
public function __construct(string $name, array $alternatives = [])
{
$msg = \sprintf('You have requested a non-existent resolver "%s".', $name);
if ($alternatives) {
if (1 === \count($alternatives)) {
$msg .= ' Did you mean this: "';
} else {
$msg .= ' Did you mean one of these: "';
}
$msg .= implode('", "', $alternatives).'"?';
}
parent::__construct($msg);
}
}
@@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Stopwatch\Stopwatch;
/**
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class TraceableArgumentResolver implements ArgumentResolverInterface
{
public function __construct(
private ArgumentResolverInterface $resolver,
private Stopwatch $stopwatch,
) {
}
public function getArguments(InputInterface $input, callable $command, ?\ReflectionFunctionAbstract $reflector = null): array
{
$e = $this->stopwatch->start('command.get_arguments');
try {
return $this->resolver->getArguments($input, $command, $reflector);
} finally {
$e->stop();
}
}
}
@@ -0,0 +1,90 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputInterface;
/**
* Resolves a BackedEnum instance from a Command argument or option.
*
* @author Robin Chalas <robin.chalas@gmail.com>
* @author Jérôme Tamarelle <jerome@tamarelle.net>
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
final class BackedEnumValueResolver implements ValueResolverInterface
{
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
if ($argument = Argument::tryFrom($member->getMember())) {
if (!is_subclass_of($argument->typeName, \BackedEnum::class)) {
return [];
}
return [$this->resolveArgument($argument, $input)];
}
if ($option = Option::tryFrom($member->getMember())) {
if (!is_subclass_of($option->typeName, \BackedEnum::class)) {
return [];
}
return [$this->resolveOption($option, $input)];
}
return [];
}
private function resolveArgument(Argument $argument, InputInterface $input): ?\BackedEnum
{
$value = $input->getArgument($argument->name);
if (null === $value) {
return null;
}
if ($value instanceof $argument->typeName) {
return $value;
}
if (!\is_string($value) && !\is_int($value)) {
throw InvalidArgumentException::fromEnumValue($argument->name, get_debug_type($value), $argument->suggestedValues);
}
return $argument->typeName::tryFrom($value)
?? throw InvalidArgumentException::fromEnumValue($argument->name, $value, $argument->suggestedValues);
}
private function resolveOption(Option $option, InputInterface $input): ?\BackedEnum
{
$value = $input->getOption($option->name);
if (null === $value) {
return null;
}
if ($value instanceof $option->typeName) {
return $value;
}
if (!\is_string($value) && !\is_int($value)) {
throw InvalidOptionException::fromEnumValue($option->name, get_debug_type($value), $option->suggestedValues);
}
return $option->typeName::tryFrom($value)
?? throw InvalidOptionException::fromEnumValue($option->name, $value, $option->suggestedValues);
}
}
@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\InputInterface;
/**
* Resolves values from #[Argument] or #[Option] attributes for built-in PHP types.
*
* Handles: string, bool, int, float, array
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class BuiltinTypeValueResolver implements ValueResolverInterface
{
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
if ($member->isVariadic()) {
return [];
}
if ($argument = Argument::tryFrom($member->getMember())) {
if (is_subclass_of($argument->typeName, \BackedEnum::class)) {
return [];
}
return [$input->getArgument($argument->name)];
}
if ($option = Option::tryFrom($member->getMember())) {
if (is_subclass_of($option->typeName, \BackedEnum::class)) {
return [];
}
return [$this->resolveOption($option, $input)];
}
return [];
}
private function resolveOption(Option $option, InputInterface $input): mixed
{
$value = $input->getOption($option->name);
if (null === $value && \in_array($option->typeName, Option::ALLOWED_UNION_TYPES, true)) {
return true;
}
if ('array' === $option->typeName && $option->allowNull && [] === $value) {
return null;
}
if ('bool' === $option->typeName) {
if ($option->allowNull && null === $value) {
return null;
}
return $value ?? $option->default;
}
return $value;
}
}
@@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Psr\Clock\ClockInterface;
use Symfony\Component\Console\Attribute\MapDateTime;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\InputInterface;
/**
* Resolves a \DateTime* instance as a command input argument or option.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Tim Goudriaan <tim@codedmonkey.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class DateTimeValueResolver implements ValueResolverInterface
{
public function __construct(
private readonly ?ClockInterface $clock = null,
) {
}
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
$type = $member->getType();
if (!$type instanceof \ReflectionNamedType || !is_a($type->getName(), \DateTimeInterface::class, true)) {
return [];
}
$attribute = $member->getAttribute(MapDateTime::class);
$inputName = $attribute?->argument ?? $attribute?->option ?? $member->getInputName();
// Try to get value from argument or option
$value = null;
if ($input->hasArgument($inputName)) {
$value = $input->getArgument($inputName);
} elseif ($input->hasOption($inputName)) {
$value = $input->getOption($inputName);
}
/** @var class-string<\DateTimeImmutable>|class-string<\DateTime> $class */
$class = \DateTimeInterface::class === $type->getName() ? \DateTimeImmutable::class : $type->getName();
if (!$value) {
if ($member->isNullable()) {
return [null];
}
if (!$this->clock) {
return [new $class()];
}
$value = $this->clock->now();
}
if ($value instanceof \DateTimeInterface) {
return [$value instanceof $class ? $value : $class::createFromInterface($value)];
}
$format = $attribute?->format;
if (null !== $format) {
$date = $class::createFromFormat($format, $value, $this->clock?->now()->getTimeZone());
if (($class::getLastErrors() ?: ['warning_count' => 0])['warning_count']) {
$date = false;
}
} else {
if (false !== filter_var($value, \FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
$value = '@'.$value;
}
try {
$date = new $class($value, $this->clock?->now()->getTimeZone());
} catch (\Exception) {
$date = false;
}
}
if (!$date) {
$message = \sprintf('Invalid date given for parameter "$%s".', $argumentName);
if ($format) {
$message .= \sprintf(' Expected format: "%s".', $format);
}
$message .= ' Use #[MapDateTime(format: \'your-format\')] to specify a custom format.';
throw new \RuntimeException($message);
}
return [$date];
}
}
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\InputInterface;
/**
* Yields the default value defined in the command signature when no input value has been explicitly passed.
*
* @author Iltar van der Berg <kjarli@gmail.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class DefaultValueResolver implements ValueResolverInterface
{
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
if ($member->hasDefaultValue()) {
return [$member->getDefaultValue()];
}
if ($member->isNullable() && !$member->isVariadic()) {
return [null];
}
return [];
}
}
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\File\InputFile;
use Symfony\Component\Console\Input\InputInterface;
/**
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class InputFileValueResolver implements ValueResolverInterface
{
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
$type = $member->getType();
if (!$type instanceof \ReflectionNamedType || InputFile::class !== $type->getName()) {
return [];
}
if ($argument = Argument::tryFrom($member->getMember())) {
return $this->resolveValue($input->getArgument($argument->name), $member);
}
if ($option = Option::tryFrom($member->getMember())) {
return $this->resolveValue($input->getOption($option->name), $member);
}
return [];
}
private function resolveValue(mixed $value, ReflectionMember $member): iterable
{
if (!$value) {
if ($member->isNullable()) {
return [null];
}
return [];
}
if ($value instanceof InputFile) {
return [$value];
}
return [InputFile::fromPath($value)];
}
}
@@ -0,0 +1,128 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\MapInput;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Exception\InputValidationFailedException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Resolves the value of a input argument/option to an object holding the #[MapInput] attribute.
*
* @author Yonel Ceruto <open@yceruto.dev>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class MapInputValueResolver implements ValueResolverInterface
{
public function __construct(
private readonly ValueResolverInterface $builtinTypeResolver,
private readonly ValueResolverInterface $backedEnumResolver,
private readonly ValueResolverInterface $dateTimeResolver,
private readonly ?ValidatorInterface $validator = null,
) {
}
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
if (!$attribute = MapInput::tryFrom($member->getMember())) {
return [];
}
$instance = $this->resolveMapInput($attribute, $input);
$violations = $this->validator?->validate($instance, null, $attribute->validationGroups) ?? [];
if (!\count($violations)) {
return [$instance];
}
$map = $this->buildPropertyToInputMap($attribute);
$messages = [];
foreach ($violations as $violation) {
$path = $violation->getPropertyPath();
$label = $map[$path] ?? $path;
$messages[] = $label.': '.$violation->getMessage();
}
throw new InputValidationFailedException(implode("\n", $messages), $violations);
}
private function resolveMapInput(MapInput $mapInput, InputInterface $input): object
{
$instance = $mapInput->getClass()->newInstanceWithoutConstructor();
foreach ($mapInput->getDefinition() as $name => $spec) {
// ignore required arguments that are not set yet (may happen in interactive mode)
if ($spec instanceof Argument && $spec->isRequired() && \in_array($input->getArgument($spec->name), [null, []], true)) {
continue;
}
$instance->$name = match (true) {
$spec instanceof Argument => $this->resolveArgumentSpec($spec, $mapInput->getClass()->getProperty($name), $input),
$spec instanceof Option => $this->resolveOptionSpec($spec, $mapInput->getClass()->getProperty($name), $input),
$spec instanceof MapInput => $this->resolveMapInput($spec, $input),
};
}
return $instance;
}
/**
* @return array<string, string>
*/
private function buildPropertyToInputMap(MapInput $mapInput, string $prefix = ''): array
{
$map = [];
foreach ($mapInput->getDefinition() as $propertyName => $spec) {
$path = $prefix.$propertyName;
$map[$path] = match (true) {
$spec instanceof Argument => $spec->name,
$spec instanceof Option => '--'.$spec->name,
default => $path,
};
if ($spec instanceof MapInput) {
$map += $this->buildPropertyToInputMap($spec, $path.'.');
}
}
return $map;
}
private function resolveArgumentSpec(Argument $argument, \ReflectionProperty $property, InputInterface $input): mixed
{
if (is_subclass_of($argument->typeName, \BackedEnum::class)) {
return iterator_to_array($this->backedEnumResolver->resolve($property->name, $input, new ReflectionMember($property)))[0] ?? null;
}
if (is_a($argument->typeName, \DateTimeInterface::class, true)) {
return iterator_to_array($this->dateTimeResolver->resolve($property->name, $input, new ReflectionMember($property)))[0] ?? null;
}
return iterator_to_array($this->builtinTypeResolver->resolve($property->name, $input, new ReflectionMember($property)))[0] ?? null;
}
private function resolveOptionSpec(Option $option, \ReflectionProperty $property, InputInterface $input): mixed
{
if (is_subclass_of($option->typeName, \BackedEnum::class)) {
return iterator_to_array($this->backedEnumResolver->resolve($property->name, $input, new ReflectionMember($property)))[0] ?? null;
}
if (is_a($option->typeName, \DateTimeInterface::class, true)) {
return iterator_to_array($this->dateTimeResolver->resolve($property->name, $input, new ReflectionMember($property)))[0] ?? null;
}
return iterator_to_array($this->builtinTypeResolver->resolve($property->name, $input, new ReflectionMember($property)))[0] ?? null;
}
}
@@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\ArgumentResolver\Exception\NearMissValueResolverException;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* Yields a service from a service locator keyed by command and argument name.
*
* @author Nicolas Grekas <p@tchwork.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class ServiceValueResolver implements ValueResolverInterface
{
public function __construct(
private readonly ContainerInterface $container,
) {
}
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
$command = $input->getFirstArgument();
if ($command && $this->container->has($command)) {
$locator = $this->container->get($command);
if ($locator instanceof ContainerInterface && $locator->has($argumentName)) {
try {
return [$locator->get($argumentName)];
} catch (RuntimeException|\Throwable $e) {
$what = \sprintf('argument $%s', $argumentName);
$message = str_replace(\sprintf('service "%s"', $argumentName), $what, $e->getMessage());
$what .= \sprintf(' of command "%s"', $command);
$message = preg_replace('/service "\.service_locator\.[^"]++"/', $what, $message);
if ($e->getMessage() === $message) {
$message = \sprintf('Cannot resolve %s: %s', $what, $message);
}
throw new NearMissValueResolverException($message, $e->getCode(), $e);
}
}
}
$type = $member->getType();
if (!$type instanceof \ReflectionNamedType || $type->isBuiltin()) {
return [];
}
$typeName = $type->getName();
if (!$this->container->has($typeName)) {
return [];
}
try {
$service = $this->container->get($typeName);
if (!$service instanceof $typeName) {
throw new NearMissValueResolverException(\sprintf('Service "%s" exists in the container but is not an instance of "%s".', $typeName, $typeName));
}
return [$service];
} catch (\Throwable $e) {
throw new NearMissValueResolverException(\sprintf('Cannot resolve parameter "$%s" of type "%s": %s', $argumentName, $typeName, $e->getMessage()), previous: $e);
}
}
}
@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Stopwatch\Stopwatch;
/**
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class TraceableValueResolver implements ValueResolverInterface
{
public function __construct(
private ValueResolverInterface $inner,
private Stopwatch $stopwatch,
) {
}
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
$method = $this->inner::class.'::'.__FUNCTION__;
$this->stopwatch->start($method, 'command.argument_value_resolver');
try {
yield from $this->inner->resolve($argumentName, $input, $member);
} finally {
$this->stopwatch->stop($method);
}
}
}
@@ -0,0 +1,87 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Uid\AbstractUid;
/**
* Resolves an AbstractUid instance from a Command argument or option.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class UidValueResolver implements ValueResolverInterface
{
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
if ($argument = Argument::tryFrom($member->getMember())) {
if (!is_subclass_of($argument->typeName, AbstractUid::class)) {
return [];
}
return [$this->resolveArgument($argument, $input)];
}
if ($option = Option::tryFrom($member->getMember())) {
if (!is_subclass_of($option->typeName, AbstractUid::class)) {
return [];
}
return [$this->resolveOption($option, $input)];
}
return [];
}
private function resolveArgument(Argument $argument, InputInterface $input): ?AbstractUid
{
$value = $input->getArgument($argument->name);
if (null === $value) {
return null;
}
if ($value instanceof $argument->typeName) {
return $value;
}
if (!\is_string($value) || !$argument->typeName::isValid($value)) {
throw new InvalidArgumentException(\sprintf('The uid for the "%s" argument is invalid.', $argument->name));
}
return $argument->typeName::fromString($value);
}
private function resolveOption(Option $option, InputInterface $input): ?AbstractUid
{
$value = $input->getOption($option->name);
if (null === $value) {
return null;
}
if ($value instanceof $option->typeName) {
return $value;
}
if (!\is_string($value) || !$option->typeName::isValid($value)) {
throw new InvalidOptionException(\sprintf('The uid for the "--%s" option is invalid.', $option->name));
}
return $option->typeName::fromString($value);
}
}
@@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\InputInterface;
/**
* Responsible for resolving the value of a Command argument based on its
* parameter metadata and the Command MapInput.
*
* @author Nicolas Grekas <p@tchwork.com>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
interface ValueResolverInterface
{
/**
* Returns the possible value(s) for the argument.
*/
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable;
}
@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
use Symfony\Component\Console\Input\InputInterface;
/**
* Yields a variadic argument's values from the input.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
final class VariadicValueResolver implements ValueResolverInterface
{
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
{
if (!$member->isVariadic()) {
return [];
}
if ($argument = Argument::tryFrom($member->getMember())) {
$values = $input->getArgument($argument->name);
if (!\is_array($values)) {
throw new \InvalidArgumentException(\sprintf('The action argument "...$%1$s" is required to be an array, the input argument "%1$s" contains a type of "%2$s" instead.', $argument->name, get_debug_type($values)));
}
return $values;
}
if ($option = Option::tryFrom($member->getMember())) {
$values = $input->getOption($option->name);
if (!\is_array($values)) {
throw new \InvalidArgumentException(\sprintf('The action argument "...$%1$s" is required to be an array, the input option "--%1$s" contains a type of "%2$s" instead.', $option->name, get_debug_type($values)));
}
return $values;
}
return [];
}
}