Skip to content

PSR 15 #12907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Jan 29, 2019
Merged

PSR 15 #12907

merged 38 commits into from
Jan 29, 2019

Conversation

ADmad
Copy link
Member

@ADmad ADmad commented Jan 16, 2019

This implements PSR 15 support while maintaining the ability to use existing invokable/callable middlewares.

A notable change is you can no longer pass a response instance to Server::run() (though I am not sure it has much practical use). If we want to retain the ability to pass a custom response instance to app than we can do by adding a new argument to BaseApplication's constructor.

P.S. I haven't added/updated the docblocks so please avoid commenting on that for now.

@ADmad ADmad added this to the 4.0.0 milestone Jan 16, 2019
*/
protected $callables = [];
protected $middlewares = [];
Copy link
Member

Choose a reason for hiding this comment

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

I learned this in my PR cakephp/app#630:

Plural of middleware is middleware.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's acceptable to ignore grammatical rules for a variable name to make it apparent that this is a list of multiple middleware.

Also there are some source which state that in technical context the plural form is middlewares.

@ADmad
Copy link
Member Author

ADmad commented Jan 16, 2019

BC issue can occur if any existing double pass middleware manipulates the response instance before passing it to $next. The explanation for it is provided at the bottom here. Given that such usage is incorrect I don't think it's a problem, people should just fix their code (I updated such incorrect usage in tests).

@ADmad ADmad changed the title Psr 15 PSR 15 Jan 16, 2019
@garas
Copy link
Member

garas commented Jan 16, 2019

Function in DoublePassMiddleware::decorateHandler() could check if it gets same unmanipulated new Response() object from process() and at least warn about manipulated response.

@ADmad
Copy link
Member Author

ADmad commented Jan 16, 2019

Hmm... that sounds worth trying.

@markstory markstory added the http label Jan 17, 2019
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke(
public function process(
Copy link
Member

Choose a reason for hiding this comment

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

While not likely it is possible that userland applications have overridden __invoke(). We might want to have a shim __invoke method that calls process()

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO it would be acceptable to ask users to just update their application class for a new major release, for such a rarely required modification.

@ADmad
Copy link
Member Author

ADmad commented Jan 18, 2019

All tests pass now. I can start cleaning things up and updating the core middlewares to PSR 15 standard upon approval.

@garas I tried your idea of checking for prematurely modified response in DoublePassMiddleware but it caused IntegrationTestTrait tests to fail because of false positives. I searched a bit but couldn't find any such decorator with such a check in other frameworks/libs.

@codecov
Copy link

codecov bot commented Jan 19, 2019

Codecov Report

Merging #12907 into 4.x will increase coverage by <.01%.
The diff coverage is 98.88%.

Impacted file tree graph

@@             Coverage Diff              @@
##                4.x   #12907      +/-   ##
============================================
+ Coverage     93.15%   93.16%   +<.01%     
- Complexity    13202    13208       +6     
============================================
  Files           468      470       +2     
  Lines         32801    32810       +9     
============================================
+ Hits          30556    30566      +10     
+ Misses         2245     2244       -1
Impacted Files Coverage Δ Complexity Δ
src/Http/ActionDispatcher.php 100% <100%> (+2.85%) 15 <15> (-3) ⬇️
src/Http/Server.php 93.93% <100%> (-0.35%) 16 <4> (-2)
src/Http/Runner.php 100% <100%> (ø) 5 <3> (+2) ⬆️
.../Http/Middleware/DoublePassDecoratorMiddleware.php 100% <100%> (ø) 3 <3> (?)
src/Http/Middleware/BodyParserMiddleware.php 95.23% <100%> (ø) 15 <4> (ø) ⬇️
src/Http/Middleware/EncryptedCookieMiddleware.php 100% <100%> (ø) 15 <4> (ø) ⬇️
src/Http/Middleware/SecurityHeadersMiddleware.php 98.11% <100%> (ø) 14 <2> (ø) ⬇️
src/I18n/Middleware/LocaleSelectorMiddleware.php 100% <100%> (ø) 6 <5> (ø) ⬇️
src/Error/Middleware/ErrorHandlerMiddleware.php 98.41% <100%> (-0.08%) 23 <5> (ø)
src/Http/BaseApplication.php 94.73% <100%> (ø) 26 <1> (ø) ⬇️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d124a3a...de21860. Read the comment docs.

@codecov
Copy link

codecov bot commented Jan 19, 2019

Codecov Report

Merging #12907 into 4.x will decrease coverage by <.01%.
The diff coverage is 90.47%.

Impacted file tree graph

@@             Coverage Diff              @@
##                4.x   #12907      +/-   ##
============================================
- Coverage     93.14%   93.14%   -0.01%     
- Complexity    13203    13212       +9     
============================================
  Files           468      470       +2     
  Lines         32787    32809      +22     
============================================
+ Hits          30541    30559      +18     
- Misses         2246     2250       +4
Impacted Files Coverage Δ Complexity Δ
src/Http/MiddlewareQueue.php 91.93% <100%> (+0.26%) 30 <10> (+1) ⬆️
src/Http/Middleware/DoublePassMiddleware.php 100% <100%> (ø) 4 <4> (?)
src/Http/ActionDispatcher.php 92.3% <100%> (+0.41%) 21 <7> (+2) ⬆️
src/Http/Server.php 94.11% <100%> (-0.17%) 16 <4> (-2)
src/Http/BaseApplication.php 94.73% <100%> (ø) 26 <1> (ø) ⬇️
src/Http/Runner.php 100% <100%> (ø) 5 <2> (+2) ⬆️
src/Routing/Middleware/RoutingMiddleware.php 100% <100%> (ø) 15 <0> (ø) ⬇️
src/Http/Middleware/CallableMiddleware.php 33.33% <33.33%> (ø) 2 <2> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 86952ca...9c31296. Read the comment docs.

@ADmad
Copy link
Member Author

ADmad commented Jan 19, 2019

@markstory Stickler needs a smack 🙂. phpcs build is passing on travis and locally too for me.

@markstory
Copy link
Member

@ADmad Stickler only has the 'old' CakePHP code standard installed. I've not figured a clever way to have both versions installed.

ADmad added 2 commits January 23, 2019 13:44
…te a response.

HttpAllicationInterface now extends RequestHanlderInterface instead of MiddlewareInterface.
@ADmad ADmad force-pushed the psr-15 branch 2 times, most recently from dfc1d07 to b4414cb Compare January 24, 2019 19:11
@ADmad
Copy link
Member Author

ADmad commented Jan 25, 2019

All changes done as per feedback. This is ready for final review.

}

return $response;
$this->logException($request, $exception);
Copy link
Member

Choose a reason for hiding this comment

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

The empty catch block is a bit odd. Should this go in the catch?

}

// End of the queue
return $response;
return (new Response())->withStatus(500);
Copy link
Member

Choose a reason for hiding this comment

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

This could have a body that makes it easier for the user to know what went wrong. I don't see this as an error that should happen much in production so it could contain context on how to fix the problem.

*
* @return void
*/
public function testRunResponseReplace()
{
$this->markTestSkipped(
'Skip until we figure out a way to test for premature response modification in DoublePassMiddleware.'
Copy link
Member

Choose a reason for hiding this comment

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

Any idea on what to do here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't find a way to check and warn dev for this, so I think the test should be just dropped. The middleware example in manual does have a comment stating that response should be modified after calling $next.

@ADmad
Copy link
Member Author

ADmad commented Jan 26, 2019

@markstory Updates done as per your feedback.

@ADmad
Copy link
Member Author

ADmad commented Jan 27, 2019

@markstory I cleaned up ActionDispatcher.

ADmad added a commit to cakephp/docs that referenced this pull request Jan 27, 2019
Copy link
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@ADmad ADmad merged commit 29d9aea into 4.x Jan 29, 2019
@ADmad ADmad deleted the psr-15 branch January 29, 2019 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy