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,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 [];
}
}