Skip to content

Commit

Permalink
Merge branch 'mathmarques-lazy-route-finalize' into 3.x
Browse files Browse the repository at this point in the history
Closes #1632
  • Loading branch information
akrabat committed Nov 29, 2015
2 parents e269160 + d7085ac commit 48be9d2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 69 deletions.
3 changes: 1 addition & 2 deletions Slim/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ public function run($silent = false)
$request = $this->container->get('request');
$response = $this->container->get('response');

// Finalize routes here for middleware stack & ensure basePath is set
// Ensure basePath is set
$router = $this->container->get('router');
$router->finalize();
if (is_callable([$request->getUri(), 'getBasePath']) && is_callable([$router, 'setBasePath'])) {
$router->setBasePath($request->getUri()->getBasePath());
}
Expand Down
8 changes: 0 additions & 8 deletions Slim/Interfaces/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ interface RouterInterface
*/
public function map($methods, $pattern, $handler);


/**
* Finalize registered routes in preparation for dispatching
*
* NOTE: The routes can only be finalized once.
*/
public function finalize();

/**
* Dispatch router for HTTP request
*
Expand Down
11 changes: 11 additions & 0 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Route extends Routable implements RouteInterface
*/
protected $groups;

private $finalized = false;

/**
* Output buffering mode
*
Expand Down Expand Up @@ -89,6 +91,10 @@ public function __construct($methods, $pattern, $callable, $groups = [], $identi
*/
public function finalize()
{
if ($this->finalized) {
return;
}

$groupMiddleware = [];
foreach ($this->getGroups() as $group) {
$groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
Expand All @@ -99,6 +105,8 @@ public function finalize()
foreach ($this->getMiddleware() as $middleware) {
$this->addMiddleware($middleware);
}

$this->finalized = true;
}

/**
Expand Down Expand Up @@ -281,6 +289,9 @@ public function prepare(ServerRequestInterface $request, array $arguments)
*/
public function run(ServerRequestInterface $request, ResponseInterface $response)
{
// Finalise route now that we are about to run it
$this->finalize();

// Traverse middleware stack and fetch updated response
return $this->callMiddlewareStack($request, $response);
}
Expand Down
18 changes: 0 additions & 18 deletions Slim/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ class Router implements RouterInterface
*/
protected $routeGroups = [];

private $finalized = false;

/**
* @var \FastRoute\Dispatcher
*/
Expand Down Expand Up @@ -139,21 +137,6 @@ public function map($methods, $pattern, $handler)
return $route;
}

/**
* Finalize registered routes in preparation for dispatching
*
* NOTE: The routes can only be finalized once.
*/
public function finalize()
{
if (!$this->finalized) {
foreach ($this->getRoutes() as $route) {
$route->finalize();
}
$this->finalized = true;
}
}

/**
* Dispatch router for HTTP request
*
Expand All @@ -165,7 +148,6 @@ public function finalize()
*/
public function dispatch(ServerRequestInterface $request)
{
$this->finalize();
$uri = '/' . ltrim($request->getUri()->getPath(), '/');

return $this->createDispatcher()->dispatch(
Expand Down
Loading

0 comments on commit 48be9d2

Please sign in to comment.