Skip to content

Commit bcd0bcd

Browse files
authored
Init ReactPHP runtime (#3)
* Init ReactPHP runtime * cs * Update composer.json on psr-nyholm-laminas * Exclude runtime * normalize composer * Properly install lowest * Change composer working dir
0 parents  commit bcd0bcd

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "runtime/react",
3+
"type": "library",
4+
"description": "ReactPHP runtime",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Nyholm",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"psr/http-server-handler": "^1.0",
14+
"react/http": "^1.2",
15+
"symfony/runtime": "5.x-dev"
16+
},
17+
"require-dev": {
18+
"symfony/phpunit-bridge": "5.x-dev"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Runtime\\React\\": "src/",
23+
"Symfony\\Runtime\\Psr\\": "runtime/"
24+
}
25+
},
26+
"minimum-stability": "dev",
27+
"prefer-stable": true
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Runtime\Psr\Server;
4+
5+
use Runtime\React\Runtime;
6+
7+
class RequestHandlerInterfaceRuntime extends Runtime
8+
{
9+
}

src/Runner.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Runtime\React;
4+
5+
use Psr\Http\Message\ServerRequestInterface;
6+
use Symfony\Component\Runtime\RunnerInterface;
7+
8+
class Runner implements RunnerInterface
9+
{
10+
private $application;
11+
private $port;
12+
13+
public function __construct($application, $port)
14+
{
15+
$this->application = $application;
16+
$this->port = $port;
17+
}
18+
19+
public function run(): int
20+
{
21+
$application = $this->application;
22+
$loop = \React\EventLoop\Factory::create();
23+
24+
$server = new \React\Http\Server($loop, function (ServerRequestInterface $request) use ($application) {
25+
return $application->handle($request);
26+
});
27+
28+
$socket = new \React\Socket\Server($this->port, $loop);
29+
$server->listen($socket);
30+
31+
$loop->run();
32+
33+
return 0;
34+
}
35+
}

src/Runtime.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Runtime\React;
4+
5+
use Psr\Http\Server\RequestHandlerInterface;
6+
use Symfony\Component\Runtime\GenericRuntime;
7+
use Symfony\Component\Runtime\RunnerInterface;
8+
9+
/**
10+
* A runtime for ReactPHP.
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class Runtime extends GenericRuntime
15+
{
16+
private $port;
17+
18+
public function __construct(array $options)
19+
{
20+
$this->port = $options['port'];
21+
parent::__construct($options);
22+
}
23+
24+
public function getRunner(?object $application): RunnerInterface
25+
{
26+
if ($application instanceof RequestHandlerInterface) {
27+
return new Runner($application, $this->port);
28+
}
29+
30+
return parent::getRunner($application);
31+
}
32+
}

tests/phpt/psr15.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Psr\Http\Message\ResponseInterface;
4+
use Psr\Http\Message\ServerRequestInterface;
5+
use Psr\Http\Server\RequestHandlerInterface;
6+
7+
$_SERVER['APP_RUNTIME'] = \Runtime\React\Runtime::class;
8+
9+
require __DIR__.'/autoload.php';
10+
11+
return function (array $context) {
12+
return new class() implements RequestHandlerInterface {
13+
public function handle(ServerRequestInterface $request): ResponseInterface
14+
{
15+
return new \React\Http\Message\Response(200, [], 'Hello PSR-15');
16+
}
17+
};
18+
};

tests/phpt/psr15.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test PSR-15 Request/Response
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/psr15.php';
9+
10+
?>
11+
--EXPECTF--
12+
Hello PSR-15

0 commit comments

Comments
 (0)