Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
24 / 25
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApieRouteLoader
96.00% covered (success)
96.00%
24 / 25
50.00% covered (danger)
50.00%
1 / 2
7
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadRoutes
95.83% covered (success)
95.83%
23 / 24
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\LaravelApie\Wrappers\Routing;
3
4use Apie\Common\Enums\UrlPrefix;
5use Apie\Common\Interfaces\HasRouteDefinition;
6use Apie\Common\Interfaces\RouteDefinitionProviderInterface;
7use Apie\Common\RouteDefinitions\PossibleRoutePrefixProvider;
8use Apie\Core\BoundedContext\BoundedContextHashmap;
9use Apie\Core\Context\ApieContext;
10use Apie\Core\ValueObjects\Utils;
11use Apie\LaravelApie\Wrappers\Security\VerifyApieUser;
12use Illuminate\Routing\RouteRegistrar;
13use Illuminate\Session\Middleware\StartSession;
14
15class ApieRouteLoader
16{
17    private bool $loaded = false;
18
19    public function __construct(
20        private readonly RouteDefinitionProviderInterface $routeProvider,
21        private readonly BoundedContextHashmap $boundedContextHashmap,
22        private readonly PossibleRoutePrefixProvider $routePrefixProvider
23    ) {
24    }
25
26    public function loadRoutes(RouteRegistrar $routeRegistrar): void
27    {
28        if ($this->loaded === true) {
29            throw new \RuntimeException('Do not load the "ApieRouteLoader" twice!');
30        }
31        $this->loaded = true;
32        $apieContext = new ApieContext([]);
33        $cmsMiddleware = Utils::toArray(config('apie.cms.laravel_middleware') ?? []);
34        $apiMiddleware = Utils::toArray(config('apie.rest_api.laravel_middleware') ?? []);
35        foreach ($this->boundedContextHashmap as $boundedContextId => $boundedContext) {
36            foreach ($this->routeProvider->getActionsForBoundedContext($boundedContext, $apieContext) as $routeDefinition) {
37                /** @var HasRouteDefinition $routeDefinition */
38                $prefix = $this->routePrefixProvider->getPossiblePrefixes($routeDefinition);
39
40                $path = $prefix . $boundedContextId . '/' . ltrim($routeDefinition->getUrl(), '/');
41
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.' . $boundedContextId . '.' . $routeDefinition->getOperationId());
52                foreach ($routeDefinition->getUrlPrefixes() as $urlPrefix) {
53                    if ($urlPrefix === UrlPrefix::CMS) {
54                        $route->middleware([StartSession::class, VerifyApieUser::class, ...$cmsMiddleware]);
55                    } else {
56                        $route->middleware([StartSession::class, VerifyApieUser::class, ...$apiMiddleware]);
57                    }
58                }
59                $route->wheres = $prefix->getRouteRequirements();
60            }
61        }
62    }
63}