Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
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\HttpKernel\Controller\ArgumentResolver;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
16
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
17
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
 
20
/**
21
 * Attempt to resolve backed enum cases from request attributes, for a route path parameter,
22
 * leading to a 404 Not Found if the attribute value isn't a valid backing value for the enum type.
23
 *
24
 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
25
 *
26
 * @final since Symfony 6.2
27
 */
28
class BackedEnumValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
29
{
30
    /**
31
     * @deprecated since Symfony 6.2, use resolve() instead
32
     */
33
    public function supports(Request $request, ArgumentMetadata $argument): bool
34
    {
35
        @trigger_deprecation('symfony/http-kernel', '6.2', 'The "%s()" method is deprecated, use "resolve()" instead.', __METHOD__);
36
 
37
        if (!is_subclass_of($argument->getType(), \BackedEnum::class)) {
38
            return false;
39
        }
40
 
41
        if ($argument->isVariadic()) {
42
            // only target route path parameters, which cannot be variadic.
43
            return false;
44
        }
45
 
46
        // do not support if no value can be resolved at all
47
        // letting the \Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver be used
48
        // or \Symfony\Component\HttpKernel\Controller\ArgumentResolver fail with a meaningful error.
49
        return $request->attributes->has($argument->getName());
50
    }
51
 
52
    public function resolve(Request $request, ArgumentMetadata $argument): iterable
53
    {
54
        if (!is_subclass_of($argument->getType(), \BackedEnum::class)) {
55
            return [];
56
        }
57
 
58
        if ($argument->isVariadic()) {
59
            // only target route path parameters, which cannot be variadic.
60
            return [];
61
        }
62
 
63
        // do not support if no value can be resolved at all
64
        // letting the \Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver be used
65
        // or \Symfony\Component\HttpKernel\Controller\ArgumentResolver fail with a meaningful error.
66
        if (!$request->attributes->has($argument->getName())) {
67
            return [];
68
        }
69
 
70
        $value = $request->attributes->get($argument->getName());
71
 
72
        if (null === $value) {
73
            return [null];
74
        }
75
 
76
        if ($value instanceof \BackedEnum) {
77
            return [$value];
78
        }
79
 
80
        if (!\is_int($value) && !\is_string($value)) {
81
            throw new \LogicException(sprintf('Could not resolve the "%s $%s" controller argument: expecting an int or string, got "%s".', $argument->getType(), $argument->getName(), get_debug_type($value)));
82
        }
83
 
84
        /** @var class-string<\BackedEnum> $enumType */
85
        $enumType = $argument->getType();
86
 
87
        try {
88
            return [$enumType::from($value)];
89
        } catch (\ValueError $e) {
90
            throw new NotFoundHttpException(sprintf('Could not resolve the "%s $%s" controller argument: ', $argument->getType(), $argument->getName()).$e->getMessage(), $e);
91
        }
92
    }
93
}