|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Http\Adapter\Swoole; |
| 4 | + |
| 5 | +use Swoole\Coroutine; |
| 6 | +use Utopia\Http\Adapter; |
| 7 | +use Utopia\DI\Container; |
| 8 | +use Swoole\Http\Server as SwooleServer; |
| 9 | +use Swoole\Http\Request as SwooleRequest; |
| 10 | +use Swoole\Http\Response as SwooleResponse; |
| 11 | + |
| 12 | +class HttpServer extends Adapter |
| 13 | +{ |
| 14 | + protected SwooleServer $server; |
| 15 | + protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container'; |
| 16 | + protected Container $container; |
| 17 | + protected ?Container $requestContainer = null; |
| 18 | + protected bool $coroutines; |
| 19 | + |
| 20 | + public function __construct(string $host, ?string $port = null, array $settings = [], ?Container $container = null, bool $coroutines = false) |
| 21 | + { |
| 22 | + $this->coroutines = $coroutines; |
| 23 | + $this->server = new SwooleServer($host, (int) $port); |
| 24 | + $this->server->set(\array_merge([ |
| 25 | + 'enable_coroutine' => $coroutines, |
| 26 | + ], $settings)); |
| 27 | + $this->container = $container ?? new Container(); |
| 28 | + } |
| 29 | + |
| 30 | + public function onRequest(callable $callback) |
| 31 | + { |
| 32 | + $this->server->on('request', function (SwooleRequest $request, SwooleResponse $response) use ($callback) { |
| 33 | + $handler = function () use ($request, $response, $callback) { |
| 34 | + $requestContainer = new Container($this->container); |
| 35 | + $requestContainer->set('swooleRequest', fn () => $request); |
| 36 | + $requestContainer->set('swooleResponse', fn () => $response); |
| 37 | + |
| 38 | + if ($this->coroutines && Coroutine::getCid() !== -1) { |
| 39 | + Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer; |
| 40 | + } else { |
| 41 | + $this->requestContainer = $requestContainer; |
| 42 | + } |
| 43 | + |
| 44 | + \call_user_func($callback, new Request($request), new Response($response)); |
| 45 | + }; |
| 46 | + |
| 47 | + if ($this->coroutines) { |
| 48 | + go($handler); |
| 49 | + } else { |
| 50 | + $handler(); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + public function getContainer(): Container |
| 56 | + { |
| 57 | + if ($this->coroutines && Coroutine::getCid() !== -1) { |
| 58 | + return Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] ?? $this->container; |
| 59 | + } |
| 60 | + |
| 61 | + return $this->requestContainer ?? $this->container; |
| 62 | + } |
| 63 | + |
| 64 | + public function getServer(): SwooleServer |
| 65 | + { |
| 66 | + return $this->server; |
| 67 | + } |
| 68 | + |
| 69 | + public function onStart(callable $callback) |
| 70 | + { |
| 71 | + $this->server->on('start', function () use ($callback) { |
| 72 | + if ($this->coroutines) { |
| 73 | + go(function () use ($callback) { |
| 74 | + \call_user_func($callback, $this); |
| 75 | + }); |
| 76 | + } else { |
| 77 | + \call_user_func($callback, $this); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + public function start() |
| 83 | + { |
| 84 | + return $this->server->start(); |
| 85 | + } |
| 86 | +} |
0 commit comments