Skip to content

Commit

Permalink
Refactor routing middleware reflecting changes from slimphp#2398
Browse files Browse the repository at this point in the history
Fix copyright year
  • Loading branch information
l0gicgate committed May 17, 2018
1 parent 04f3816 commit ad2e7ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Slim/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim
* @copyright Copyright (c) 2011-2017 Josh Lockhart
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
namespace Slim;
Expand Down
2 changes: 1 addition & 1 deletion Slim/DispatcherResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim
* @copyright Copyright (c) 2011-2017 Josh Lockhart
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
namespace Slim;
Expand Down
38 changes: 22 additions & 16 deletions Slim/Middleware/RoutingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
namespace Slim\Middleware;

use Slim\Dispatcher;
use FastRoute\Dispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Exception\HttpMethodNotAllowedException;
Expand Down Expand Up @@ -41,6 +41,9 @@ public function __construct(RouterInterface $router)
* @param ResponseInterface $response PSR7 response
* @param callable $next Middleware callable
* @return ResponseInterface PSR7 response
*
* @throws HttpNotFoundException
* @throws HttpMethodNotAllowedException
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
Expand All @@ -53,29 +56,32 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
*
* @param ServerRequestInterface $request PSR7 server request
* @return ServerRequestInterface
*
* @throws HttpNotFoundException
* @throws HttpMethodNotAllowedException
*/
public function performRouting(ServerRequestInterface $request)
{
$dispatcherResults = $this->router->dispatch($request);
$routeStatus = $dispatcherResults->getRouteStatus();

if ($dispatcherResults->getRouteStatus() === Dispatcher::FOUND) {
$routeArguments = $dispatcherResults->getRouteArguments();
$route = $this->router->lookupRoute($dispatcherResults->getRouteHandler());
$route->prepare($request, $routeArguments);
switch ($routeStatus) {
default:
case Dispatcher::FOUND:
$routeArguments = $dispatcherResults->getRouteArguments();
$route = $this->router->lookupRoute($dispatcherResults->getRouteHandler());
$route->prepare($request, $routeArguments);
return $request
->withAttribute('route', $route)
->withAttribute('dispatcherResults', $dispatcherResults);

// Add route to the request's attributes
$request = $request->withAttribute('route', $route);
} elseif ($routeInfo[0] === Dispatcher::NOT_FOUND) {
$exception = new HttpNotFoundException($request);
throw $exception;
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
$exception = new HttpMethodNotAllowedException($request);
$exception->setAllowedMethods($routeInfo[1]);
throw $exception;
}
case Dispatcher::NOT_FOUND:
throw new HttpNotFoundException($request);

return $request->withAttribute('dispatcherResults', $dispatcherResults);
case Dispatcher::METHOD_NOT_ALLOWED:
$exception = new HttpMethodNotAllowedException($request);
$exception->setAllowedMethods($dispatcherResults->getAllowedMethods());
throw $exception;
}
}
}

0 comments on commit ad2e7ce

Please sign in to comment.