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

Remove FinalHandler, ErrorMiddlewareInterface, and Dispatch #67

Conversation

weierophinney
Copy link
Member

This pull request is issued against the dev-2.0.0 branch, and builds directly on the changes in #66, with the following additional changes:

  • Removes the FinalHandler class.
  • Removes the ErrorMiddlewareInterface.
  • Removes the Dispatch class; since all middleware is assumed to have the same signature now, and error handling can be done in middleware such as the ErrorHandler, it's redundant.
  • Updates the MiddlewareInterface to make the third argument required, and rename it to $next.
    • Updates all implementations to follow suit.
  • Updates Next:
    • Removes the $done constructor argument and property.
    • Removes the $dispatch property.
    • Inlines the code to dispatch middleware, as only one signature is now supported.
    • Returns the result of invoking middleware directly; it no longer verifies a response was returned. (This is no longer necessary, as it can be performed by classes such as the ErrorHandler.)
    • Returns the response provided at invocation directly if no more middleware is queued.
  • Updates MiddlewarePipe:
    • Passes the result of invoking the next layer of the queue to the provided $next argument.
    • If the next layer of the queue does not return a response, the original response is passed to the $next argument.

All tests were audited to check for features no longer supported, and documentation has been updated.

@weierophinney
Copy link
Member Author

I've merged #66 to develop, and rebased dev-2.0.0 and this branch from there, as it is a continuation of the features started on that PR.

Ready for review! Pinging @zendframework/community-review-team @michaelmoussa and @xtreamwayz !

@@ -112,6 +96,15 @@ function ($request, $response, $next)
}
```

> ### Do not pass an altered response
Copy link
Contributor

Choose a reason for hiding this comment

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

This threw me off a bit. The example demonstrated passing an altered response, but then we recommend not doing that without showing how to do it the recommended way.

Perhaps, after "or returning a brand new response entirely" we add:

[...] as shown below:

function ($request, $response, $next)
{
    $nextResponse = $next($request, $response);
    return $nextResponse->withAddedHeader('Cache-Control', [
        'public',
        'max-age=18600',
        's-maxage=18600',
    ]);
}

?

Copy link
Contributor

Choose a reason for hiding this comment

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

Note: The $updated = $response->addHeader('Cache-Control', [ line under Providing an altered response: should be ->withAddedHeader.

Copy link
Member

@geerteltink geerteltink Sep 29, 2016

Choose a reason for hiding this comment

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

$response->addHeader should be $response->withAddedHeader for all 3 examples under Providing an altered response, Providing both an altered request and response and Returning a response to complete the request.

Since this code is also in 1.3, a PR to fix this might be in place.

Copy link
Member

Choose a reason for hiding this comment

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

Created PR #69 to address this and some more.

Copy link
Member Author

Choose a reason for hiding this comment

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

@michaelmoussa Thanks for the feedback! I've updated the examples to only demonstrate manipulating the returned response, instead of the response at invocation.

Copy link
Member Author

Choose a reason for hiding this comment

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

@xtreamwayz thanks for the PR; merged, and I've rebased this PR with the changes.

@@ -163,56 +161,6 @@ And, if not calling `$next()`, returning the response instance:
return $response;
```

### Raising an error condition
Copy link
Contributor

Choose a reason for hiding this comment

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

I know error handling is covered elsewhere, but should we at least mention here that raising an error condition is as simple as throwing an exception, and then link to the documentation on error handling?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great feedback; I've re-instated this section, but now with an example demonstrating raising an exception from middleware.

These changes are tied together to an extent, as the `FinalHandler` was
required by `MiddlewarePipe` due to the fact that the `$next` (formerly
`$out`) argument was optional.

Accomplished here:

- Removed the `FinalHandler` implementation and its tests.
- Updated `MiddlewareInterface` to:
  - Rename `$out` to `$next`
  - Make `$next` required
  - Fix docblocks to reflect current interface
- Updated `MiddlewarePipe`:
  - Rename `$done` to `$next`
  - Make `$next` required
  - Remove marshaling of a `FinalHandler`, as `$next` is never null now.
  - Return the results of `$next` always, without type checking. That
    can be done in the `ErrorHandler` now.
- Updated both `ErrorHandler` and `NotFoundHandler` to make `$next`
  required.
- Updated `ErrorHandler` to no longer check for a null `$next`; it
  cannot happen now.
- Fixed any failing tests; most of these were due to missing `$next` arguments,
  or obsolete assumptions.
- Removed tests that were testing features of the `FinalHandler` and/or
  presence/absence of `$next`.
- Removed `MissingDelegateException` as it's no longer used.
This patch removes the Dispatch functionality, as it becomes obsolete
with the `ErrorHandler` addition from 1.3, and with the pending removal of
`ErrorMiddlewareInterface`. `Next` now inlines the following code:

```php
$middleware = $route->handler;
return $middleware($request, $response, $this);
```

This also involves two other changes to `Next`:

- Removal of the `$done` property and constructor argument.
- If no middleware remains in the queue, `Next` returns the passed
  response.
- MiddlewarePipe now executes `Next()`, but then passes the result
  to the `$next` argument provided to it. This allows invoking the next
  layer, if provided; the `NoopFinalHandler` will return the response
  verbatim in that instance.
- Removes references to `FinalHandler` and `ErrorMiddlewareInterface`
- Demonstrates piping `ErrorHandler` earlier
- Documents 2.0.0 changes in the migration guide
- Remove references to ErrorMiddlewareInterface.
- Remove references to FinalHandler.
@weierophinney weierophinney force-pushed the feature/final-handler-removal branch from b2f84db to 6b7e993 Compare September 29, 2016 13:18
- No longer demonstrating updating the response provided at invocation; instead,
  demonstrating manipulating the response *returned*.
- Demonstrate raising an exception to report an error condition.
@michaelmoussa
Copy link
Contributor

👍 LGTM now @weierophinney. Sad to see getArity(...) go, though. Such a cool name! :)

@weierophinney
Copy link
Member Author

Sad to see getArity(...) go

Me too, but as it's not used anymore, no need to keep it!

@weierophinney weierophinney merged commit ad5a739 into zendframework:dev-2.0.0 Sep 29, 2016
weierophinney added a commit that referenced this pull request Sep 29, 2016
@weierophinney weierophinney deleted the feature/final-handler-removal branch September 29, 2016 13:52
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