@@ -175,55 +175,90 @@ public function run(string $uri, string $position = 'before')
175
175
{
176
176
$ this ->initialize (strtolower ($ uri ));
177
177
178
- foreach ($ this ->filtersClass [$ position ] as $ className ) {
178
+ if ($ position === 'before ' ) {
179
+ $ result = $ this ->runBefore ($ this ->filtersClass [$ position ]);
180
+
181
+ // If the response object was sent back,
182
+ // then send it and quit.
183
+ if ($ result instanceof ResponseInterface) {
184
+ // short circuit - bypass any other filters
185
+ return $ result ;
186
+ }
187
+
188
+ return $ result ;
189
+ }
190
+
191
+ if ($ position === 'after ' ) {
192
+ $ result = $ this ->runAfter ($ this ->filtersClass [$ position ]);
193
+ }
194
+
195
+ return $ result ;
196
+ }
197
+
198
+ /**
199
+ * @return RequestInterface|ResponseInterface|string
200
+ */
201
+ private function runBefore (array $ filterClasses )
202
+ {
203
+ foreach ($ filterClasses as $ className ) {
179
204
$ class = new $ className ();
180
205
181
206
if (! $ class instanceof FilterInterface) {
182
207
throw FilterException::forIncorrectInterface (get_class ($ class ));
183
208
}
184
209
185
- if ($ position === 'before ' ) {
186
- $ result = $ class ->before (
187
- $ this ->request ,
188
- $ this ->argumentsClass [$ className ] ?? null
189
- );
190
-
191
- if ($ result instanceof RequestInterface) {
192
- $ this ->request = $ result ;
210
+ $ result = $ class ->before (
211
+ $ this ->request ,
212
+ $ this ->argumentsClass [$ className ] ?? null
213
+ );
193
214
194
- continue ;
195
- }
215
+ if ( $ result instanceof RequestInterface) {
216
+ $ this -> request = $ result ;
196
217
197
- // If the response object was sent back,
198
- // then send it and quit.
199
- if ($ result instanceof ResponseInterface) {
200
- // short circuit - bypass any other filters
201
- return $ result ;
202
- }
203
- // Ignore an empty result
204
- if (empty ($ result )) {
205
- continue ;
206
- }
218
+ continue ;
219
+ }
207
220
221
+ // If the response object was sent back,
222
+ // then send it and quit.
223
+ if ($ result instanceof ResponseInterface) {
224
+ // short circuit - bypass any other filters
208
225
return $ result ;
209
226
}
210
227
211
- if ($ position === 'after ' ) {
212
- $ result = $ class ->after (
213
- $ this ->request ,
214
- $ this ->response ,
215
- $ this ->argumentsClass [$ className ] ?? null
216
- );
228
+ // Ignore an empty result
229
+ if (empty ($ result )) {
230
+ continue ;
231
+ }
217
232
218
- if ( $ result instanceof ResponseInterface) {
219
- $ this -> response = $ result ;
233
+ return $ result;
234
+ }
220
235
221
- continue ;
222
- }
236
+ return $ this ->request ;
237
+ }
238
+
239
+ private function runAfter (array $ filterClasses ): ResponseInterface
240
+ {
241
+ foreach ($ filterClasses as $ className ) {
242
+ $ class = new $ className ();
243
+
244
+ if (! $ class instanceof FilterInterface) {
245
+ throw FilterException::forIncorrectInterface (get_class ($ class ));
246
+ }
247
+
248
+ $ result = $ class ->after (
249
+ $ this ->request ,
250
+ $ this ->response ,
251
+ $ this ->argumentsClass [$ className ] ?? null
252
+ );
253
+
254
+ if ($ result instanceof ResponseInterface) {
255
+ $ this ->response = $ result ;
256
+
257
+ continue ;
223
258
}
224
259
}
225
260
226
- return $ position === ' before ' ? $ this -> request : $ this ->response ;
261
+ return $ this ->response ;
227
262
}
228
263
229
264
/**
0 commit comments