@@ -54,16 +54,31 @@ public function __construct($route, $options = [])
54
54
public function toArray ()
55
55
{
56
56
return array_merge ([
57
- 'name ' => $ this ->route ->getName (),
58
- 'methods ' => $ this ->route -> getMethods (),
59
- 'domain ' => $ this ->route ->domain (),
60
- 'path ' => $ this ->preparePath (),
61
- 'action ' => $ this ->route ->getAction (),
62
- 'wheres ' => $ this ->extractWheres (),
63
- 'errors ' => $ this ->errors ,
57
+ 'name ' => $ this ->route ->getName (),
58
+ 'methods ' => $ this ->getMethods (),
59
+ 'domain ' => $ this ->route ->domain (),
60
+ 'path ' => $ this ->preparePath (),
61
+ 'action ' => $ this ->route ->getAction (),
62
+ 'wheres ' => $ this ->extractWheres (),
63
+ 'errors ' => $ this ->errors ,
64
64
], $ this ->getMeta (), $ this ->options );
65
65
}
66
66
67
+ /**
68
+ * Cross version get methods.
69
+ *
70
+ * @return array
71
+ */
72
+ private function getMethods ()
73
+ {
74
+ // Laravel <5.4
75
+ if (method_exists ($ this ->route , 'getMethods ' )) {
76
+ $ this ->route ->getMethods ();
77
+ }
78
+ // Laravel 5.4+
79
+ return $ this ->route ->methods ();
80
+ }
81
+
67
82
protected function extractWheres ()
68
83
{
69
84
$ prop = $ this ->getRouteReflection ()->getProperty ('wheres ' );
@@ -73,7 +88,7 @@ protected function extractWheres()
73
88
74
89
// Хак, чтобы в json всегда был объект
75
90
if (empty ($ wheres )) {
76
- return (object )[];
91
+ return (object ) [];
77
92
}
78
93
79
94
return $ wheres ;
@@ -94,7 +109,7 @@ protected function extractAnnotation()
94
109
{
95
110
$ reflection = $ this ->getActionReflection ();
96
111
97
- if (!is_null ($ reflection )) {
112
+ if (! is_null ($ reflection )) {
98
113
return $ reflection ->getDocComment ();
99
114
}
100
115
@@ -114,7 +129,7 @@ protected function extractFormRequest()
114
129
// TODO Write the reasoning behind following lines.
115
130
try {
116
131
$ class = $ parameter ->getClass ();
117
- } catch (\ReflectionException $ e ){
132
+ } catch (\ReflectionException $ e ) {
118
133
break ;
119
134
}
120
135
@@ -170,18 +185,21 @@ protected function getActionReflection()
170
185
list ($ controller , $ action ) = explode ('@ ' , $ uses );
171
186
172
187
// Если нет контроллера.
173
- if (!class_exists ($ controller )) {
188
+ if (! class_exists ($ controller )) {
174
189
$ this ->setError ('uses ' , 'controller does not exists ' );
190
+
175
191
return null ;
176
192
}
177
193
178
194
// Если нет метода в контроллере.
179
- if (!method_exists ($ controller , $ action )) {
195
+ if (! method_exists ($ controller , $ action )) {
180
196
$ this ->setError ('uses ' , 'controller@method does not exists ' );
197
+
181
198
return null ;
182
199
}
183
200
184
- return $ this ->actionReflection = new \ReflectionMethod ($ controller , $ action );
201
+ return $ this ->actionReflection = new \ReflectionMethod ($ controller ,
202
+ $ action );
185
203
}
186
204
187
205
if (is_callable ($ uses )) {
@@ -195,14 +213,29 @@ protected function getActionReflection()
195
213
196
214
protected function preparePath ()
197
215
{
198
- $ path = $ this ->route -> getPath ();
216
+ $ path = $ this ->getUri ();
199
217
if ($ path === '/ ' ) {
200
218
return $ path ;
201
219
}
202
220
203
221
return trim ($ path , '/ ' );
204
222
}
205
223
224
+ /**
225
+ * Backwards compatible uri getter.
226
+ *
227
+ * @return string
228
+ */
229
+ protected function getUri (){
230
+ if (method_exists ($ this ->route , 'getPath ' )){
231
+ // Laravel <5.4
232
+ return $ this ->route ->getPath ();
233
+ }
234
+
235
+ // Laravel 5.4+
236
+ return $ this ->route ->uri ();
237
+ }
238
+
206
239
protected function setError ($ type , $ text , $ params = [])
207
240
{
208
241
$ this ->errors [$ type ] = trans ($ text , $ params );
@@ -215,9 +248,9 @@ protected function getMeta()
215
248
{
216
249
if ($ this ->addMeta ) {
217
250
return [
218
- 'annotation ' => $ this ->extractAnnotation (),
251
+ 'annotation ' => $ this ->extractAnnotation (),
219
252
'formRequest ' => $ this ->extractFormRequest (),
220
- 'errors ' => $ this ->errors ,
253
+ 'errors ' => $ this ->errors ,
221
254
];
222
255
}
223
256
0 commit comments