8000 Simplify the routing by reducing the amount of calls to prepareRespon… · laravel/lumen-framework@a2882d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2882d9

Browse files
committed
Simplify the routing by reducing the amount of calls to prepareResponse. Middlewares will be fired in a similar manner to L5, prior to the router finding and handling a route.
Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
1 parent b7734d6 commit a2882d9

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

src/Application.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,13 +1086,15 @@ public function dispatch($request = null)
10861086
}
10871087

10881088
try {
1089-
if (isset($this->routes[$method.$pathInfo])) {
1090-
return $this->handleFoundRoute([true, $this->routes[$method.$pathInfo]['action'], []]);
1091-
}
1089+
return $this->sendThroughPipeline($this->middleware, function () use ($method, $pathInfo) {
1090+
if (isset($this->routes[$method.$pathInfo])) {
1091+
return $this->handleFoundRoute([true, $this->routes[$method.$pathInfo]['action'], []]);
1092+
}
10921093

1093-
return $this->handleDispatcherResponse(
1094-
$this->createDispatcher()->dispatch($method, $pathInfo)
1095-
);
1094+
return $this->handleDispatcherResponse(
1095+
$this->createDispatcher()->dispatch($method, $pathInfo)
1096+
);
1097+
});
10961098
} catch (Exception $e) {
10971099
return $this->sendExceptionToHandler($e);
10981100
}
@@ -1144,7 +1146,7 @@ protected function handleDispatcherResponse($routeInfo)
11441146
}
11451147

11461148
/**
1147-
* Handle a route that was found by the dispatcher.
1149+
* Handle a route found by the dispatcher.
11481150
*
11491151
* @param array $routeInfo
11501152
* @return mixed
@@ -1153,32 +1155,14 @@ protected function handleFoundRoute($routeInfo)
11531155
{
11541156
$this->currentRoute = $routeInfo;
11551157

1156-
// Pipe through global middleware...
1157-
if (count($this->middleware) > 0) {
1158-
return $this->prepareResponse($this->sendThroughPipeline($this->middleware, function () use ($routeInfo) {
1159-
return $this->prepareResponse($this->handleArrayBasedFoundRoute($routeInfo));
1160-
}));
1161-
}
1162-
1163-
return $this->handleArrayBasedFoundRoute($routeInfo);
1164-
}
1165-
1166-
/**
1167-
* Handle an array based route that was found by the dispatcher.
1168-
*
1169-
* @param array $routeInfo
1170-
* @return mixed
1171-
*/
1172-
protected function handleArrayBasedFoundRoute($routeInfo)
1173-
{
11741158
$action = $routeInfo[1];
11751159

11761160
// Pipe through route middleware...
11771161
if (isset($action['middleware'])) {
11781162
$middleware = $this->gatherMiddlewareClassNames($action['middleware']);
11791163

11801164
return $this->prepareResponse($this->sendThroughPipeline($middleware, function () use ($routeInfo) {
1181-
return $this->prepareResponse($this->callActionOnArrayBasedRoute($routeInfo));
1165+
return $this->callActionOnArrayBasedRoute($routeInfo);
11821166
}));
11831167
}
11841168

@@ -1324,10 +1308,14 @@ protected function gatherMiddlewareClassNames($middleware)
13241308
*/
13251309
protected function sendThroughPipeline(array $middleware, Closure $then)
13261310
{
1327-
return (new Pipeline($this))
1328-
->send($this->make('request'))
1329-
->through($middleware)
1330-
->then($then);
1311+
if (count($middleware) > 0) {
1312+
return (new Pipeline($this))
1313+
->send($this->make('request'))
1314+
->through($middleware)
1315+
->then($then);
1316+
}
1317+
1318+
return $then();
13311319
}
13321320

13331321
/**

0 commit comments

Comments
 (0)
0