Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Refactor routing and dispatch logic #48

Conversation

weierophinney
Copy link
Member

This patch accomplishes several things.

First, it extracts the logic for producing a 405 response to new middleware, Zend\Expressive\Router\MethodNotAllowedMiddleware. This middleware checks for a route result that is due to method failure, and, in such a case, uses the composed response prototype. This change will allow users to substitute their own MethodNotAllowedMiddleware, allowing for templated solutions, or to omit it entirely, allowing fallback to the 404 handler.

Second, the RouteMiddleware was modified as follows:

  • it no longer requires a response prototype
  • it always injects the route result as a request attribute
  • it no longer produces a 405 response on its own

These changes mean the middleware does only what it says (routing).

Third, the Route class was modified to implement the PSR-15 MiddlewareInterface. When processed, it proxies to the composed middleware instance.

Fourth, the RouteResult was modified to implement the PSR-15 MiddlewareInterface. When processed, if it is a route failure, it delegates to the provided handler. If it is a successful route match, it proxies to the matched Route instance. The getMatchedMiddleware() method was removed, as it's essentially unnecessary; processing the route result accomplishes the same thing.

Fifth, the DispatchMiddleware was modified to process the RouteResult instance pulled from the request, instead of retrieving its composed middleware to process.

This patch accomplishes several things.

First, it extracts the logic for producing a 405 response to new
middleware, `Zend\Expressive\Router\MethodNotAllowedMiddleware`. This
middleware checks for a route result that is due to method failure, and,
in such a case, uses the composed response prototype.

Second, the `RouteMiddleware` was modified as follows:

- it no longer requires a response prototype
- it _always_ injects the route result as a request attribute
- it no longer produces a 405 response on its own

Third, the `Route` class was modified to implement the PSR-15
`MiddlewareInterface`. When processed, it proxies to the composed
middleware instance.

Fourth, the `RouteResult` was modified to implement the PSR-15
`MiddlewareInterface`. When processed, if it is a route failure, it
delegates to the provided handler. If it is a successful route match, it
proxies to the matched `Route` instance. The `getMatchedMiddleware()`
method was removed, as it's essentially unnecessary.

Fifth, the `DispatchMiddleware` was modified to process the
`RouteResult` instance pulled from the request, instead of retrieving
its composed middleware.
Copy link
Member

@michalbundyra michalbundyra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍


public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$routeResult = $request->getAttribute(RouteResult::class, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second param false can be removed, we are not using that is false, the same result we get with null (default value).

Since the default is to return a `null`, which evaluates as false-y for
purposes of conditionals, we can omit it.
Copy link
Member

@geerteltink geerteltink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Router\DispatchMiddleware;
use Zend\Expressive\Router\Route;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used anymore, same goes for the $responsePrototype property.

use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused.

@weierophinney weierophinney merged commit 168c89b into zendframework:release-3.0.0 Feb 13, 2018
weierophinney added a commit that referenced this pull request Feb 13, 2018
@weierophinney weierophinney deleted the feature/result-as-middleware branch February 13, 2018 15:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants