File tree Expand file tree Collapse file tree 7 files changed +136
-0
lines changed
Expand file tree Collapse file tree 7 files changed +136
-0
lines changed Original file line number Diff line number Diff line change 1+ /vendor /
2+ composer.lock
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " runtime/react" ,
3+ "type" : " library" ,
4+ "description" : " ReactPHP runtime" ,
5+ "license" : " MIT" ,
6+ "authors" : [
7+ {
8+ "name" : " Nyholm" ,
9+ "email" : " tobias.nyholm@gmail.com"
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+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Symfony \Runtime \Psr \Server ;
4+
5+ use Runtime \React \Runtime ;
6+
7+ class RequestHandlerInterfaceRuntime extends Runtime
8+ {
9+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 <tobias.nyholm@gmail.com>
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+ }
Original file line number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments