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