Skip to content

Commit fffe0e6

Browse files
authored
Adding Symfony Roadrunner (#14)
* Adding Symfony Roadrunner * cs
0 parents  commit fffe0e6

File tree

8 files changed

+167
-0
lines changed

8 files changed

+167
-0
lines changed

.github/FUNDING.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: [nyholm]

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# RoadRunner Runtime for Symfony with nyholm/psr7
2+
3+
A runtime for [RoadRunner](https://roadrunner.dev/)
4+
5+
## Installation
6+
7+
```
8+
composer require runtime/roadrunner-symfony-nyholm
9+
```
10+
11+
## Usage
12+
13+
Define the environment variable `APP_RUNTIME` for your application.
14+
15+
```
16+
APP_RUNTIME=Runtime\RoadRunnerSymfonyNyholm\Runtime
17+
```
18+
19+
20+
```php
21+
// public/index.php
22+
23+
use App\Kernel;
24+
25+
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
26+
27+
return function (array $context) {
28+
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
29+
};
30+
31+
```
32+

composer.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "runtime/roadrunner-symfony-nyholm",
3+
"type": "library",
4+
"description": "Roadrunner runtime for with nyholm/psr7",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Tobias Nyholm",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"nyholm/psr7": "^1.4",
14+
"spiral/roadrunner": "^2.0",
15+
"symfony/psr-http-message-bridge": "^2.1",
16+
"symfony/runtime": "5.x-dev"
17+
},
18+
"require-dev": {
19+
"symfony/phpunit-bridge": "^5.2"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Runtime\\RoadRunnerSymfonyNyholm\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Runtime\\RoadRunnerSymfonyNyholm\\Tests\\": "tests/"
29+
}
30+
},
31+
"minimum-stability": "dev",
32+
"prefer-stable": true
33+
}

phpunit.xml.dist

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
backupGlobals="false"
5+
colors="true"
6+
bootstrap="vendor/autoload.php"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
>
10+
<php>
11+
<ini name="error_reporting" value="-1"/>
12+
</php>
13+
<testsuites>
14+
<testsuite name="Test Suite">
15+
<directory>./tests</directory>
16+
<directory suffix=".phpt">./tests/phpt</directory>
17+
</testsuite>
18+
</testsuites>
19+
</phpunit>

src/Runner.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Runtime\RoadRunnerSymfonyNyholm;
4+
5+
use Nyholm\Psr7;
6+
use Spiral\RoadRunner;
7+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
8+
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
9+
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
10+
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
11+
use Symfony\Component\HttpKernel\HttpKernelInterface;
12+
use Symfony\Component\HttpKernel\TerminableInterface;
13+
use Symfony\Component\Runtime\RunnerInterface;
14+
15+
/**
16+
* @author Tobias Nyholm <[email protected]>
17+
*/
18+
class Runner implements RunnerInterface
19+
{
20+
private $kernel;
21+
private $httpFoundationFactory;
22+
private $httpMessageFactory;
23+
private $psrFactory;
24+
25+
public function __construct(HttpKernelInterface $kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null)
26+
{
27+
$this->kernel = $kernel;
28+
$this->psrFactory = new Psr7\Factory\Psr17Factory();
29+
$this->httpFoundationFactory = $httpFoundationFactory ?? new HttpFoundationFactory();
30+
$this->httpMessageFactory = $httpMessageFactory ?? new PsrHttpFactory($this->psrFactory, $this->psrFactory, $this->psrFactory, $this->psrFactory);
31+
}
32+
33+
public function run(): int
34+
{
35+
$worker = RoadRunner\Worker::create();
36+
$worker = new RoadRunner\Http\PSR7Worker($worker, $this->psrFactory, $this->psrFactory, $this->psrFactory);
37+
38+
while ($request = $worker->waitRequest()) {
39+
try {
40+
$sfRequest = $this->httpFoundationFactory->createRequest($request);
41+
$sfResponse = $this->kernel->handle($sfRequest);
42+
$worker->respond($this->httpMessageFactory->createResponse($sfResponse));
43+
44+
if ($this->kernel instanceof TerminableInterface) {
45+
$this->kernel->terminate($sfRequest, $sfResponse);
46+
}
47+
} catch (\Throwable $e) {
48+
$worker->getWorker()->error((string) $e);
49+
}
50+
}
51+
52+
return 0;
53+
}
54+
}

src/Runtime.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Runtime\RoadRunnerSymfonyNyholm;
4+
5+
use Symfony\Component\HttpKernel\HttpKernelInterface;
6+
use Symfony\Component\Runtime\RunnerInterface;
7+
use Symfony\Component\Runtime\SymfonyRuntime;
8+
9+
/**
10+
* A runtime for RoadRunner.
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class Runtime extends SymfonyRuntime
15+
{
16+
public function getRunner(?object $application): RunnerInterface
17+
{
18+
if ($application instanceof HttpKernelInterface) {
19+
return new Runner($application);
20+
}
21+
22+
return parent::getRunner($application);
23+
}
24+
}

tests/.gitignore

Whitespace-only changes.

0 commit comments

Comments
 (0)