Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.00% |
44 / 50 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ApieRouteLoader | |
88.00% |
44 / 50 |
|
50.00% |
1 / 2 |
11.21 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
loadRoutes | |
87.76% |
43 / 49 |
|
0.00% |
0 / 1 |
10.18 |
1 | <?php |
2 | namespace Apie\LaravelApie\Wrappers\Routing; |
3 | |
4 | use Apie\Common\Enums\UrlPrefix; |
5 | use Apie\Common\Interfaces\GlobalRouteDefinitionProviderInterface; |
6 | use Apie\Common\Interfaces\HasRouteDefinition; |
7 | use Apie\Common\Interfaces\RouteDefinitionProviderInterface; |
8 | use Apie\Common\RouteDefinitions\PossibleRoutePrefixProvider; |
9 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
10 | use Apie\Core\Context\ApieContext; |
11 | use Apie\Core\Enums\RequestMethod; |
12 | use Apie\Core\ValueObjects\Utils; |
13 | use Apie\LaravelApie\Wrappers\Security\VerifyApieUser; |
14 | use Illuminate\Routing\RouteRegistrar; |
15 | use Illuminate\Session\Middleware\StartSession; |
16 | |
17 | class ApieRouteLoader |
18 | { |
19 | private bool $loaded = false; |
20 | |
21 | public function __construct( |
22 | private readonly RouteDefinitionProviderInterface $routeProvider, |
23 | private readonly BoundedContextHashmap $boundedContextHashmap, |
24 | private readonly PossibleRoutePrefixProvider $routePrefixProvider |
25 | ) { |
26 | } |
27 | |
28 | public function loadRoutes(RouteRegistrar $routeRegistrar): void |
29 | { |
30 | if ($this->loaded === true) { |
31 | throw new \RuntimeException('Do not load the "ApieRouteLoader" twice!'); |
32 | } |
33 | $this->loaded = true; |
34 | $apieContext = new ApieContext([]); |
35 | $cmsMiddleware = Utils::toArray(config('apie.cms.laravel_middleware') ?? []); |
36 | $apiMiddleware = Utils::toArray(config('apie.rest_api.laravel_middleware') ?? []); |
37 | |
38 | if ($this->routeProvider instanceof GlobalRouteDefinitionProviderInterface) { |
39 | foreach ($this->routeProvider->getGlobalRoutes() as $routeDefinition) { |
40 | /** @var HasRouteDefinition $routeDefinition */ |
41 | |
42 | $path = ltrim($routeDefinition->getUrl(), '/'); |
43 | $method = $routeDefinition->getMethod(); |
44 | $defaults = $routeDefinition->getRouteAttributes() |
45 | + [ |
46 | '_is_apie' => true, |
47 | 'uses' => $routeDefinition->getController(), |
48 | ]; |
49 | /** @var \Illuminate\Routing\Route $route */ |
50 | if ($method === RequestMethod::ANY) { |
51 | $route = $routeRegistrar->match( |
52 | array_map(fn ($s) => $s->value, RequestMethod::cases()), |
53 | $path, |
54 | $routeDefinition->getController() |
55 | ); |
56 | } else { |
57 | $route = $routeRegistrar->{strtolower($method->value)}($path, $routeDefinition->getController()); |
58 | } |
59 | $route->defaults += $defaults; |
60 | $route->name('apie.global.' . $routeDefinition->getOperationId()); |
61 | $route->middleware([StartSession::class, VerifyApieUser::class, ...$cmsMiddleware]); |
62 | } |
63 | } |
64 | |
65 | foreach ($this->boundedContextHashmap as $boundedContextId => $boundedContext) { |
66 | foreach ($this->routeProvider->getActionsForBoundedContext($boundedContext, $apieContext) as $routeDefinition) { |
67 | /** @var HasRouteDefinition $routeDefinition */ |
68 | $prefix = $this->routePrefixProvider->getPossiblePrefixes($routeDefinition); |
69 | |
70 | $path = $prefix . $boundedContextId . '/' . ltrim($routeDefinition->getUrl(), '/'); |
71 | |
72 | $method = $routeDefinition->getMethod(); |
73 | $defaults = $routeDefinition->getRouteAttributes() |
74 | + [ |
75 | '_is_apie' => true, |
76 | 'uses' => $routeDefinition->getController(), |
77 | ]; |
78 | /** @var \Illuminate\Routing\Route $route */ |
79 | if ($method === RequestMethod::ANY) { |
80 | $route = $routeRegistrar->match( |
81 | array_map(fn ($s) => $s->value, RequestMethod::cases()), |
82 | $path, |
83 | $routeDefinition->getController() |
84 | ); |
85 | } else { |
86 | $route = $routeRegistrar->{strtolower($method->value)}($path, $routeDefinition->getController()); |
87 | } |
88 | $route->defaults += $defaults; |
89 | $route->name('apie.' . $boundedContextId . '.' . $routeDefinition->getOperationId()); |
90 | foreach ($routeDefinition->getUrlPrefixes() as $urlPrefix) { |
91 | if ($urlPrefix === UrlPrefix::CMS) { |
92 | $route->middleware([StartSession::class, VerifyApieUser::class, ...$cmsMiddleware]); |
93 | } else { |
94 | $route->middleware([StartSession::class, VerifyApieUser::class, ...$apiMiddleware]); |
95 | } |
96 | } |
97 | $route->wheres = $prefix->getRouteRequirements(); |
98 | } |
99 | } |
100 | } |
101 | } |