-
-
Notifications
You must be signed in to change notification settings - Fork 57
Create PSR-7 messages using PSR-17 factories #43
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
Conversation
e59727a
to
a6d4b9c
Compare
$this->assertEquals(0, $file->getSize()); | ||
$this->assertEquals(UPLOAD_ERR_NO_FILE, $file->getError()); | ||
$this->assertFalse($file->getSize(), 'SplFile::getSize() returns false on error'); | ||
$this->assertInternalType('integer', $file->getClientSize()); |
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.
Since Symfony 4.1 the UploadedFile::getClientSize
method is deprecated and it just call SplFile::getSize
, so the tests are failing when PHP >= 7.1 because of the symfony/http-foundation package version.
I think that this assertion can be removed without any problem, and IMHO all these assertion about the UploadedFile
instance should be removed too.
What do you think?
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 removed that in #45
Ready for review. There are two different errors when testing in PHP >= 7.1, but they are not related to this PR. |
.travis.yml
Outdated
@@ -20,9 +20,13 @@ matrix: | |||
- php: 5.6 | |||
env: COMPOSER_OPTIONS="" SYMFONY_DEPRECATIONS_HELPER=weak | |||
- php: 7.0 | |||
- php: 7.1 | |||
- php: 7.2 | |||
- php: nightly | |||
- php: hhvm |
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.
hhvm can be dropped.
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.
Great! I requested some changes.
Lets wait until #45 is merged.
Factory/PsrHttpFactory.php
Outdated
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
|
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.
Remove extra space
Factory/PsrHttpFactory.php
Outdated
*/ | ||
class PsrHttpFactory implements HttpMessageFactoryInterface | ||
{ | ||
|
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.
Remove extra space
Factory/PsrHttpFactory.php
Outdated
$response = $response->withBody($stream); | ||
|
||
$headers = $symfonyResponse->headers->all(); | ||
|
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.
Remove this empty line. (Just my preference)
Factory/PsrHttpFactory.php
Outdated
} | ||
|
||
|
||
|
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.
Remove extra empty lines.
Factory/PsrHttpFactory.php
Outdated
} | ||
|
||
$protocolVersion = $symfonyResponse->getProtocolVersion(); | ||
if ('1.1' !== $protocolVersion) { |
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.
Remove this if
. I think we should always set protocol version
Factory/PsrHttpFactory.php
Outdated
->withCookieParams($symfonyRequest->cookies->all()) | ||
->withQueryParams($symfonyRequest->query->all()) | ||
->withParsedBody($symfonyRequest->request->all()) | ||
->withRequestTarget($symfonyRequest->getRequestUri()) |
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 line is not really needed. PSR7 implementation will fetch the request target from the URI
Tests/Factory/PsrHttpFactoryTest.php
Outdated
{ | ||
protected function getHttpMessageFactory() | ||
{ | ||
if (!class_exists('Http\Factory\Diactoros\ServerRequestFactory')) { |
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.
Is this really a PSR17 implementation? I did not know they already tagged it.
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.
Hehe. Oh sorry. That class if from the http-interop/http-factory-diactoros
package.... I created that package a few years back =)
Could you rebase on master now? The master branch is green on travis. |
75dc839
to
756cf14
Compare
756cf14
to
3635172
Compare
@Nyholm Travis is happy with this PR |
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.
Good. I like this. I just have some minor questions.
- php: 5.6 | ||
env: DEPENDENCIES="zendframework/zend-diactoros:^1.4.1" |
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.
Are these three lines needed? Global value is fine, right?
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.
Global value now includes http-interop/http-factory-diactoros:^1.0
which requires psr/http-factory:^1.0
which requires PHP >= 7.
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.
Okey. Good
- php: 7.2 | ||
env: DEPENDENCIES="symfony/lts:^3 symfony/force-lowest:~3.4.0 zendframework/zend-diactoros:^1.4.1" | ||
env: DEPENDENCIES="$DEPENDENCIES symfony/lts:^3 symfony/force-lowest:~3.4.0" |
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.
Are these two changes needed?
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 needed to add http-interop/http-factory-diactoros:^1.0
, so I prefer to make explicit that we are using here default dependencies combined with the symfony metapackages to force testing with LTS versions.
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.
But http-interop/http-factory-diactoros is already in the global. Right?
So no need to add it.
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.
But if we overwrite the dependencies here without including the previous value, this configuration will mark DiactorosFactoryTest
and PsrHttpFactoryTest
as skipped because zendframework/zend-diactoros
and http-interop/http-factory-diactoros
won't be installed.
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.
Hehe. I’ve been reviewing this PR multiple times but I’ve always read this change wrong.
Thank you for you explanation and your patience. Of course, this change is valid.
Factory/PsrHttpFactory.php
Outdated
@@ -75,7 +75,6 @@ public function createRequest(Request $symfonyRequest) | |||
->withCookieParams($symfonyRequest->cookies->all()) | |||
->withQueryParams($symfonyRequest->query->all()) | |||
->withParsedBody($symfonyRequest->request->all()) | |||
->withRequestTarget($symfonyRequest->getRequestUri()) |
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.
@Nyholm After removing this line, Travis is unhappy. The RequestInterface::getRequestTarget()
method should return the query string raw value or the urldecoded value?
See https://travis-ci.org/symfony/psr-http-message-bridge/jobs/413915386#L518
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.
Just viewed your comment in the test. Thanks.
$this->assertEquals('42', $queryParams['bar']['baz']); | ||
|
||
$requestTarget = $psrRequest->getRequestTarget(); | ||
$this->assertEquals('/testCreateRequest?foo=1&bar[baz]=42', $requestTarget); |
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.
You must “urldecode($requestTarget)”
It is a valid response from the psr 7 implementation.
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.
Thank you for this PR. I like it.
Is there anything I can do to get this PR merged? |
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.
LGTM thanks (with some minor comments)
Factory/PsrHttpFactory.php
Outdated
private $uploadedFileFactory; | ||
private $responseFactory; | ||
|
||
public function __construct( |
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.
the symfony code-style is to use single lines signatures
Factory/PsrHttpFactory.php
Outdated
|
||
foreach ($uploadedFiles as $key => $value) { | ||
if (null === $value) { | ||
$files[$key] = $this->uploadedFileFactory->createUploadedFile( |
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 think we prefer a long line here
Factory/PsrHttpFactory.php
Outdated
ob_start(function ($buffer) use ($stream) { | ||
$stream->write($buffer); | ||
|
||
return false; |
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 should return the empty string instead I suppose (please update DiacrotosFactory also it has the same "issue")
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.
Excellent. Thank you!
8aab743
to
dd81b4b
Compare
Thank you @ajgarlag. |
$request = $request->withHeader($name, $value); | ||
} | ||
|
||
if (PHP_VERSION_ID < 50600) { |
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.
the constant should be fully qualified, to allow the compiler to optimize this condition
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.
and do we even care about PHP 5.5 here ? AFAICT, the PSR factory does not support it
Use any implementation of the justs approved PSR-17 to build PSR-7 HTTP messages.