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\Event;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\HttpKernelInterface;
16
 
17
/**
18
 * Allows filtering of a controller callable.
19
 *
20
 * You can call getController() to retrieve the current controller. With
21
 * setController() you can set a new controller that is used in the processing
22
 * of the request.
23
 *
24
 * Controllers should be callables.
25
 *
26
 * @author Bernhard Schussek <bschussek@gmail.com>
27
 */
28
final class ControllerEvent extends KernelEvent
29
{
30
    private string|array|object $controller;
31
    private \ReflectionFunctionAbstract $controllerReflector;
32
    private array $attributes;
33
 
34
    public function __construct(HttpKernelInterface $kernel, callable $controller, Request $request, ?int $requestType)
35
    {
36
        parent::__construct($kernel, $request, $requestType);
37
 
38
        $this->setController($controller);
39
    }
40
 
41
    public function getController(): callable
42
    {
43
        return $this->controller;
44
    }
45
 
46
    public function getControllerReflector(): \ReflectionFunctionAbstract
47
    {
48
        return $this->controllerReflector;
49
    }
50
 
51
    /**
52
     * @param array<class-string, list<object>>|null $attributes
53
     */
54
    public function setController(callable $controller, array $attributes = null): void
55
    {
56
        if (null !== $attributes) {
57
            $this->attributes = $attributes;
58
        }
59
 
60
        if (isset($this->controller) && ($controller instanceof \Closure ? $controller == $this->controller : $controller === $this->controller)) {
61
            $this->controller = $controller;
62
 
63
            return;
64
        }
65
 
66
        if (null === $attributes) {
67
            unset($this->attributes);
68
        }
69
 
70
        if (\is_array($controller) && method_exists(...$controller)) {
71
            $this->controllerReflector = new \ReflectionMethod(...$controller);
72
        } elseif (\is_string($controller) && false !== $i = strpos($controller, '::')) {
73
            $this->controllerReflector = new \ReflectionMethod($controller);
74
        } else {
75
            $this->controllerReflector = new \ReflectionFunction($controller(...));
76
        }
77
 
78
        $this->controller = $controller;
79
    }
80
 
81
    /**
82
     * @return array<class-string, list<object>>
83
     */
84
    public function getAttributes(): array
85
    {
86
        if (isset($this->attributes)) {
87
            return $this->attributes;
88
        }
89
 
90
        if (\is_array($this->controller) && method_exists(...$this->controller)) {
91
            $class = new \ReflectionClass($this->controller[0]);
92
        } elseif (\is_string($this->controller) && false !== $i = strpos($this->controller, '::')) {
93
            $class = new \ReflectionClass(substr($this->controller, 0, $i));
94
        } else {
95
            $class = str_contains($this->controllerReflector->name, '{closure}') ? null : (\PHP_VERSION_ID >= 80111 ? $this->controllerReflector->getClosureCalledClass() : $this->controllerReflector->getClosureScopeClass());
96
        }
97
        $this->attributes = [];
98
 
99
        foreach (array_merge($class?->getAttributes() ?? [], $this->controllerReflector->getAttributes()) as $attribute) {
100
            if (class_exists($attribute->getName())) {
101
                $this->attributes[$attribute->getName()][] = $attribute->newInstance();
102
            }
103
        }
104
 
105
        return $this->attributes;
106
    }
107
}