Skip to content

Commit

Permalink
Don't autowire controllers if no PSR-7 implementation is found, close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal authored Mar 23, 2021
1 parent c9a2385 commit 9c828e6
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 17 deletions.
21 changes: 21 additions & 0 deletions .github/nyholm_psr7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
# Register nyholm/psr7 services for autowiring with PSR-17 (HTTP factories)
Psr\Http\Message\RequestFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\ResponseFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\ServerRequestFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\StreamFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\UploadedFileFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\UriFactoryInterface: '@nyholm.psr7.psr17_factory'

# Register nyholm/psr7 services for autowiring with HTTPlug factories
Http\Message\MessageFactory: '@nyholm.psr7.httplug_factory'
Http\Message\RequestFactory: '@nyholm.psr7.httplug_factory'
Http\Message\ResponseFactory: '@nyholm.psr7.httplug_factory'
Http\Message\StreamFactory: '@nyholm.psr7.httplug_factory'
Http\Message\UriFactory: '@nyholm.psr7.httplug_factory'

nyholm.psr7.psr17_factory:
class: Nyholm\Psr7\Factory\Psr17Factory

nyholm.psr7.httplug_factory:
class: Nyholm\Psr7\Factory\HttplugFactory
19 changes: 19 additions & 0 deletions .github/psr_http_message_bridge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:
_defaults:
autowire: true
autoconfigure: true

Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface: '@Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory'

Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface: '@Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory'

Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory: null
Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory: null

# Uncomment the following line to allow controllers to receive a
# PSR-7 server request object instead of an HttpFoundation request
Symfony\Bridge\PsrHttpMessage\ArgumentValueResolver\PsrServerRequestResolver: null

# Uncomment the following line to allow controllers to return a
# PSR-7 response object instead of an HttpFoundation response
Symfony\Bridge\PsrHttpMessage\EventListener\PsrResponseListener: null
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
node-version: ${{ env.NODE_VERSION }}

- run: composer install --prefer-dist --no-interaction --no-progress
- run: cp .github/psr_http_message_bridge.yaml fixtures/applications/Symfony/config/packages
- run: cp .github/nyholm_psr7.yaml fixtures/applications/Symfony/config/packages
- run: composer require symfony/psr7-pack # To make PHPStan and me happy, it prevent me to write stubs for PHPStan

- run: composer validate --strict

Expand Down Expand Up @@ -123,6 +126,9 @@ jobs:
node-version: ${{ env.NODE_VERSION }}

- run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
- run: cp .github/psr_http_message_bridge.yaml fixtures/applications/Symfony/config/packages
- run: cp .github/nyholm_psr7.yaml fixtures/applications/Symfony/config/packages
- run: composer require symfony/psr7-pack

- run: composer server-start

Expand Down
70 changes: 58 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,68 @@ If you want to add more assertions but can't extend from `MailerContext`, you ca

##### Installing a PSR-7 implementation

First, you need to install a PSR-7 implementation, e.g. [nyholm/psr7](https://github.com/Nyholm/psr7) :
You can install `symfony/psr7-pack` which will install all you need:

```console
$ composer require nyholm/psr7
```
- [nyholm/psr7](https://github.com/Nyholm/psr7) for the PSR-7 implementation
- [symfony/psr-http-message-bridge](https://github.com/symfony/psr-http-message-bridge) for the Symfony integration

Then you need to install the [Sensio FrameworkExtraBundle](https://github.com/sensiolabs/SensioFrameworkExtraBundle) and [Symfony PSR-7 Bridge](https://github.com/symfony/psr-http-message-bridge):
Those dependencies will make sure that the PSR-7 compatible controllers provided by SymfonyMailerTesting will be working with Symfony.

```console
$ composer require sensio/framework-extra-bundle
$ composer require --dev symfony/psr-http-message-bridge
```
##### Configuring Symfony

Those dependencies will make sure that the PSR-7 compatible [`MailerController.php`](./src/MailerController.php) will be compatible with Symfony.
You must configuring the Symfony PSR HTTP Message Bridge and the PSR-7 integration.

The following files are automatically created by Symfony Flex but can require some configuration:
- `config/packages/psr_http_message_bridge.yaml`, **ensure services `Symfony\Bridge\PsrHttpMessage\ArgumentValueResolver\PsrServerRequestResolver`
and `Symfony\Bridge\PsrHttpMessage\EventListener\PsrResponseListener` are enabled**:
```yaml
# config/packages/psr_http_message_bridge.yaml
services:
_defaults:
autowire: true
autoconfigure: true
Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface: '@Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory'
Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface: '@Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory'
Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory: null
Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory: null
# Uncomment the following line to allow controllers to receive a
# PSR-7 server request object instead of an HttpFoundation request
Symfony\Bridge\PsrHttpMessage\ArgumentValueResolver\PsrServerRequestResolver: null
# Uncomment the following line to allow controllers to return a
# PSR-7 response object instead of an HttpFoundation response
Symfony\Bridge\PsrHttpMessage\EventListener\PsrResponseListener: null
```
- `config/packages/nyholm_psr7.yaml`:
```yaml
# config/packages/nyholm_psr7.yaml
services:
# Register nyholm/psr7 services for autowiring with PSR-17 (HTTP factories)
Psr\Http\Message\RequestFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\ResponseFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\ServerRequestFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\StreamFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\UploadedFileFactoryInterface: '@nyholm.psr7.psr17_factory'
Psr\Http\Message\UriFactoryInterface: '@nyholm.psr7.psr17_factory'
# Register nyholm/psr7 services for autowiring with HTTPlug factories
Http\Message\MessageFactory: '@nyholm.psr7.httplug_factory'
Http\Message\RequestFactory: '@nyholm.psr7.httplug_factory'
Http\Message\ResponseFactory: '@nyholm.psr7.httplug_factory'
Http\Message\StreamFactory: '@nyholm.psr7.httplug_factory'
Http\Message\UriFactory: '@nyholm.psr7.httplug_factory'
nyholm.psr7.psr17_factory:
class: Nyholm\Psr7\Factory\Psr17Factory
nyholm.psr7.httplug_factory:
class: Nyholm\Psr7\Factory\HttplugFactory
```

##### Configuring Symfony

Then you have to import the routes:

Expand All @@ -112,7 +158,7 @@ symfony_mailer_testing:
resource: '@SymfonyMailerTestingBundle/Resources/config/routing.yaml'
```

And configure the firewall to disallow the firewall on `/_symfony_mailer_testing`:
And disable the firewall and access control on `/_symfony_mailer_testing`:

```diff
firewalls:
Expand Down
Binary file modified bin/symfony
Binary file not shown.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@
"behat/behat": "^3.6",
"friends-of-behat/symfony-extension": "^2.1",
"friendsofphp/php-cs-fixer": "^2.16",
"nyholm/psr7": "^1.2",
"phpspec/phpspec": "^6.1",
"phpstan/phpstan": "^0.12.23",
"phpstan/phpstan-phpunit": "^0.12.8",
"phpstan/phpstan-strict-rules": "^0.12.2",
"phpstan/phpstan-symfony": "^0.12.6",
"phpstan/phpstan-webmozart-assert": "^0.12.4",
"phpunit/phpunit": "^8.5.2",
"sensio/framework-extra-bundle": "^5.5",
"symfony/framework-bundle": "^4.4.1 || ^5.0",
"symfony/psr-http-message-bridge": "^2.0"
"symfony/framework-bundle": "^4.4.1 || ^5.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion fixtures/applications/Symfony/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle::class => ['test' => true],
Kocal\SymfonyMailerTesting\Bridge\Symfony\SymfonyMailerTestingBundle::class => ['test' => true],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:
_defaults:
autowire: true
autoconfigure: true

Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface: '@Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory'

Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface: '@Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory'

Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory: null
Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory: null

# Uncomment the following line to allow controllers to receive a
# PSR-7 server request object instead of an HttpFoundation request
Symfony\Bridge\PsrHttpMessage\ArgumentValueResolver\PsrServerRequestResolver: null

# Uncomment the following line to allow controllers to return a
# PSR-7 response object instead of an HttpFoundation response
Symfony\Bridge\PsrHttpMessage\EventListener\PsrResponseListener: null
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kocal\SymfonyMailerTesting\Bridge\Symfony\DependencyInjection;

use Kocal\SymfonyMailerTesting\Controller\MailerController;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -19,5 +20,9 @@ public function load(array $configs, ContainerBuilder $container): void
);

$loader->load('services.yaml');

if (!interface_exists(\Psr\Http\Message\ResponseInterface::class)) {
$container->removeDefinition(MailerController::class);
}
}
}

0 comments on commit 9c828e6

Please sign in to comment.