11
11
12
12
namespace FOS \RestBundle \Controller \Annotations ;
13
13
14
- use Symfony \Component \Routing \Annotation \Route as BaseRoute ;
14
+ use Symfony \Component \Routing \Annotation \Route as BaseAnnotationRoute ;
15
+ use Symfony \Component \Routing \Attribute \Route as BaseAttributeRoute ;
16
+
17
+ if (class_exists (BaseAttributeRoute::class)) {
18
+ /**
19
+ * Compatibility layer for Symfony 6.4 and later.
20
+ *
21
+ * @internal
22
+ */
23
+ class CompatRoute extends BaseAttributeRoute
24
+ {
25
+ }
26
+ } else {
27
+ /**
28
+ * Compatibility layer for Symfony 6.3 and earlier.
29
+ *
30
+ * @internal
31
+ */
32
+ class CompatRoute extends BaseAnnotationRoute
33
+ {
34
+ }
35
+ }
15
36
16
37
/**
17
38
* Route annotation class.
21
42
* @Target({"CLASS", "METHOD"})
22
43
*/
23
44
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD )]
24
- class Route extends BaseRoute
45
+ class Route extends CompatRoute
25
46
{
47
+ /**
48
+ * @param array|string $data
49
+ * @param array|string|null $path
50
+ * @param string[] $requirements
51
+ * @param string[]|string $methods
52
+ * @param string[]|string $schemes
53
+ *
54
+ * @throws \TypeError if the $data argument is an unsupported type
55
+ */
26
56
public function __construct (
27
57
$ data = [],
28
58
$ path = null ,
@@ -41,78 +71,56 @@ public function __construct(
41
71
bool $ stateless = null ,
42
72
string $ env = null
43
73
) {
44
- // BC layer for symfony < 5.2
45
- // Before symfony/routing 5.2 the constructor only had one parameter
46
- $ method = new \ReflectionMethod (BaseRoute::class, '__construct ' );
47
- if (1 === $ method ->getNumberOfParameters ()) {
74
+ // Use Reflection to get the constructor from the parent class two levels up (accounting for our compat definition)
75
+ $ method = (new \ReflectionClass ($ this ))->getParentClass ()->getParentClass ()->getMethod ('__construct ' );
76
+
77
+ // The $data constructor parameter was removed in Symfony 6.0 in favor of named arguments
78
+ if ('data ' === $ method ->getParameters ()[0 ]->getName ()) {
79
+ parent ::__construct (
80
+ $ data ,
81
+ $ path ,
82
+ $ name ,
83
+ $ requirements ,
84
+ $ options ,
85
+ $ defaults ,
86
+ $ host ,
87
+ $ methods ,
88
+ $ schemes ,
89
+ $ condition ,
90
+ $ priority ,
91
+ $ locale ,
92
+ $ format ,
93
+ $ utf8 ,
94
+ $ stateless ,
95
+ $ env
96
+ );
97
+ } else {
48
98
if (\is_string ($ data )) {
49
- $ path = $ data ;
50
- $ data = [];
99
+ $ data = ['path ' => $ data ];
51
100
} elseif (!\is_array ($ data )) {
52
101
throw new \TypeError (sprintf ('"%s": Argument $data is expected to be a string or array, got "%s". ' , __METHOD__ , get_debug_type ($ data )));
102
+ } elseif (0 !== count ($ data ) && [] === \array_intersect (\array_keys ($ data ), ['path ' , 'name ' , 'requirements ' , 'options ' , 'defaults ' , 'host ' , 'methods ' , 'schemes ' , 'condition ' , 'priority ' , 'locale ' , 'format ' , 'utf8 ' , 'stateless ' , 'env ' ])) {
103
+ $ localizedPaths = $ data ;
104
+ $ data = ['path ' => $ localizedPaths ];
53
105
}
54
106
55
- $ data ['path ' ] = $ path ;
56
- $ data ['name ' ] = $ name ;
57
- $ data ['requirements ' ] = $ requirements ;
58
- $ data ['options ' ] = $ options ;
59
- $ data ['defaults ' ] = $ defaults ;
60
- $ data ['host ' ] = $ host ;
61
- $ data ['methods ' ] = $ methods ;
62
- $ data ['schemes ' ] = $ schemes ;
63
- $ data ['condition ' ] = $ condition ;
64
-
65
- parent ::__construct ($ data );
66
- } else {
67
- // BC layer for symfony < 6.0
68
- // The constructor parameter $data has been removed since symfony 6.0
69
- if ('data ' === $ method ->getParameters ()[0 ]->getName ()) {
70
- parent ::__construct (
71
- $ data ,
72
- $ path ,
73
- $ name ,
74
- $ requirements ,
75
- $ options ,
76
- $ defaults ,
77
- $ host ,
78
- $ methods ,
79
- $ schemes ,
80
- $ condition ,
81
- $ priority ,
82
- $ locale ,
83
- $ format ,
84
- $ utf8 ,
85
- $ stateless ,
86
- $ env
87
- );
88
- } else {
89
- if (\is_string ($ data )) {
90
- $ data = ['path ' => $ data ];
91
- } elseif (!\is_array ($ data )) {
92
- throw new \TypeError (sprintf ('"%s": Argument $data is expected to be a string or array, got "%s". ' , __METHOD__ , get_debug_type ($ data )));
93
- } elseif (0 !== count ($ data ) && [] === \array_intersect (\array_keys ($ data ), ['path ' , 'name ' , 'requirements ' , 'options ' , 'defaults ' , 'host ' , 'methods ' , 'schemes ' , 'condition ' , 'priority ' , 'locale ' , 'format ' , 'utf8 ' , 'stateless ' , 'env ' ])) {
94
- $ localizedPaths = $ data ;
95
- $ data = ['path ' => $ localizedPaths ];
96
- }
97
-
98
- parent ::__construct (
99
- $ data ['path ' ] ?? $ path ,
100
- $ data ['name ' ] ?? $ name ,
101
- $ data ['requirements ' ] ?? $ requirements ,
102
- $ data ['options ' ] ?? $ options ,
103
- $ data ['defaults ' ] ?? $ defaults ,
104
- $ data ['host ' ] ?? $ host ,
105
- $ data ['methods ' ] ?? $ methods ,
106
- $ data ['schemes ' ] ?? $ schemes ,
107
- $ data ['condition ' ] ?? $ condition ,
108
- $ data ['priority ' ] ?? $ priority ,
109
- $ data ['locale ' ] ?? $ locale ,
110
- $ data ['format ' ] ?? $ format ,
111
- $ data ['utf8 ' ] ?? $ utf8 ,
112
- $ data ['stateless ' ] ?? $ stateless ,
113
- $ data ['env ' ] ?? $ env
114
- );
115
- }
107
+ parent ::__construct (
108
+ $ data ['path ' ] ?? $ path ,
109
+ $ data ['name ' ] ?? $ name ,
110
+ $ data ['requirements ' ] ?? $ requirements ,
111
+ $ data ['options ' ] ?? $ options ,
112
+ $ data ['defaults ' ] ?? $ defaults ,
113
+ $ data ['host ' ] ?? $ host ,
114
+ $ data ['methods ' ] ?? $ methods ,
115
+ $ data ['schemes ' ] ?? $ schemes ,
116
+ $ data ['condition ' ] ?? $ condition ,
117
+ $ data ['priority ' ] ?? $ priority ,
118
+ $ data ['locale ' ] ?? $ locale ,
119
+ $ data ['format ' ] ?? $ format ,
120
+ $ data ['utf8 ' ] ?? $ utf8 ,
121
+ $ data ['stateless ' ] ?? $ stateless ,
122
+ $ data ['env ' ] ?? $ env
123
+ );
116
124
}
117
125
118
126
if (!$ this ->getMethods ()) {
0 commit comments