package and depencies
This commit is contained in:
@@ -16,6 +16,7 @@ use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolve
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver;
|
||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
|
||||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
|
||||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
|
||||
@@ -27,11 +28,11 @@ use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInter
|
||||
*/
|
||||
final class ArgumentResolver implements ArgumentResolverInterface
|
||||
{
|
||||
private $argumentMetadataFactory;
|
||||
private $argumentValueResolvers;
|
||||
private ArgumentMetadataFactoryInterface $argumentMetadataFactory;
|
||||
private iterable $argumentValueResolvers;
|
||||
|
||||
/**
|
||||
* @param iterable<mixed, ArgumentValueResolverInterface> $argumentValueResolvers
|
||||
* @param iterable<mixed, ArgumentValueResolverInterface|ValueResolverInterface> $argumentValueResolvers
|
||||
*/
|
||||
public function __construct(ArgumentMetadataFactoryInterface $argumentMetadataFactory = null, iterable $argumentValueResolvers = [])
|
||||
{
|
||||
@@ -39,33 +40,34 @@ final class ArgumentResolver implements ArgumentResolverInterface
|
||||
$this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getArguments(Request $request, callable $controller): array
|
||||
public function getArguments(Request $request, callable $controller, \ReflectionFunctionAbstract $reflector = null): array
|
||||
{
|
||||
$arguments = [];
|
||||
|
||||
foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller) as $metadata) {
|
||||
foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller, $reflector) as $metadata) {
|
||||
foreach ($this->argumentValueResolvers as $resolver) {
|
||||
if (!$resolver->supports($request, $metadata)) {
|
||||
if ((!$resolver instanceof ValueResolverInterface || $resolver instanceof TraceableValueResolver) && !$resolver->supports($request, $metadata)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$resolved = $resolver->resolve($request, $metadata);
|
||||
|
||||
$atLeastOne = false;
|
||||
foreach ($resolved as $append) {
|
||||
$atLeastOne = true;
|
||||
$arguments[] = $append;
|
||||
$count = 0;
|
||||
foreach ($resolver->resolve($request, $metadata) as $argument) {
|
||||
++$count;
|
||||
$arguments[] = $argument;
|
||||
}
|
||||
|
||||
if (!$atLeastOne) {
|
||||
if (1 < $count && !$metadata->isVariadic()) {
|
||||
throw new \InvalidArgumentException(sprintf('"%s::resolve()" must yield at most one value for non-variadic arguments.', get_debug_type($resolver)));
|
||||
}
|
||||
|
||||
if ($count) {
|
||||
// continue to the next controller argument
|
||||
continue 2;
|
||||
}
|
||||
|
||||
if (!$resolver instanceof ValueResolverInterface) {
|
||||
throw new \InvalidArgumentException(sprintf('"%s::resolve()" must yield at least one value.', get_debug_type($resolver)));
|
||||
}
|
||||
|
||||
// continue to the next controller argument
|
||||
continue 2;
|
||||
}
|
||||
|
||||
$representative = $controller;
|
||||
@@ -73,7 +75,7 @@ final class ArgumentResolver implements ArgumentResolverInterface
|
||||
if (\is_array($representative)) {
|
||||
$representative = sprintf('%s::%s()', \get_class($representative[0]), $representative[1]);
|
||||
} elseif (\is_object($representative)) {
|
||||
$representative = \get_class($representative);
|
||||
$representative = get_debug_type($representative);
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.', $representative, $metadata->getName()));
|
||||
|
Reference in New Issue
Block a user