Skip to content

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

Merged
merged 1 commit into from
Aug 30, 2018

Conversation

ajgarlag
Copy link

@ajgarlag ajgarlag commented Aug 1, 2018

Use any implementation of the justs approved PSR-17 to build PSR-7 HTTP messages.

@ajgarlag ajgarlag force-pushed the feature/psr17-http-factories branch 5 times, most recently from e59727a to a6d4b9c Compare August 1, 2018 12:12
$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());
Copy link
Author

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?

Copy link
Member

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

@ajgarlag ajgarlag changed the title [WIP] Create PSR-7 messages using PSR-17 factories Create PSR-7 messages using PSR-17 factories Aug 1, 2018
@ajgarlag
Copy link
Author

ajgarlag commented Aug 1, 2018

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
Copy link

Choose a reason for hiding this comment

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

This was referenced Aug 4, 2018
Copy link
Member

@Nyholm Nyholm left a 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.

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;


Copy link
Member

Choose a reason for hiding this comment

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

Remove extra space

*/
class PsrHttpFactory implements HttpMessageFactoryInterface
{

Copy link
Member

Choose a reason for hiding this comment

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

Remove extra space

$response = $response->withBody($stream);

$headers = $symfonyResponse->headers->all();

Copy link
Member

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)

}



Copy link
Member

Choose a reason for hiding this comment

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

Remove extra empty lines.

}

$protocolVersion = $symfonyResponse->getProtocolVersion();
if ('1.1' !== $protocolVersion) {
Copy link
Member

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

->withCookieParams($symfonyRequest->cookies->all())
->withQueryParams($symfonyRequest->query->all())
->withParsedBody($symfonyRequest->request->all())
->withRequestTarget($symfonyRequest->getRequestUri())
Copy link
Member

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

{
protected function getHttpMessageFactory()
{
if (!class_exists('Http\Factory\Diactoros\ServerRequestFactory')) {
Copy link
Member

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.

Copy link
Member

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 =)

@ajgarlag
Copy link
Author

ajgarlag commented Aug 6, 2018

Thanks @Nyholm for the review. I'll wait for #45 to be merged.

@Nyholm
Copy link
Member

Nyholm commented Aug 8, 2018

Could you rebase on master now? The master branch is green on travis.

@ajgarlag ajgarlag force-pushed the feature/psr17-http-factories branch 2 times, most recently from 75dc839 to 756cf14 Compare August 8, 2018 15:54
@ajgarlag ajgarlag changed the title Create PSR-7 messages using PSR-17 factories [WIP] Create PSR-7 messages using PSR-17 factories Aug 8, 2018
@ajgarlag ajgarlag force-pushed the feature/psr17-http-factories branch from 756cf14 to 3635172 Compare August 8, 2018 21:12
@ajgarlag ajgarlag changed the title [WIP] Create PSR-7 messages using PSR-17 factories Create PSR-7 messages using PSR-17 factories Aug 8, 2018
@ajgarlag
Copy link
Author

ajgarlag commented Aug 8, 2018

@Nyholm Travis is happy with this PR

Copy link
Member

@Nyholm Nyholm left a 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"
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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"
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

@@ -75,7 +75,6 @@ public function createRequest(Request $symfonyRequest)
->withCookieParams($symfonyRequest->cookies->all())
->withQueryParams($symfonyRequest->query->all())
->withParsedBody($symfonyRequest->request->all())
->withRequestTarget($symfonyRequest->getRequestUri())
Copy link
Author

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

Copy link
Author

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);
Copy link
Member

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.

Copy link
Member

@Nyholm Nyholm left a 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.

@ajgarlag
Copy link
Author

Is there anything I can do to get this PR merged?

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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)

private $uploadedFileFactory;
private $responseFactory;

public function __construct(
Copy link
Member

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


foreach ($uploadedFiles as $key => $value) {
if (null === $value) {
$files[$key] = $this->uploadedFileFactory->createUploadedFile(
Copy link
Member

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

ob_start(function ($buffer) use ($stream) {
$stream->write($buffer);

return false;
Copy link
Member

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")

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Excellent. Thank you!

@fabpot fabpot force-pushed the feature/psr17-http-factories branch from 8aab743 to dd81b4b Compare August 30, 2018 16:24
@fabpot
Copy link
Member

fabpot commented Aug 30, 2018

Thank you @ajgarlag.

@fabpot fabpot merged commit dd81b4b into symfony:master Aug 30, 2018
fabpot added a commit that referenced this pull request Aug 30, 2018
This PR was squashed before being merged into the 1.0-dev branch (closes #43).

Discussion
----------

Create PSR-7 messages using PSR-17 factories

Use any implementation of the justs approved PSR-17 to build PSR-7 HTTP messages.

Commits
-------

dd81b4b Create PSR-7 messages using PSR-17 factories
$request = $request->withHeader($name, $value);
}

if (PHP_VERSION_ID < 50600) {
Copy link
Member

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

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants