-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Integrate PsrHttpMessageBridge
#52372
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
Closed
derrabus
wants to merge
1
commit into
symfony:7.1
from
derrabus:improvement/psr-http-message-integration
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Symfony/Bundle/FrameworkBundle/Resources/config/psr_http_message_bridge.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ServerRequestFactoryInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Message\UploadedFileFactoryInterface; | ||
use Symfony\Bridge\PsrHttpMessage\ArgumentValueResolver\PsrServerRequestResolver; | ||
use Symfony\Bridge\PsrHttpMessage\EventListener\PsrResponseListener; | ||
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; | ||
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; | ||
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface; | ||
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; | ||
|
||
return static function (ContainerConfigurator $container) { | ||
$container->services() | ||
->set('psr_http_message_bridge.http_foundation_factory', HttpFoundationFactory::class) | ||
->alias(HttpFoundationFactoryInterface::class, 'psr_http_message_bridge.http_foundation_factory') | ||
|
||
->set('psr_http_message_bridge.psr_http_factory', PsrHttpFactory::class) | ||
->args([ | ||
service(ServerRequestFactoryInterface::class)->nullOnInvalid(), | ||
service(StreamFactoryInterface::class)->nullOnInvalid(), | ||
service(UploadedFileFactoryInterface::class)->nullOnInvalid(), | ||
service(ResponseFactoryInterface::class)->nullOnInvalid(), | ||
]) | ||
->alias(HttpMessageFactoryInterface::class, 'psr_http_message_bridge.psr_http_factory') | ||
|
||
->set('psr_http_message_bridge.psr_server_request_resolver', PsrServerRequestResolver::class) | ||
->args([service('psr_http_message_bridge.psr_http_factory')]) | ||
->tag('controller.argument_value_resolver', ['priority' => -100]) | ||
|
||
->set('psr_http_message_bridge.psr_response_listener', PsrResponseListener::class) | ||
->args([ | ||
service('psr_http_message_bridge.http_foundation_factory'), | ||
]) | ||
->tag('kernel.event_subscriber') | ||
; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...rameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/PsrHttpMessageController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
|
||
final class PsrHttpMessageController | ||
{ | ||
public function __construct( | ||
private readonly ResponseFactoryInterface&StreamFactoryInterface $factory, | ||
) { | ||
} | ||
|
||
public function __invoke(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$responsePayload = json_encode([ | ||
'message' => sprintf('Hello %s!', $request->getQueryParams()['name'] ?? 'World'), | ||
], \JSON_THROW_ON_ERROR); | ||
|
||
return $this->factory->createResponse() | ||
->withHeader('Content-Type', 'application/json') | ||
->withBody($this->factory->createStream($responsePayload)) | ||
; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/PsrHttpMessageBridgeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; | ||
|
||
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; | ||
|
||
class PsrHttpMessageBridgeTest extends AbstractWebTestCase | ||
{ | ||
public function testBridgeIntegration() | ||
{ | ||
if ((new \ReflectionClass(PsrHttpFactory::class))->getConstructor()->getNumberOfRequiredParameters() > 0) { | ||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('PSR HTTP Message support cannot be enabled for version 2 or earlier. Please update symfony/psr-http-message-bridge to 6.4 or wire all services manually.'); | ||
} | ||
|
||
$client = $this->createClient(['test_case' => 'PsrHttpMessageBridge', 'root_config' => 'config.yml', 'debug' => true]); | ||
$client->request('GET', '/psr_http?name=Symfony'); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
$this->assertResponseHeaderSame('content-type', 'application/json'); | ||
$this->assertJsonStringEqualsJsonString('{"message":"Hello Symfony!"}', $client->getResponse()->getContent()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/PsrHttpMessageBridge/bundles.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; | ||
|
||
return [ | ||
new FrameworkBundle(), | ||
new TestBundle(), | ||
]; |
9 changes: 9 additions & 0 deletions
9
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/PsrHttpMessageBridge/config.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
- { resource: services.yml } | ||
|
||
framework: | ||
http_method_override: false | ||
profiler: ~ | ||
psr_http_message_bridge: | ||
enabled: true |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/PsrHttpMessageBridge/routing.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_emailtest_bundle: | ||
resource: '@TestBundle/Resources/config/routing.yml' |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/PsrHttpMessageBridge/services.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
nyholm_psr7_factory: | ||
class: Nyholm\Psr7\Factory\Psr17Factory | ||
|
||
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\PsrHttpMessageController: | ||
public: true | ||
arguments: ['@nyholm_psr7_factory'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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'm trying to run this PR to see how it goes. I did install a webapp-pack and added the psr bridge.
Then I created a controller and added a PSR RequestInterface as argument.
Because this priority is below
ServiceValueResolver
, I've an error telling me no such service.Then I changed the -100 to 50 (same level as RequestValueResolver), and the error is now "no PSR-17 factories have been provided."
This is consistent with the fact that the bridge is ... a bridge between Symfony and ... a library that needs to be installed.
A solution to this could be to require
php-http/discovery
+psr/http-factory-implementation
. That's the only way for us to provide a nice out of the box experience.-- or --
we keep the psr7-pack + its recipe? 👼