Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/Http/Adapter/SwooleCoroutine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@

class Server extends Adapter
{
protected SwooleServer $server;
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';

protected SwooleServer $server;
protected Container $container;

/** @var callable|null */
protected $onStartCallback = null;

public function __construct(string $host, ?string $port = null, array $settings = [], ?Container $container = null)
{
public function __construct(
string $host,
?string $port = null,
array $settings = [],
?Container $container = null
) {
$this->server = new SwooleServer($host, $port, false, true);
$this->server->set(\array_merge($settings, [
'http_parse_cookie' => false,
Expand All @@ -29,25 +35,23 @@ public function __construct(string $host, ?string $port = null, array $settings
public function onRequest(callable $callback)
{
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
go(function () use ($request, $response, $callback) {
$requestContainer = new Container($this->container);
$requestContainer->set('swooleRequest', fn () => $request);
$requestContainer->set('swooleResponse', fn () => $response);
$requestContainer = new Container($this->container);
$requestContainer->set('swooleRequest', fn () => $request);
$requestContainer->set('swooleResponse', fn () => $response);

Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;
Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;

try {
\call_user_func($callback, new Request($request), new Response($response));
});
} finally {
unset(Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY]);
}
});
}

public function getContainer(): Container
{
if (Coroutine::getCid() !== -1) {
return Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] ?? $this->container;
}

return $this->container;
return Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] ?? $this->container;
}

public function getServer(): SwooleServer
Expand All @@ -62,11 +66,10 @@ public function onStart(callable $callback)

public function start()
{
go(function () {
if ($this->onStartCallback) {
\call_user_func($this->onStartCallback, $this);
}
$this->server->start();
});
if ($this->onStartCallback) {
\call_user_func($this->onStartCallback, $this);
}

$this->server->start();
}
}
Loading