Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| RouteDispatcherServiceProvider | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| register | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
| generateRouting | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Middleware; |
| 6 | |
| 7 | use Engelsystem\Config\Config; |
| 8 | use Engelsystem\Container\ServiceProvider; |
| 9 | use FastRoute\Dispatcher as FastRouteDispatcher; |
| 10 | use FastRoute\RouteCollector; |
| 11 | use Psr\Http\Server\MiddlewareInterface; |
| 12 | |
| 13 | use function FastRoute\cachedDispatcher as FRCashedDispatcher; |
| 14 | |
| 15 | class RouteDispatcherServiceProvider extends ServiceProvider |
| 16 | { |
| 17 | public function register(): void |
| 18 | { |
| 19 | /** @var Config $config */ |
| 20 | $config = $this->app->get('config'); |
| 21 | |
| 22 | $options = [ |
| 23 | 'cacheFile' => $this->app->get('path.cache.routes'), |
| 24 | ]; |
| 25 | |
| 26 | if ($config->get('environment') == 'development') { |
| 27 | $options['cacheDisabled'] = true; |
| 28 | } |
| 29 | |
| 30 | $this->app->alias(RouteDispatcher::class, 'route.dispatcher'); |
| 31 | |
| 32 | $this->app |
| 33 | ->when(RouteDispatcher::class) |
| 34 | ->needs(FastRouteDispatcher::class) |
| 35 | ->give(function () use ($options) { |
| 36 | return $this->generateRouting($options); |
| 37 | }); |
| 38 | |
| 39 | $this->app |
| 40 | ->when(RouteDispatcher::class) |
| 41 | ->needs(MiddlewareInterface::class) |
| 42 | ->give(LegacyMiddleware::class); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Includes the routes.php file |
| 47 | * |
| 48 | * @codeCoverageIgnore |
| 49 | */ |
| 50 | protected function generateRouting(array $options = []): FastRouteDispatcher |
| 51 | { |
| 52 | $routesFile = config_path('routes.php'); |
| 53 | $routesCacheFile = $this->app->get('path.cache.routes'); |
| 54 | |
| 55 | if ( |
| 56 | file_exists($routesCacheFile) |
| 57 | && filemtime($routesFile) > filemtime($routesCacheFile) |
| 58 | ) { |
| 59 | unlink($routesCacheFile); |
| 60 | } |
| 61 | |
| 62 | return FRCashedDispatcher(function (RouteCollector $route): void { |
| 63 | require config_path('routes.php'); |
| 64 | }, $options); |
| 65 | } |
| 66 | } |