-
Notifications
You must be signed in to change notification settings - Fork 89
Provide support for zend-stratigility 2.2.2 #293
Provide support for zend-stratigility 2.2.2 #293
Conversation
src/MiddlewareListener.php
Outdated
@@ -146,6 +147,19 @@ private function createPipeFromSpec( | |||
throw InvalidMiddlewareException::fromMiddlewareName($middlewareName); | |||
} | |||
|
|||
// Decorate double-pass middleware | |||
if (is_callable($middlewareToBePiped)) { | |||
$r = $this->getReflectionFunction($middlewareToBePiped); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use always meaningful names for variables. (loops are an exception)
But do we really need a reflection here? Smells like a hack.
A check of the interfaces and a simple method_exists
is maybe enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been taken from zend-stratigility
as a way of finding whether the middleware is a double-pass one, but I'll change it (it seemed like an over-complication to me, too).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been taken from zend-stratigility…
Ah, good to know.
it seemed like an over-complication to me, too
If the interface Interop\Http\ServerMiddleware\MiddlewareInterface
is not present but the current middleware is callable, then there is also no check for the parameters.
And the decorator DoublePassMiddlewareDecorator
includes also a test of the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've applied changes. Now we check whether the callable is missing the process
method or if it does not implement the Interop\Http\ServerMiddleware\MiddlewareInterface
, then wraps it, using the decorator.
Related to #290 |
41f6194
to
2d54d6e
Compare
src/MiddlewareListener.php
Outdated
if (is_callable($middlewareToBePiped) | ||
&& ( | ||
! $middlewaresToBePiped instanceof MiddlewareInterface | ||
|| ! method_exists($middlewaresToBePiped, 'process') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes should covered by unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new test testSuccessfullyDispatchesDoublePassMiddleware
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the test, but as I come to think about the if condition, doesn't it have to be x && !y && !z
instead of x && (!y || !z)
.. the first one makes more sense than the second one. Otherwise middlewares that are not an instance of MiddlewareInterface
, but have the process
method would be decorated, as well. Hm...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been digging a bit deeper into the failed tests and this PR. What you are basically doing is decorating the middleware in the tests to prevent the deprecation notice from stratigility. Doing it this way it will prevent notices for users as well. By the time zend-mvc is updated to support stratigility 3.0 their applications are not working anymore and they never saw a notice.
I think the correct way is to actually fix the middleware listener tests.
zend-mvc/test/MiddlewareListenerTest.php
Lines 62 to 69 in a8d4568
public function testSuccessfullyDispatchesMiddleware() | |
{ | |
$event = $this->createMvcEvent('path', function ($request, $response) { | |
$this->assertInstanceOf(ServerRequestInterface::class, $request); | |
$this->assertInstanceOf(ResponseInterface::class, $response); | |
$response->getBody()->write('Test!'); | |
return $response; | |
}); |
As you can see that test uses the double pass middleware. This should be changed to use the PSR-12 Middleware interface:
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface;
src/MiddlewareListener.php
Outdated
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface as PsrServerRequestInterface; | ||
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Psr\Http\Message\ResponseInterface
is imported twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was there before the changes, but I fixed it anyway.
To summarize the discussion we had last night on Slack:
zend-mvc is tested against stratigility ^2.0.1. The deprecation notices were added in 2.2.0. Since stratigility 3 requires php ^7.1 and zend-mvc requires ^5.6 || ^7.0, support for PSR-15 is out of the question. We tried to make expressive 2 compatible with all different middleware interfaces and it became pretty messy. I suggested this solution:
/cc @weierophinney |
@xtreamwayz |
The |
The v2 series of zend-mvc is no longer supported (see the LTS page for details). As such, we can ignore those versions entirely. Users will have to heed the notices and update their code accordingly. For v3, the approach @thexpand outlines seems sensible. |
No, I missed something... the zend-mvc 3 release :D |
So I guess this can be closed in favor of #295? |
I marked this PR as won't fix but will keep it open for now. Next week I will pull the stratigility support into satellite mvc package and reapply 2.2.2 changes there. |
Alternative solution to be provided by #308 |
Travis was failing on
latest
builds on all platforms. I've rancomposer update
and came across a deprecation error when I ran the unit tests. Callable double-pass middleware must be decorated in order to work with the new version ofzend-stratigility
.That's what this hotfix is all about - it includes the updated Composer files and the decorated middleware.