Skip to content

[11.x] Support auto-discovery of PSR-17 implementations #53711

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 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@
"league/flysystem-read-only": "^3.25.1",
"league/flysystem-sftp-v3": "^3.25.1",
"mockery/mockery": "^1.6.10",
"nyholm/psr7": "^1.2",
"orchestra/testbench-core": "^9.6",
"pda/pheanstalk": "^5.0.6",
"php-http/discovery": "^1.15",
"phpstan/phpstan": "^1.11.5",
"phpunit/phpunit": "^10.5.35|^11.3.6",
"predis/predis": "^2.3",
"resend/resend-php": "^0.10.0",
"symfony/cache": "^7.0.3",
"symfony/http-client": "^7.0.3",
"symfony/translation": "^7.0.3",
"symfony/psr-http-message-bridge": "^7.0.3"
"symfony/psr-http-message-bridge": "^7.0.3",
"symfony/translation": "^7.0.3"
},
"conflict": {
"mockery/mockery": "1.6.8",
Expand Down Expand Up @@ -184,8 +184,8 @@
"league/flysystem-read-only": "Required to use read-only disks (^3.25.1)",
"league/flysystem-sftp-v3": "Required to use the Flysystem SFTP driver (^3.25.1).",
"mockery/mockery": "Required to use mocking (^1.6).",
"nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).",
"pda/pheanstalk": "Required to use the beanstalk queue driver (^5.0).",
"php-http/discovery": "Required to use PSR-7 bridging features (^1.15).",
"phpunit/phpunit": "Required to use assertions and run tests (^10.5|^11.0).",
"predis/predis": "Required to use the predis connector (^2.3).",
"psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0).",
Expand All @@ -201,7 +201,8 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
"composer/package-versions-deprecated": true,
"php-http/discovery": false
}
},
"minimum-stability": "stable",
Expand Down
17 changes: 7 additions & 10 deletions src/Illuminate/Routing/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract;
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
use Illuminate\Support\ServiceProvider;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response as PsrResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\HttpFoundation\Response;

class RoutingServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -134,10 +133,8 @@ protected function registerRedirector()
protected function registerPsrRequest()
{
$this->app->bind(ServerRequestInterface::class, function ($app) {
if (class_exists(Psr17Factory::class) && class_exists(PsrHttpFactory::class)) {
$psr17Factory = new Psr17Factory;

return with((new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
if (class_exists(PsrHttpFactory::class)) {
return with((new PsrHttpFactory)
->createRequest($illuminateRequest = $app->make('request')), function (ServerRequestInterface $request) use ($illuminateRequest) {
if ($illuminateRequest->getContentTypeFormat() !== 'json' && $illuminateRequest->request->count() === 0) {
return $request;
Expand All @@ -149,7 +146,7 @@ protected function registerPsrRequest()
});
}

throw new BindingResolutionException('Unable to resolve PSR request. Please install the symfony/psr-http-message-bridge and nyholm/psr7 packages.');
throw new BindingResolutionException('Unable to resolve PSR request. Please install the "symfony/psr-http-message-bridge" package.');
});
}

Expand All @@ -161,11 +158,11 @@ protected function registerPsrRequest()
protected function registerPsrResponse()
{
$this->app->bind(ResponseInterface::class, function () {
if (class_exists(PsrResponse::class)) {
return new PsrResponse;
if (class_exists(PsrHttpFactory::class)) {
return (new PsrHttpFactory)->createResponse(new Response);
}

throw new BindingResolutionException('Unable to resolve PSR response. Please install the nyholm/psr7 package.');
throw new BindingResolutionException('Unable to resolve PSR response. Please install the "symfony/psr-http-message-bridge" package.');
});
}

Expand Down
7 changes: 5 additions & 2 deletions src/Illuminate/Routing/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@
},
"suggest": {
"illuminate/console": "Required to use the make commands (^11.0).",
"nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).",
"php-http/discovery": "Required to use PSR-7 bridging features (^1.15).",
"symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^7.0)."
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": false
}
},
"minimum-stability": "dev"
}
24 changes: 24 additions & 0 deletions tests/Integration/Routing/RoutingServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Tests\Integration\Routing;

use Orchestra\Testbench\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class RoutingServiceProviderTest extends TestCase
{
public function testResolvingPsrRequest()
{
$psrRequest = $this->app->make(ServerRequestInterface::class);

$this->assertInstanceOf(ServerRequestInterface::class, $psrRequest);
}

public function testResolvingPsrResponse()
{
$psrResponse = $this->app->make(ResponseInterface::class);

$this->assertInstanceOf(ResponseInterface::class, $psrResponse);
}
}