Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x Refactor RouteGroup & Route #2612

Merged
merged 9 commits into from Apr 2, 2019

refactor group middleware appending logic in Route

  • Loading branch information...
l0gicgate committed Mar 21, 2019
commit b9d1a710a71a4d90360cae9ad557406ebd75948c
@@ -104,11 +104,10 @@ public function addMiddleware(MiddlewareInterface $middleware): RouteInterface;
/**
* Prepare the route for use
*
* @param ServerRequestInterface $request
* @param array $arguments
* @return RouteInterface
*/
public function prepare(ServerRequestInterface $request, array $arguments): RouteInterface;
public function prepare(array $arguments): RouteInterface;
/**
* Run route
@@ -74,7 +74,7 @@ public function performRouting(ServerRequestInterface $request): ServerRequestIn
$routeArguments = $routingResults->getRouteArguments();
$routeIdentifier = $routingResults->getRouteIdentifier() ?? '';
$route = $this->router->lookupRoute($routeIdentifier);
$route->prepare($request, $routeArguments);
$route->prepare($routeArguments);
return $request
->withAttribute('route', $route)
->withAttribute('routingResults', $routingResults);
@@ -107,6 +107,11 @@ class Route implements RouteInterface, RequestHandlerInterface
*/
protected $pattern;
/**
* @var bool
*/
protected $groupMiddlewareAppended = false;
/**
* Create new route
*
@@ -303,7 +308,7 @@ public function addMiddleware(MiddlewareInterface $middleware): RouteInterface
/**
* {@inheritdoc}
*/
public function prepare(ServerRequestInterface $request, array $arguments): RouteInterface
public function prepare(array $arguments): RouteInterface
{
// Remove temp arguments
$this->setArguments($this->savedArguments);
@@ -313,7 +318,14 @@ public function prepare(ServerRequestInterface $request, array $arguments): Rout
$this->setArgument($k, $v, false);
}
// Add middleware from Groups
return $this;
}
/**
* @return void
*/
protected function appendGroupMiddlewareToRoute(): void
{
$inner = $this->middlewareDispatcher;
$this->middlewareDispatcher = new MiddlewareDispatcher($inner, $this->container);
@@ -322,15 +334,18 @@ public function prepare(ServerRequestInterface $request, array $arguments): Rout
$group->appendMiddlewareToDispatcher($this->middlewareDispatcher);
}
return $this;
$this->groupMiddlewareAppended = true;
}
/**
* {@inheritdoc}
*/
public function run(ServerRequestInterface $request): ResponseInterface
{
// Traverse middleware stack and fetch updated response
if (!$this->groupMiddlewareAppended) {
$this->appendGroupMiddlewareToRoute();
}
return $this->middlewareDispatcher->handle($request);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.