Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Runtime] Add support for PSR-7, PSR-15 and PSR-17 #40436

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
wants to merge 4 commits into from
Closed
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
5 changes: 4 additions & 1 deletion 5 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@
"masterminds/html5": "^2.6",
"monolog/monolog": "^1.25.1|^2",
"nyholm/psr7": "^1.0",
"nyholm/psr7-server": "^1.0",
"paragonie/sodium_compat": "^1.8",
"pda/pheanstalk": "^4.0",
"php-http/httplug": "^1.0|^2.0",
"predis/predis": "~1.1",
"psr/http-client": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/simple-cache": "^1.0",
"egulias/email-validator": "^2.1.10|^3.1",
"symfony/mercure-bundle": "^0.2",
Expand All @@ -148,7 +150,8 @@
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
"twig/cssinliner-extra": "^2.12|^3",
"twig/inky-extra": "^2.12|^3",
"twig/markdown-extra": "^2.12|^3"
"twig/markdown-extra": "^2.12|^3",
"laminas/laminas-httphandlerrunner": "^1.2"
},
"conflict": {
"async-aws/core": "<1.5",
Expand Down
108 changes: 108 additions & 0 deletions 108 src/Symfony/Component/Runtime/PsrRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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\Runtime;

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Runtime\Runner\Psr\LaminasEmitter;

/**
* A runtime that supports PSR-7, PSR-15 and PSR-17.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*
* @experimental in 5.3
*/
class PsrRuntime extends GenericRuntime
{
/**
* @var ServerRequestCreator|null
*/
private $requestCreator;

private $options = [];

/**
* @param array {
* debug?: ?bool,
* server_request_creator?: ?string,
* psr17_server_request_factory?: ?string,
* psr17_uri_factory?: ?string,
* psr17_uploaded_file_factory?: ?string,
* psr17_stream_factory?: ?string,
* laminas_emitter?: ?string,
* } $options
*/
public function __construct(array $options = [])
{
$this->options = $options;

parent::__construct($options);
}

public function getRunner(?object $application): RunnerInterface
{
if ($application instanceof RequestHandlerInterface) {
return LaminasEmitter::createForRequestHandler($application, $this->createRequest(), ['emitter' => $this->options['laminas_emitter'] ?? null]);
}

if ($application instanceof ResponseInterface) {
return LaminasEmitter::createForResponse($application, ['emitter' => $this->options['laminas_emitter'] ?? null]);
}

return parent::getRunner($application);
}

/**
* @return mixed
*/
protected function getArgument(\ReflectionParameter $parameter, ?string $type)
{
if (ServerRequestInterface::class === $type) {
return $this->createRequest();
}

return parent::getArgument($parameter, $type);
}

/**
* @return ServerRequestInterface
*/
private function createRequest()
{
if (null === $this->requestCreator) {
if (!class_exists(ServerRequestCreator::class)) {
throw new \LogicException(sprintf('The "%s" class requires "nyholm/psr7-server". Try running "composer require nyholm/psr7-server".', self::class));
}

$creatorClass = $this->options['server_request_creator'] ?? ServerRequestCreator::class;
if (isset($this->options['psr17_server_request_factory'], $this->options['psr17_uri_factory'], $this->options['psr17_uploaded_file_factory'], $this->options['psr17_stream_factory'])) {
$this->requestCreator = new $creatorClass(
new $this->options['psr17_server_request_factory'](),
new $this->options['psr17_uri_factory'](),
new $this->options['psr17_uploaded_file_factory'](),
new $this->options['psr17_stream_factory']()
);
} elseif (class_exists(Psr17Factory::class)) {
$psr17Factory = new Psr17Factory();
$this->requestCreator = new $creatorClass($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
} else {
throw new \LogicException(sprintf('The "%s" class requires PSR-17 factories. Try running "composer require nyholm/psr7" or provide class names to the "%s"::__construct().', self::class, self::class));
}
}

return $this->requestCreator->fromGlobals();
}
}
79 changes: 79 additions & 0 deletions 79 src/Symfony/Component/Runtime/Runner/Psr/LaminasEmitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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\Runtime\Runner\Psr;

use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Runtime\RunnerInterface;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*
* @experimental in 5.3
*/
class LaminasEmitter implements RunnerInterface
{
private $requestHandler;
private $response;
private $request;
private $emitter;

private function __construct(array $options)
{
$class = $options['emitter'] ?? SapiEmitter::class;
if (!class_exists(SapiEmitter::class)) {
throw new \LogicException(sprintf('The "%s" class requires "laminas/laminas-httphandlerrunner". Try running "composer require laminas/laminas-httphandlerrunner".', self::class));
}

if (!class_exists($class)) {
throw new \LogicException(sprintf('The class "%s" cannot be found.', $class));
}

$this->emitter = new $class();
}

/**
* @param array{emitter?: ?string} $options
*/
public static function createForResponse(ResponseInterface $response, array $options): self
{
$emitter = new self($options);
$emitter->response = $response;

return $emitter;
}

/**
* @param array{emitter?: ?string} $options
*/
public static function createForRequestHandler(RequestHandlerInterface $handler, ServerRequestInterface $request, array $options): self
{
$emitter = new self($options);
$emitter->requestHandler = $handler;
$emitter->request = $request;

return $emitter;
}

public function run(): int
{
if (null === $this->response) {
$this->response = $this->requestHandler->handle($this->request);
}

$this->emitter->emit($this->response);

return 0;
}
}
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Runtime/Tests/phpt/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
}

$app = require $_SERVER['SCRIPT_FILENAME'];
$runtime = new SymfonyRuntime($_SERVER['APP_RUNTIME_OPTIONS']);
$runtimeClass = $_SERVER['APP_RUNTIME'] ?? SymfonyRuntime::class;
$runtime = new $runtimeClass($_SERVER['APP_RUNTIME_OPTIONS']);
[$app, $args] = $runtime->getResolver($app)->resolve();
exit($runtime->getRunner($app(...$args))->run());
}
Expand Down
18 changes: 18 additions & 0 deletions 18 src/Symfony/Component/Runtime/Tests/phpt/psr15.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

$_SERVER['APP_RUNTIME'] = \Symfony\Component\Runtime\PsrRuntime::class;

require __DIR__.'/autoload.php';

return function (array $context) {
return new class() implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new \Nyholm\Psr7\Response(200, [], 'Hello PSR-15');
}
};
};
12 changes: 12 additions & 0 deletions 12 src/Symfony/Component/Runtime/Tests/phpt/psr15.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test PSR-15 Request/Response
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/psr15.php';

?>
--EXPECTF--
Hello PSR-15
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/Runtime/Tests/phpt/psr7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Psr\Http\Message\ServerRequestInterface;

$_SERVER['APP_RUNTIME'] = \Symfony\Component\Runtime\PsrRuntime::class;

require __DIR__.'/autoload.php';

return function (ServerRequestInterface $request) {
return new \Nyholm\Psr7\Response(200, [], 'Hello PSR-7');
};
12 changes: 12 additions & 0 deletions 12 src/Symfony/Component/Runtime/Tests/phpt/psr7.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test PSR-7 Request/Response
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/psr7.php';

?>
--EXPECTF--
Hello PSR-7
6 changes: 5 additions & 1 deletion 6 src/Symfony/Component/Runtime/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
"symfony/console": "^4.4|^5",
"symfony/dotenv": "^5.1",
"symfony/http-foundation": "^4.4|^5",
"symfony/http-kernel": "^4.4|^5"
"symfony/http-kernel": "^4.4|^5",
"psr/http-server-handler": "^1.0",
"nyholm/psr7": "^1.3",
"nyholm/psr7-server": "^1.0",
"laminas/laminas-httphandlerrunner": "^1.2"
},
"conflict": {
"symfony/dotenv": "<5.1"
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.