Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
/zend-mvcPublic archive

Conversation

@asgrim
Copy link
Contributor

Taking issue#196 into play, this PR proposes to update theMiddlewareListener to useStratigility to allow piped middleware. This means themiddleware param can become anarray and everything is pushed into theMiddlewarePipe... as let's be fair, only allowing just a single middleware is only marginally useful.

For this to work,zendframework/zend-stratigility needs to become a requirement for those usingMiddlewareListener (I added it asrequire-dev for now), so could be seen as a BC break potentially; so happy to negotiate on how we'd like this added tocomposer.json. Otherwise, I believe (pending CI results) all the other tests still pass though.

@asgrim
Copy link
ContributorAuthor

Apparently I didn't have an up to datedevelop branch >.<

@asgrimasgrimforce-pushed themodify-middleware-listener-to-use-stratigility-pipe branch from0e0a650 to2f947e8CompareFebruary 19, 2017 12:39
@asgrim
Copy link
ContributorAuthor

Rebased correctly now \o/

Ocramius
Ocramius previously requested changesFeb 23, 2017
$psr7Request,
$psr7ResponsePrototype,
function (PsrServerRequestInterface$request,PsrResponseInterface$response) {
thrownewReachedFinalHandlerException(
Copy link
Member

Choose a reason for hiding this comment

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

@weierophinney are you OK with moving this to a static named constructor instead? Asking because consistency...

Choose a reason for hiding this comment

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

I am, but I'm not convinced it's needed, as there's a static message with no dynamic substitutions, and no additional behavior.

Choose a reason for hiding this comment

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

One potential refactor I see here is to instead defineReachedFinalHandlerException as follows:

class ReachedFinalHandlerExceptionextends RuntimeException{publicstaticfunctioncreateFinalHandler()    {returnnewself('Reached the final handler for middleware pipe - check the pipe configuration');    }publicfunction__invoke()    {throw$this;    }}

This would then allow you to do the following above:

$return =$pipe($psr7Request,$psr7Response, ReachedFinalHandlerException::createFinalHandler());

Not sure if that's abusing inheritance and SRP, though. 😁

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Yikes, I'm not really sure about the exception itself being the final handler, although I gotta say it's quite clever... does feel a bit wrong. I'll just refactor to named constructor for now and if you want to change it to be the actual final handler, I'm happy to :)


$middlewareName ='noMiddlewarePiped';
$middlewareToBePiped =null;
foreach ($middlewaresToBePipedas$middlewareToBePiped) {
Copy link
Member

Choose a reason for hiding this comment

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

Segregate to a private method.

Copy link
Member

Choose a reason for hiding this comment

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

Probably should just return$pipe in the private method.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Done

$pipe =newMiddlewarePipe();
$pipe->setResponsePrototype($psr7ResponsePrototype);

$middlewaresToBePiped = !is_array($middleware) ? [$middleware] :$middleware;
Copy link
Member

Choose a reason for hiding this comment

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

Don't negate: invert the ternary please

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Done

$middlewareName ='noMiddlewarePiped';
$middlewareToBePiped =null;
foreach ($middlewaresToBePipedas$middlewareToBePiped) {
$middlewareName =is_string($middlewareToBePiped) ?$middlewareToBePiped :get_class($middlewareToBePiped);
Copy link
Member

Choose a reason for hiding this comment

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

Becauseget_class() is stupid this check is insufficient

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Didn't change this from the original code...

foreach ($middlewaresToBePipedas$middlewareToBePiped) {
$middlewareName =is_string($middlewareToBePiped) ?$middlewareToBePiped :get_class($middlewareToBePiped);

if (is_string($middlewareToBePiped) &&$serviceManager->has($middlewareToBePiped)) {
Copy link
Member

Choose a reason for hiding this comment

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

is_string() check is duplicate

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Not sure what to do about it; it's just the flexibility around using a string as the middleware (load from container), or a callable (not ideal, but allowed)

if (is_string($middlewareToBePiped) &&$serviceManager->has($middlewareToBePiped)) {
$middlewareToBePiped =$serviceManager->get($middlewareToBePiped);
}
if (!is_callable($middlewareToBePiped)) {
Copy link
Member

Choose a reason for hiding this comment

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

Should probably alsoNOT be a string?

Copy link
Member

Choose a reason for hiding this comment

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

An example is an incompatible function name being passed in.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

If it's a string, it won't be a string any more. Also just following same logic that was already there...

$event->setController($middlewareName);
$event->setControllerClass(get_class($middleware));
if (null !==$middlewareToBePiped) {
$event->setControllerClass(get_class($middlewareToBePiped));
Copy link
Member

Choose a reason for hiding this comment

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

You always assume that the last piped middleware is the "responding" one here... Probably needs documenting/commenting

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Removed these as there's no longer just one middleware, so doesn't make sense. I guess this causes a definite BC break which is kinda rubbish though?

$middlewaresToBePiped = !is_array($middleware) ? [$middleware] :$middleware;

$middlewareName ='noMiddlewarePiped';
$middlewareToBePiped =null;
Copy link
Member

Choose a reason for hiding this comment

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

Can an empty pipeline be considered valid?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I don't see why not; you'll just reach the final handler anyway...

@asgrim
Copy link
ContributorAuthor

@Ocramius some changes applied as requested.

@asgrim
Copy link
ContributorAuthor

shakes fist at PHP 5.6 support

@asgrimasgrim removed their assignmentMar 6, 2017
composer.json Outdated
"fabpot/php-cs-fixer":"1.7.*",
"phpunit/phpunit":"^4.5"
"phpunit/phpunit":"^4.5",
"zendframework/zend-stratigility":"^2.0"

Choose a reason for hiding this comment

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

This should likely be^2.0.1, but I can take care of that during merge.

$psr7Request,
$psr7ResponsePrototype,
function (PsrServerRequestInterface$request,PsrResponseInterface$response) {
thrownewReachedFinalHandlerException(

Choose a reason for hiding this comment

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

I am, but I'm not convinced it's needed, as there's a static message with no dynamic substitutions, and no additional behavior.

$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
$event->setError($application::ERROR_EXCEPTION);
$event->setController($middlewareName);
$event->setControllerClass(get_class($middleware));

Choose a reason for hiding this comment

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

Why are these lines removed?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

There is potentially no longer just "one" middleware name... how do we decide which is the failed one? First? Last? IIRC I don't think there's any way (outside of the exception bubble here)

Choose a reason for hiding this comment

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

Ah, right - that makes sense, then.


if (is_string($middlewareToBePiped) &&$serviceLocator->has($middlewareToBePiped)) {
$middlewareToBePiped =$serviceLocator->get($middlewareToBePiped);
}

Choose a reason for hiding this comment

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

This needs to change to be:

if (!$middlewareToBePipedinstanceof \Http\Interop\ServerMiddleware\MiddlewareInterface    && !is_callable($middlewareToBePiped))

as zend-stratigility 2.0 allows either of those types. Testing only foris_callable() means that http-interop middleware cannot be used!

Choose a reason for hiding this comment

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

Once you make those changes, you'll also need to add tests for piping http-interop middleware.

$psr7Request,
$psr7ResponsePrototype,
function (PsrServerRequestInterface$request,PsrResponseInterface$response) {
thrownewReachedFinalHandlerException(

Choose a reason for hiding this comment

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

One potential refactor I see here is to instead defineReachedFinalHandlerException as follows:

class ReachedFinalHandlerExceptionextends RuntimeException{publicstaticfunctioncreateFinalHandler()    {returnnewself('Reached the final handler for middleware pipe - check the pipe configuration');    }publicfunction__invoke()    {throw$this;    }}

This would then allow you to do the following above:

$return =$pipe($psr7Request,$psr7Response, ReachedFinalHandlerException::createFinalHandler());

Not sure if that's abusing inheritance and SRP, though. 😁

@weierophinney
Copy link
Member

Also,@asgrim — You'll need to rebase against latest develop branch, as I pushed a bunch of QA tooling changes earlier (PHPUnit 5.7/6.0 support, in particular, as well as zend-coding-standards integration).

@asgrimasgrim self-assigned thisApr 28, 2017
@asgrimasgrimforce-pushed themodify-middleware-listener-to-use-stratigility-pipe branch fromb8511d3 to7d89900CompareApril 28, 2017 16:04
{
$expectedOutput =uniqid('expectedOutput',true);

$event =$this->createMvcEvent('path',newclass($expectedOutput)implements MiddlewareInterface {
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

bah, doesn't work on PHP 5.6 - I thoughtdevelop branches were min PHP 7.1 now? Maybe I didn't read properly?

@asgrim
Copy link
ContributorAuthor

@weierophinney all updated, thanks for review!

@asgrimasgrim removed their assignmentApr 30, 2017
…g-strategyFixes@param &@return DefaultRenderingStrategy typo at HttpDefaultRenderingStrategyFactory
Forward portzendframework#237Conflicts:src/Service/HttpDefaultRenderingStrategyFactory.php
Copy link
Member

@weierophinneyweierophinney left a comment

Choose a reason for hiding this comment

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

Few last nit-picks, but looks great!

$psr7Request =$psr7Request->withAttribute($key,$value);
}
$return =$middleware($psr7Request, Psr7Response::fromZend($response));
$return =$pipe(
Copy link
Member

Choose a reason for hiding this comment

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

@asgrim So sorry I didn't notice this earlier, but... If we're pinning to Stratigility 2, we should likely use the http-interopprocess() method here instead of__invoke(), which means using aDelegateInterface instead of a callable.

May I suggest the following:

$pipe->setResponsePrototype($psr7ResponsePrototype);$return =$pipe->process($psr7Request,new \Zend\Stratigility\Delegate\CallableDelegateDecorator(function (PsrServerRequestInterface$request,PsrResponseInterface$response) {throw ReachedFinalHandlerException::create();    }));

That would accomplish the same as what you have here, and be forwards compatible.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, this seems reasonable, though I note that__invoke does the wrapping in theCallableDelegateDecorator for us, but I have no strong feelings either way. If__invoke is eventually going to go, then yes the switch makes sense!

setResponsePrototype is already called increatePipeFromSpec btw, so I don't think needs calling again (unless I missed something)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, okay, nice.

As noted, usingprocess() will be forwards compatible, meaning that when Stratigility adopts a finalized PSR-15, we likely can just add an|| to the constraint and not need any actual code changes. I figure, do it once. 😄

asgrim reacted with thumbs up emoji

namespaceZend\Mvc\Exception;

finalclass MiddlewareNotCallableExceptionextends RuntimeException
Copy link
Member

Choose a reason for hiding this comment

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

Since we're allowing either callable or http-interop middleware, perhaps we should change this toInvalidMiddlewareException instead?

asgrim reacted with thumbs up emoji
$pipe =newMiddlewarePipe();
$pipe->setResponsePrototype($responsePrototype);
foreach ($middlewaresToBePipedas$middlewareToBePiped) {
if (null ===$middlewareToBePiped) {
Copy link
Member

Choose a reason for hiding this comment

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

If we rename the exception toInvalidMiddlewareException, we could also add another named constructor:::becauseNull() (obviously, choose something better!).

asgrim reacted with thumbs up emoji
@weierophinneyweierophinney dismissedOcramius’sstale reviewMay 1, 2017 15:58

Requested changes incorporated.

Copy link
Member

@weierophinneyweierophinney left a comment

Choose a reason for hiding this comment

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

Per discussion in Slack, I'll perform the last requested changes now and merge.

Thanks,@asgrim!

…stener-to-use-stratigility-pipeModify middleware listener to use stratigility pipe
Also, adds a `fromNull()` method, allowing it to be used in cases wherea `null` value is provided for middleware. This means that `null`middleware is handled exactly the same way as any other invalidmiddleware type, which required changes to several test expectations.
Instead of `__invoke()`, for forwards compatibility once PSR-15 isratified.
@weierophinneyweierophinney merged commit6a4bf9f intozendframework:developMay 1, 2017
weierophinney added a commit that referenced this pull requestMay 1, 2017
weierophinney added a commit that referenced this pull requestMay 1, 2017
@weierophinney
Copy link
Member

Thanks,@asgrim

@asgrimasgrim deleted the modify-middleware-listener-to-use-stratigility-pipe branchMay 1, 2017 16:40
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

2 more reviewers

@weierophinneyweierophinneyweierophinney approved these changes

@OcramiusOcramiusOcramius left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.1.0

Development

Successfully merging this pull request may close these issues.

4 participants

@asgrim@weierophinney@Ocramius@samsonasik

[8]ページ先頭

©2009-2025 Movatter.jp