Part of #2492955: [meta] PSR-7 support
Blocked by #2497691: Include Symfony PSR-7 bridge library
Problem/Motivation
Right now, if a Controller returns a PSR-7 Response object Drupal doesn't know what to do with it and barfs. (Or rather, HttpKernel will raise an error.) That's not good, when there's a very simple conversion library available.
Proposed resolution
Add a View listener that checks for any PSR-7 Response object as a controller result, and uses the bridge library linked above to convert it to a Symfony Response. No muss, no fuss.
The listener should have one dependency, the converter mentioned above. It should check if the controller result is an instanceof RequestInterface, and if so, call that library and set the result as the response on the event. If not, do nothing.
Tagging as Novice as this should be a simple task. Ask Crell with any questions.
Remaining tasks
Do it.
API changes
None
Beta phase evaluation
Issue category | Task because nothing is broken per se so it's not a bug, but not an entirely new feature as PSR-7 is a natural extension of our Symfony and broader PHP integration. |
---|---|
Issue priority | Major because it's not release blocking (thus not critical) but strategically important for Drupal |
Disruption | None. There should be no API breakage from this issue, and the overlap of the patches here with work elsewhere is, as far as I'm aware, minimal to none. |
Comment | File | Size | Author |
---|---|---|---|
#28 | add_psr_7_to_symfony-2497693-28.patch | 7.88 KB | borisson_ |
#28 | interdiff.txt | 4.15 KB | borisson_ |
#24 | interdiff-24.txt | 3.67 KB | joshi.rohit100 |
#24 | psr-7-response-view-2497693-24.patch | 8.17 KB | joshi.rohit100 |
#22 | interdiff-22.txt | 1.13 KB | joshi.rohit100 |
Comments
Comment #1
marvin_B8 CreditAttribution: marvin_B8 commentedSolve the Issue
Comment #2
cilefen CreditAttribution: cilefen commentedComment #5
Crell CreditAttribution: Crell as a volunteer commentedThis could be simplified. if it is an instance of ResponseInterface, then do the setResponse() call. Else do nothing.
This should be KernelEvents::VIEW. TERMINATE is a different event.
That's a very long and unweidly service name. Please check the other event subscribers and follow their style. If all else fails, the class name makes a good starting point.
Please merge both patches into a single patch file. It doesn't work if each file is in its own diff.
We also need a test to verify that it works. One simple test should be sufficient. Have a controller in a test module that returns a ResponseInterface object, and make sure that when requesting that path everything works correctly. (Stop by #drupal-contribute if you need help setting that up.)
Also note that you won't be able to finish this issue until #2497691: Include Symfony PSR-7 bridge library is in, since that's how you'd have a PSR-7 response to return. :-) It's a bit jumping the gun to start with this issue, but thank you for doing so!
Also, check your whitespace. There are a number of lines that have trailing whitespace, and there should be one blank line between a method and the start of the docblock after it.
Thanks!
Comment #6
marvin_B8 CreditAttribution: marvin_B8 commentedSecond try =)
Comment #7
marvin_B8 CreditAttribution: marvin_B8 commentedComment #9
Crell CreditAttribution: Crell as a volunteer commentedcore.services.yml and RouterTest.php still have trailing whitespace. I suggest setting your IDE to auto-trim whitespace on save. It makes life a lot easier. :-)
Drupal convention is to use protected, not private, for object member variables. There are a few exceptions, but this isn't one of them.
This is a new class, so let's be consistent and use new-style [] syntax everywhere. I'm not sure if we need to specify a priority on this listener.
Obviously none of this will work until the library is available. :-)
Comment #10
marvin_B8 CreditAttribution: marvin_B8 commentedComment #11
joshi.rohit100Comment #13
joshi.rohit100Class name should be full namespaces
same here
And same for the other places as well.
Comment #14
marvin_B8 CreditAttribution: marvin_B8 commentedComment #15
marvin_B8 CreditAttribution: marvin_B8 commentedComment #17
Crell CreditAttribution: Crell at Palantir.net commentedThe blocking issue is in, so this can move forward.
Comment #20
joshi.rohit100Comment #21
dawehnerNice to see some progress going on!
We don't use private functions, let's make them protected
Comment #22
joshi.rohit100Comment #23
Crell CreditAttribution: Crell at Palantir.net commentedLookin' great! Just a few minor points, all of them nitpicks:
Note that not setting a priority means that it gets a priority of 0, which is pretty low. However, in this case we want that because this is a likely-rare subscriber, so only initializing it late (after the more common render-based listeners have a try) makes sense to me.
No change needed here, just calling it out.
Nit: Alias to PsrResponse, not PSRResponse.
I believe the version of Diactoros we included has utility subclasses available, so you can just use return new HtmlResponse('test123'); and it will handle creating the stream for you. It should still be a valid test of this patch.
(And that would mean no need to alias the class above, invalidating my previous comment.)
Nit: Please align the method calls vertically to make it easier to read. To wit:
(Same in a few other places.)
As long as we're going to need another reroll anyway, let's use new-style arrays exclusively.
Comment #24
joshi.rohit100Point 2, 3, 4, 5 is done.
point - 1 not sure what should be the priority here.
Comment #25
dawehnerYeah I would vote for something later like -32
Comment #26
Crell CreditAttribution: Crell at Palantir.net commentedI was more indicating that the current code was fine. The order is not critical as there's no dependencies, and the default puts it after most of our common stuff anyway I think.
So, RTBC! (Darling it's better with those four letters...)
Comment #27
alexpottConverts a PSR-7 response to a Symfony response.
psr should be in capitals and work should be works
These lines are not necessary.
This is a mock so needs a type hint like:
\Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
Might as well do this in the test setUp. The method to create the httpFoundationFactory is not really necessary.
$controller result is mixed.
@return \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent|\PHPUnit_Framework_MockObject_MockObject
Can this be
$this->once()
?->willReturn($controller_result)
Comment #28
borisson_This fixes the nits from #27.
Comment #29
marvin_B8 CreditAttribution: marvin_B8 commentedLooks good to me, so RTBC
Comment #32
Crell CreditAttribution: Crell at Palantir.net commentedLooks like bot being silly.
Comment #33
alexpott#27.1 still needs addressing.
Comment #34
borisson_Added beta evaluation from meta issue.
Comment #35
alexpottThanks for addressing my feedback and adding the beta evaluation to the issue summary. Committed 3349d70 and pushed to 8.0.x. Thanks!