Skip to content

Commit

Permalink
Add swoole resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Oct 3, 2023
1 parent 96e1f58 commit c9ea90e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Http/Adapter/Swoole/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function onRequest(callable $callback)
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
$context = \strval(Coroutine::getCid());

Http::setResource('swooleRequest', fn () => $request);
Http::setResource('swooleResponse', fn () => $response);
Http::setResource('swooleRequest', fn () => $request, [], $context);
Http::setResource('swooleResponse', fn () => $response, [], $context);

call_user_func($callback, new Request($request), new Response($response), $context);
});
Expand Down
42 changes: 25 additions & 17 deletions src/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,23 @@ public function getResource(string $name, string $context = 'utopia', bool $fres

$this->resources[$context] ??= [];

if (!\array_key_exists($name, $this->resources[$context]) || $fresh || (self::$resourcesCallbacks[$name]['reset'][$context] ?? true)) {
if (!\array_key_exists($name, self::$resourcesCallbacks)) {
$resourcesCallback = &self::$resourcesCallbacks[$context] ?? [];
if(empty($resourcesCallback) || !\array_key_exists($name, $resourcesCallback)) {
$resourcesCallback = &self::$resourcesCallbacks['utopia'];
}

if (!\array_key_exists($name, $this->resources[$context]) || $fresh || ($resourcesCallback[$name]['reset'][$context] ?? true)) {
if (!\array_key_exists($name, $resourcesCallback)) {
throw new Exception('Failed to find resource: "' . $name . '"');
}

$this->resources[$context][$name] = \call_user_func_array(
self::$resourcesCallbacks[$name]['callback'],
$this->getResources(self::$resourcesCallbacks[$name]['injections'], $context)
$resourcesCallback[$name]['callback'],
$this->getResources($resourcesCallback[$name]['injections'], $context)
);
}

self::$resourcesCallbacks[$name]['reset'][$context] = false;
$resourcesCallback[$name]['reset'][$context] = false;
return $this->resources[$context][$name];
}

Expand Down Expand Up @@ -391,12 +396,15 @@ public function getResources(array $list, string $context): array
*
* @throws Exception
*/
public static function setResource(string $name, callable $callback, array $injections = []): void
public static function setResource(string $name, callable $callback, array $injections = [], string $context = 'utopia'): void
{
if ($name === 'utopia') {
throw new Exception("'utopia' is a reserved keyword.", 500);
}
self::$resourcesCallbacks[$name] = ['callback' => $callback, 'injections' => $injections, 'resets' => []];

self::$resourcesCallbacks[$context] ??= [];

self::$resourcesCallbacks[$context][$name] = ['callback' => $callback, 'injections' => $injections, 'resets' => []];
}

/**
Expand Down Expand Up @@ -697,7 +705,7 @@ public function execute(Route $route, Request $request, string $context): static
}
}
} catch (\Throwable $e) {
self::setResource('error', fn () => $e);
self::setResource('error', fn () => $e, [], $context);

foreach ($groups as $group) {
foreach (self::$errors as $error) { // Group error hooks
Expand Down Expand Up @@ -787,11 +795,11 @@ public function run(Request $request, Response $response, string $context): stat
$this->resources[$context]['request'] = $request;
$this->resources[$context]['response'] = $response;

self::setResource('context', fn () => $context);
self::setResource('context', fn () => $context, [], $context);

self::setResource('request', fn () => $request);
self::setResource('request', fn () => $request, [], $context);

self::setResource('response', fn () => $response);
self::setResource('response', fn () => $response, [], $context);

try {

Expand All @@ -800,7 +808,7 @@ public function run(Request $request, Response $response, string $context): stat
\call_user_func_array($hook->getAction(), $arguments);
}
} catch(\Exception $e) {
self::setResource('error', fn () => $e);
self::setResource('error', fn () => $e, [], $context);

foreach (self::$errors as $error) { // Global error hooks
if (in_array('*', $error->getGroups())) {
Expand Down Expand Up @@ -829,7 +837,7 @@ public function run(Request $request, Response $response, string $context): stat
$route = $this->match($request);
$groups = ($route instanceof Route) ? $route->getGroups() : [];

self::setResource('route', fn () => $route);
self::setResource('route', fn () => $route, [], $context);

if (self::REQUEST_METHOD_HEAD == $method) {
$method = self::REQUEST_METHOD_GET;
Expand Down Expand Up @@ -859,7 +867,7 @@ public function run(Request $request, Response $response, string $context): stat
if (in_array('*', $error->getGroups())) {
self::setResource('error', function () use ($e) {
return $e;
});
}, [], $context);
\call_user_func_array($error->getAction(), $this->getArguments($error, $context, [], $request->getParams()));
}
}
Expand All @@ -874,7 +882,7 @@ public function run(Request $request, Response $response, string $context): stat
$path = \parse_url($request->getURI(), PHP_URL_PATH);
$route->path($path);

self::setResource('route', fn () => $route);
self::setResource('route', fn () => $route, [], $context);
}

if (null !== $route) {
Expand All @@ -899,7 +907,7 @@ public function run(Request $request, Response $response, string $context): stat
if (in_array('*', $error->getGroups())) {
self::setResource('error', function () use ($e) {
return $e;
});
}, [], $context);
\call_user_func_array($error->getAction(), $this->getArguments($error, $context, [], $request->getParams()));
}
}
Expand All @@ -909,7 +917,7 @@ public function run(Request $request, Response $response, string $context): stat
if (in_array('*', $error->getGroups())) {
self::setResource('error', function () {
return new Exception('Not Found', 404);
});
}, [], $context);
\call_user_func_array($error->getAction(), $this->getArguments($error, $context, [], $request->getParams()));
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/ResponseSwooleTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public function setUp(): void
{
$this->client = new Client('http://swoole');
}

public function testSwooleResources(): void
{
$response = $this->client->call(Client::METHOD_DELETE, '/swoole-test');
$this->assertEquals('DELETE', $response['body']);
}
}
14 changes: 14 additions & 0 deletions tests/e2e/server_swoole.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

require_once __DIR__.'/init.php';

use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Utopia\Http\Adapter\Swoole\Server;
use Utopia\Http\Http;

use function Swoole\Coroutine\run;

Http::delete('/swoole-test')
->inject('swooleRequest')
->inject('swooleResponse')
->action(function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
$method = $swooleRequest->getMethod();
$swooleResponse->header('Content-Type', 'text/plain');
$swooleResponse->header('Cache-Control', 'no-cache');
$swooleResponse->setStatusCode(200);
$swooleResponse->write($method);
$swooleResponse->end();
});

$server = new Server('0.0.0.0', '80');
$http = new Http($server, 'UTC');

Expand Down

0 comments on commit c9ea90e

Please sign in to comment.