28
28
final class ControllerEvent extends KernelEvent
29
29
{
30
30
private string |array |object $ controller ;
31
+ private \ReflectionFunctionAbstract $ controllerReflector ;
31
32
private array $ attributes ;
32
33
33
34
public function __construct (HttpKernelInterface $ kernel , callable $ controller , Request $ request , ?int $ requestType )
@@ -42,6 +43,11 @@ public function getController(): callable
42
43
return $ this ->controller ;
43
44
}
44
45
46
+ public function getControllerReflector (): \ReflectionFunctionAbstract
47
+ {
48
+ return $ this ->controllerReflector ;
49
+ }
50
+
45
51
/**
46
52
* @param array<class-string, list<object>>|null $attributes
47
53
*/
@@ -62,17 +68,13 @@ public function setController(callable $controller, array $attributes = null): v
62
68
}
63
69
64
70
if (\is_array ($ controller ) && method_exists (...$ controller )) {
65
- $ action = new \ReflectionMethod (...$ controller );
66
- $ class = new \ReflectionClass ($ controller [0 ]);
71
+ $ this ->controllerReflector = new \ReflectionMethod (...$ controller );
67
72
} elseif (\is_string ($ controller ) && false !== $ i = strpos ($ controller , ':: ' )) {
68
- $ action = new \ReflectionMethod ($ controller );
69
- $ class = new \ReflectionClass (substr ($ controller , 0 , $ i ));
73
+ $ this ->controllerReflector = new \ReflectionMethod ($ controller );
70
74
} else {
71
- $ action = new \ReflectionFunction ($ controller (...));
72
- $ class = str_contains ($ action ->name , '{closure} ' ) ? null : $ action ->getClosureScopeClass ();
75
+ $ this ->controllerReflector = new \ReflectionFunction ($ controller (...));
73
76
}
74
77
75
- $ this ->getRequest ()->attributes ->set ('_controller_reflectors ' , [$ class , $ action ]);
76
78
$ this ->controller = $ controller ;
77
79
}
78
80
@@ -81,13 +83,20 @@ public function setController(callable $controller, array $attributes = null): v
81
83
*/
82
84
public function getAttributes (): array
83
85
{
84
- if (isset ($ this ->attributes ) || ![ $ class , $ action ] = $ this -> getRequest ()-> attributes -> get ( ' _controller_reflectors ' ) ) {
85
- return $ this ->attributes ??= [] ;
86
+ if (isset ($ this ->attributes )) {
87
+ return $ this ->attributes ;
86
88
}
87
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 : $ this ->controllerReflector ->getClosureScopeClass ();
96
+ }
88
97
$ this ->attributes = [];
89
98
90
- foreach (array_merge ($ class ?->getAttributes() ?? [], $ action ->getAttributes ()) as $ attribute ) {
99
+ foreach (array_merge ($ class ?->getAttributes() ?? [], $ this -> controllerReflector ->getAttributes ()) as $ attribute ) {
91
100
if (class_exists ($ attribute ->getName ())) {
92
101
$ this ->attributes [$ attribute ->getName ()][] = $ attribute ->newInstance ();
93
102
}
0 commit comments