Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
Wire nyholm/psr7 as default factory for PSR-7
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 8, 2019
1 parent 1fdf591 commit 4544cab
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Expand Up @@ -8,15 +8,13 @@ cache:
- .phpunit

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

matrix:
include:
- php: 7.0
- php: 7.2
env: deps=low
fast_finish: true

Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration.php
Expand Up @@ -11,6 +11,7 @@

namespace Sensio\Bundle\FrameworkExtraBundle\DependencyInjection;

use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\NodeInterface;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function getConfigTreeBuilder()
->arrayNode('psr_message')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')->defaultValue(interface_exists('Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface') && class_exists('Zend\Diactoros\ServerRequestFactory'))->end()
->booleanNode('enabled')->defaultValue(interface_exists(HttpFoundationFactoryInterface::class))->end()
->end()
->end()
->arrayNode('templating')
Expand Down
7 changes: 4 additions & 3 deletions DependencyInjection/SensioFrameworkExtraExtension.php
Expand Up @@ -11,16 +11,17 @@

namespace Sensio\Bundle\FrameworkExtraBundle\DependencyInjection;

use Psr\Http\Message\StreamFactoryInterface;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage as SecurityExpressionLanguage;
use Zend\Diactoros\ServerRequestFactory;

/**
* @author Fabien Potencier <fabien@symfony.com>
Expand Down Expand Up @@ -148,8 +149,8 @@ public function load(array $configs, ContainerBuilder $container)
if ($config['psr_message']['enabled']) {
$loader->load('psr7.xml');

if (!class_exists(ServerRequestFactory::class)) {
$definitionsToRemove[] = 'sensio_framework_extra.psr7.argument_value_resolver.server_request';
if (!interface_exists(StreamFactoryInterface::class)) {
throw new LogicException('PSR-7 support cannot be enabled as the required dependencies are not installed. Try running "composer require nyholm/psr7".');
}
}

Expand Down
14 changes: 13 additions & 1 deletion Resources/config/psr7.xml
Expand Up @@ -5,7 +5,12 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="sensio_framework_extra.psr7.http_message_factory" class="Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory" public="false" />
<service id="sensio_framework_extra.psr7.http_message_factory" class="Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory" public="false">
<argument type="service" id="Psr\Http\Message\ServerRequestFactoryInterface" />
<argument type="service" id="Psr\Http\Message\StreamFactoryInterface" />
<argument type="service" id="Psr\Http\Message\UploadedFileFactoryInterface" />
<argument type="service" id="Psr\Http\Message\ResponseFactoryInterface" />
</service>
<service id="sensio_framework_extra.psr7.http_foundation_factory" class="Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory" public="false" />

<service id="sensio_framework_extra.psr7.argument_value_resolver.server_request" class="Sensio\Bundle\FrameworkExtraBundle\Request\ArgumentValueResolver\Psr7ServerRequestResolver" public="false">
Expand All @@ -17,5 +22,12 @@
<argument type="service" id="sensio_framework_extra.psr7.http_foundation_factory" />
<tag name="kernel.event_subscriber" />
</service>

<!-- provide default aliases for PSR-17 based on nyholm/psr7 -->
<service id="nyholm.psr7.psr17_factory" class="Nyholm\Psr7\Factory\Psr17Factory" public="false" />
<service id="Psr\Http\Message\ServerRequestFactoryInterface" alias="nyholm.psr7.psr17_factory" public="false" />
<service id="Psr\Http\Message\StreamFactoryInterface" alias="nyholm.psr7.psr17_factory" public="false" />
<service id="Psr\Http\Message\UploadedFileFactoryInterface" alias="nyholm.psr7.psr17_factory" public="false" />
<service id="Psr\Http\Message\ResponseFactoryInterface" alias="nyholm.psr7.psr17_factory" public="false" />
</services>
</container>
12 changes: 6 additions & 6 deletions Resources/doc/index.rst
Expand Up @@ -189,27 +189,27 @@ SensioFrameworkExtraBundle provides support for HTTP messages interfaces defined
in `PSR-7`_. It allows to inject instances of ``Psr\Http\Message\ServerRequestInterface``
and to return instances of ``Psr\Http\Message\ResponseInterface`` in controllers.

To enable this feature, `the HttpFoundation to PSR-7 bridge`_ and `Zend Diactoros`_ must be installed:
To enable this feature, `the HttpFoundation to PSR-7 bridge`_ and `autowiring aliases for PSR-17` must be installed:

.. code-block:: bash
$ composer require symfony/psr-http-message-bridge zendframework/zend-diactoros
$ composer require symfony/psr-http-message-bridge nyholm/psr7
Then, PSR-7 messages can be used directly in controllers like in the following code
snippet::

namespace AppBundle\Controller;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

class DefaultController
{
public function indexAction(ServerRequestInterface $request)
public function indexAction(ServerRequestInterface $request, ResponseFactoryInterface $responseFactory)
{
// Interact with the PSR-7 request

$response = new Response();
$response = $responseFactory->createResponse();
// Interact with the PSR-7 response

return $response;
Expand All @@ -222,4 +222,4 @@ and :class:`Symfony\\Component\\HttpFoundation\\Response` instances.
.. _`SensioFrameworkExtraBundle`: https://github.com/sensiolabs/SensioFrameworkExtraBundle
.. _`PSR-7`: http://www.php-fig.org/psr/psr-7/
.. _`the HttpFoundation to PSR-7 bridge`: https://github.com/symfony/psr-http-message-bridge
.. _`Zend Diactoros`: https://github.com/zendframework/zend-diactoros
.. _`autowiring aliases for PSR-17`: https://github.com/symfony/recipes/blob/master/nyholm/psr7/1.0/config/packages/nyholm_psr7.yaml
7 changes: 4 additions & 3 deletions composer.json
Expand Up @@ -11,6 +11,7 @@
}
],
"require": {
"php": ">=7.1.3",
"symfony/config": "^3.3|^4.0",
"symfony/framework-bundle": "^3.4|^4.0",
"symfony/dependency-injection": "^3.3|^4.0",
Expand All @@ -20,19 +21,19 @@
"require-dev": {
"symfony/expression-language": "^3.3|^4.0",
"symfony/finder": "^3.3|^4.0",
"symfony/psr-http-message-bridge": "^0.3",
"symfony/psr-http-message-bridge": "^1.1",
"symfony/security-bundle": "^3.3|^4.0",
"symfony/yaml": "^3.3|^4.0",
"symfony/twig-bundle": "^3.3|^4.0",
"twig/twig": "~1.12|~2.0",
"symfony/browser-kit": "^3.3|^4.0",
"symfony/phpunit-bridge": "^3.4.19|^4.1.8",
"symfony/dom-crawler": "^3.3|^4.0",
"zendframework/zend-diactoros": "^1.3",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/orm": "^2.5",
"symfony/monolog-bundle": "^3.2",
"symfony/monolog-bridge": "^3.0|^4.0"
"symfony/monolog-bridge": "^3.0|^4.0",
"nyholm/psr7": "^1.1"
},
"suggest": {
"symfony/psr-http-message-bridge": "To use the PSR-7 converters",
Expand Down

0 comments on commit 4544cab

Please sign in to comment.