Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.77% |
79 / 88 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ApieRouteLoader | |
89.77% |
79 / 88 |
|
66.67% |
2 / 3 |
22.52 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| load | |
89.53% |
77 / 86 |
|
0.00% |
0 / 1 |
20.46 | |||
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\ApieBundle\Routing; |
| 3 | |
| 4 | use Apie\Cms\RouteDefinitions\CmsRouteDefinitionProvider; |
| 5 | use Apie\Common\Interfaces\GlobalRouteDefinitionProviderInterface; |
| 6 | use Apie\Common\Interfaces\HasRouteDefinition; |
| 7 | use Apie\Common\Interfaces\RouteDefinitionProviderInterface; |
| 8 | use Apie\Common\Lists\UrlPrefixList; |
| 9 | use Apie\Common\RouteDefinitions\ActionHashmap; |
| 10 | use Apie\Common\RouteDefinitions\PossibleRoutePrefixProvider; |
| 11 | use Apie\Core\ApieLib; |
| 12 | use Apie\Core\Attributes\Route as AttributesRoute; |
| 13 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 14 | use Apie\Core\ContextBuilders\ContextBuilderFactory; |
| 15 | use Apie\Core\Enums\RequestMethod; |
| 16 | use Apie\Core\ValueObjects\UrlRouteDefinition; |
| 17 | use Apie\RestApi\RouteDefinitions\RestApiRouteDefinitionProvider; |
| 18 | use Psr\Log\LoggerInterface; |
| 19 | use Psr\Log\NullLogger; |
| 20 | use ReflectionClass; |
| 21 | use Symfony\Component\Config\Loader\Loader; |
| 22 | use Symfony\Component\Config\Resource\DirectoryResource; |
| 23 | use Symfony\Component\Config\Resource\FileExistenceResource; |
| 24 | use Symfony\Component\Config\Resource\GlobResource; |
| 25 | use Symfony\Component\Config\Resource\ReflectionClassResource; |
| 26 | use Symfony\Component\Routing\Route; |
| 27 | use Symfony\Component\Routing\RouteCollection; |
| 28 | |
| 29 | /** |
| 30 | * Loads the Apie routing into the symfony loader system. |
| 31 | */ |
| 32 | final class ApieRouteLoader extends Loader |
| 33 | { |
| 34 | private bool $loaded = false; |
| 35 | |
| 36 | /** |
| 37 | * @param array<string, string> $scanBoundedContexts |
| 38 | */ |
| 39 | public function __construct( |
| 40 | private readonly RouteDefinitionProviderInterface $routeProvider, |
| 41 | private readonly BoundedContextHashmap $boundedContextHashmap, |
| 42 | private readonly PossibleRoutePrefixProvider $routePrefixProvider, |
| 43 | private readonly ContextBuilderFactory $contextBuilder, |
| 44 | private readonly LoggerInterface $logger = new NullLogger(), |
| 45 | private readonly array $scanBoundedContexts = [], |
| 46 | ) { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param mixed $resource |
| 51 | * @param mixed $type |
| 52 | */ |
| 53 | public function load($resource, $type = null): RouteCollection |
| 54 | { |
| 55 | if (true === $this->loaded) { |
| 56 | throw new \RuntimeException('Do not add the "apie" loader twice'); |
| 57 | } |
| 58 | |
| 59 | $routes = new RouteCollection(); |
| 60 | $classesForCaching = [ |
| 61 | __CLASS__, |
| 62 | $this->routeProvider, |
| 63 | $this->boundedContextHashmap, |
| 64 | $this->routePrefixProvider, |
| 65 | RestApiRouteDefinitionProvider::class, |
| 66 | CmsRouteDefinitionProvider::class, |
| 67 | RouteDefinitionProviderInterface::class, |
| 68 | HasRouteDefinition::class, |
| 69 | UrlRouteDefinition::class, |
| 70 | RequestMethod::class, |
| 71 | UrlPrefixList::class, |
| 72 | ActionHashmap::class, |
| 73 | ApieLib::class, |
| 74 | AttributesRoute::class, |
| 75 | ]; |
| 76 | if (!empty($this->scanBoundedContexts['search_path'])) { |
| 77 | if (!is_dir($this->scanBoundedContexts['search_path'])) { |
| 78 | if (!@mkdir($this->scanBoundedContexts['search_path'], recursive: true)) { |
| 79 | $this->logger->error('I could not create path: "' . $this->scanBoundedContexts['search_path'] . '"'); |
| 80 | } |
| 81 | } |
| 82 | if (is_dir($this->scanBoundedContexts['search_path'])) { |
| 83 | $routes->addResource(new GlobResource($this->scanBoundedContexts['search_path'], '*', true)); |
| 84 | } else { |
| 85 | $routes->addResource(new FileExistenceResource($this->scanBoundedContexts['search_path'])); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | foreach ($classesForCaching as $classForCaching) { |
| 90 | if (is_object($classForCaching) || class_exists($classForCaching)) { |
| 91 | $routes->addResource(new ReflectionClassResource(new ReflectionClass($classForCaching))); |
| 92 | } |
| 93 | } |
| 94 | $pathsHandled = []; |
| 95 | foreach ($this->boundedContextHashmap as $boundedContext) { |
| 96 | foreach ($boundedContext->resources as $resource) { |
| 97 | $routes->addResource(new ReflectionClassResource($resource)); |
| 98 | $path = dirname($resource->getFileName()); |
| 99 | if (!isset($pathsHandled[$path])) { |
| 100 | $pathsHandled[$path] = true; |
| 101 | $routes->addResource(new DirectoryResource($path)); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | $apieContext = $this->contextBuilder->createGeneralContext([ |
| 106 | 'route-gen' => true, |
| 107 | ]); |
| 108 | if ($this->routeProvider instanceof GlobalRouteDefinitionProviderInterface) { |
| 109 | foreach ($this->routeProvider->getGlobalRoutes() as $routeDefinition) { |
| 110 | $routes->addResource(new ReflectionClassResource(new ReflectionClass($routeDefinition))); |
| 111 | /** @var HasRouteDefinition $routeDefinition */ |
| 112 | |
| 113 | $requirements = []; |
| 114 | $url = $routeDefinition->getUrl(); |
| 115 | $placeholders = $url->getPlaceholders(); |
| 116 | if (in_array('properties', $placeholders)) { |
| 117 | $requirements['properties'] = '[a-zA-Z0-9]+(/[a-zA-Z0-9]+)*'; |
| 118 | } |
| 119 | if (in_array('path', $placeholders)) { |
| 120 | $requirements['path'] = '.*'; |
| 121 | } |
| 122 | $path = ltrim($url, '/'); |
| 123 | $method = $routeDefinition->getMethod(); |
| 124 | $defaults = $routeDefinition->getRouteAttributes() |
| 125 | + [ |
| 126 | '_controller' => $routeDefinition->getController(), |
| 127 | '_is_apie' => true, |
| 128 | ]; |
| 129 | $route = (new Route($path, $defaults, $requirements))->setMethods($method->toSymfonyRequestMethod()); |
| 130 | $routes->add( |
| 131 | 'apie._global.' . $routeDefinition->getOperationId(), |
| 132 | $route |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 | foreach ($this->boundedContextHashmap as $boundedContextId => $boundedContext) { |
| 137 | foreach ($this->routeProvider->getActionsForBoundedContext($boundedContext, $apieContext) as $routeDefinition) { |
| 138 | $routes->addResource(new ReflectionClassResource(new ReflectionClass($routeDefinition))); |
| 139 | /** @var HasRouteDefinition $routeDefinition */ |
| 140 | $prefix = $this->routePrefixProvider->getPossiblePrefixes($routeDefinition); |
| 141 | |
| 142 | $requirements = $prefix->getRouteRequirements(); |
| 143 | $url = $routeDefinition->getUrl(); |
| 144 | $placeholders = $url->getPlaceholders(); |
| 145 | if (in_array('properties', $placeholders)) { |
| 146 | $requirements['properties'] = '[a-zA-Z0-9]+(/[a-zA-Z0-9]+)*'; |
| 147 | } |
| 148 | if (in_array('path', $placeholders)) { |
| 149 | $requirements['path'] = '.*'; |
| 150 | } |
| 151 | $path = $prefix . $boundedContextId . '/' . ltrim($url, '/'); |
| 152 | $method = $routeDefinition->getMethod(); |
| 153 | $defaults = $routeDefinition->getRouteAttributes() |
| 154 | + [ |
| 155 | '_controller' => $routeDefinition->getController(), |
| 156 | '_is_apie' => true, |
| 157 | ]; |
| 158 | $route = (new Route($path, $defaults, $requirements))->setMethods([$method->value]); |
| 159 | $routes->add( |
| 160 | 'apie.' . $boundedContextId . '.' . $routeDefinition->getOperationId(), |
| 161 | $route |
| 162 | ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return $routes; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param mixed $resource |
| 171 | * @param mixed $type |
| 172 | */ |
| 173 | public function supports($resource, $type = null): bool |
| 174 | { |
| 175 | return 'apie' === $type; |
| 176 | } |
| 177 | } |