-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServerAppSetup.php
More file actions
73 lines (61 loc) · 2.38 KB
/
Copy pathServerAppSetup.php
File metadata and controls
73 lines (61 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace ESB;
use Assert\Assertion;
use ESB\Entity\VO\HttpDSN;
use ESB\Handlers\HTTP\ESBHandlerInterface;
use ESB\Handlers\HTTP\RouteCRUDHandler;
use ESB\Handlers\HTTP\RouteListHandler;
use ESB\Middleware\HTTP\InitRouteDataMiddleware;
use ESB\Repository\RouteRepositoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\App;
use Slim\Routing\RouteCollectorProxy;
use function preg_match;
class ServerAppSetup
{
public function __construct(private readonly RouteRepositoryInterface $provider, private readonly string $basePath = '/middleware')
{
Assertion::true(! ! preg_match('/(\/\w+(\/)?)+/', $this->basePath), 'ServerAppSetup: basePath expecting been uri-path');
}
public function __invoke(App $app) : void
{
$this->setupRoutes($app);
$this->setupCrudRoutes($app);
$this->setupMiddlewares($app);
}
protected function setupCrudRoutes(App $app) : void
{
$app->group('/route', function (RouteCollectorProxy $group) {
$group->options('{routes:.*}', function (RequestInterface $request, ResponseInterface $response, array $args) {
return $response;
});
$group->map(['POST', 'PUT'], '', RouteCRUDHandler::class);
$group->delete('/{name}', RouteCRUDHandler::class);
$group->get('/{name}', RouteCRUDHandler::class);
$group->get('', RouteListHandler::class);
});
}
protected function setupRoutes(App $app) : void
{
$routes = $this->provider->loadAll();
$app->group($this->basePath, function (RouteCollectorProxy $group) use ($routes) {
$group->options('{routes:.*}', function (RequestInterface $request, ResponseInterface $response, array $args) {
return $response;
});
foreach ($routes as $route) {
$routeDsn = $route->fromSystemDsn();
if (! $routeDsn instanceof HttpDSN) {
continue;
}
$group->map([$routeDsn->method], $routeDsn->path, ESBHandlerInterface::class);
}
})->add(new InitRouteDataMiddleware($this->provider, $this->basePath));
}
protected function setupMiddlewares(App $app) : void
{
$app->addRoutingMiddleware();
$app->addBodyParsingMiddleware();
}
}