Skip to content

Commit bd9526b

Browse files
committed
feat: request scopes
1 parent b872c6b commit bd9526b

File tree

4 files changed

+148
-232
lines changed

4 files changed

+148
-232
lines changed

src/Http/Adapter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Utopia\Http;
44

5+
use Utopia\DI\Container;
6+
57
abstract class Adapter
68
{
79
abstract public function onStart(callable $callback);
810
abstract public function onRequest(callable $callback);
911
abstract public function start();
12+
abstract public function getContainer(): Container;
1013
}

src/Http/Adapter/FPM/Server.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Server extends Adapter
99
{
10-
public function __construct()
10+
public function __construct(private Container $container)
1111
{
1212
}
1313

@@ -19,18 +19,18 @@ public function onRequest(callable $callback)
1919
'fpmRequest' => $request,
2020
'fpmResponse' => $response,
2121
];
22-
$configureRequestScope = function (Container $requestContainer) use ($request, $response) {
23-
$requestContainer
24-
->set('fpmRequest', fn () => $request)
25-
->set('fpmResponse', fn () => $response);
26-
};
2722

28-
call_user_func($callback, $request, $response, 'fpm', $resources, $configureRequestScope);
23+
\call_user_func($callback, $request, $response, 'fpm', $resources);
2924
}
3025

3126
public function onStart(callable $callback)
3227
{
33-
call_user_func($callback, $this);
28+
\call_user_func($callback, $this);
29+
}
30+
31+
public function getContainer(): Container
32+
{
33+
return $this->container;
3434
}
3535

3636
public function start()

src/Http/Adapter/Swoole/Server.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,40 @@
1414
class Server extends Adapter
1515
{
1616
protected SwooleServer $server;
17+
protected const string REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
18+
protected Container $container;
1719

18-
public function __construct(string $host, ?string $port = null, array $settings = [])
20+
public function __construct(string $host, ?string $port = null, array $settings = [], Container $container)
1921
{
2022
$this->server = new SwooleServer($host, $port);
2123
$this->server->set(\array_merge($settings, [
2224
'enable_coroutine' => true,
2325
'http_parse_cookie' => false,
2426
]));
27+
$this->container = $container;
2528
}
2629

2730
public function onRequest(callable $callback)
2831
{
2932
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
30-
$context = \strval(Coroutine::getCid());
31-
$requestAdapter = new Request($request);
32-
$responseAdapter = new Response($response);
33-
$resources = [
34-
'swooleRequest' => $request,
35-
'swooleResponse' => $response,
36-
];
37-
$configureRequestScope = function (Container $requestContainer) use ($request, $response) {
38-
$requestContainer
39-
->set('swooleRequest', fn () => $request)
40-
->set('swooleResponse', fn () => $response);
41-
};
42-
43-
call_user_func($callback, $requestAdapter, $responseAdapter, $context, $resources, $configureRequestScope);
33+
Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = new Container($this->container);
34+
35+
$utopiaRequest = new Request($request);
36+
$utopiaResponse = new Response($response);
37+
38+
\call_user_func($callback, $utopiaRequest, $utopiaResponse);
4439
});
4540
}
4641

42+
public function getContainer(): Container
43+
{
44+
return Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] ?? $this->container;
45+
}
46+
4747
public function onStart(callable $callback)
4848
{
49-
call_user_func($callback, $this);
49+
50+
\call_user_func($callback, $this);
5051
}
5152

5253
public function start()

0 commit comments

Comments
 (0)