|
| 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 | +} |
0 commit comments