diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-10-28 12:59:49 +0100 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2018-11-24 16:30:36 +0100 |
commit | f845a5ab8b67e1d0b11779f1754bcc3f2193d67b (patch) | |
tree | e4893832772d468873b047a443461175be774c88 /src/Middleware | |
parent | 944c29b96429ec95ac1371cb33cc43704a60c7b1 (diff) |
Added caching for templating and routing
See #486 (Implement caching)
Diffstat (limited to 'src/Middleware')
-rw-r--r-- | src/Middleware/RouteDispatcherServiceProvider.php | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/Middleware/RouteDispatcherServiceProvider.php b/src/Middleware/RouteDispatcherServiceProvider.php index 3b4fa183..193510f3 100644 --- a/src/Middleware/RouteDispatcherServiceProvider.php +++ b/src/Middleware/RouteDispatcherServiceProvider.php @@ -2,6 +2,7 @@ namespace Engelsystem\Middleware; +use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; use FastRoute\Dispatcher as FastRouteDispatcher; use FastRoute\RouteCollector; @@ -11,13 +12,24 @@ class RouteDispatcherServiceProvider extends ServiceProvider { public function register() { + /** @var Config $config */ + $config = $this->app->get('config'); + + $options = [ + 'cacheFile' => $this->app->get('path.cache.routes'), + ]; + + if ($config->get('environment') == 'development') { + $options['cacheDisabled'] = true; + } + $this->app->alias(RouteDispatcher::class, 'route.dispatcher'); $this->app ->when(RouteDispatcher::class) ->needs(FastRouteDispatcher::class) - ->give(function () { - return $this->generateRouting(); + ->give(function () use ($options) { + return $this->generateRouting($options); }); $this->app @@ -29,13 +41,24 @@ class RouteDispatcherServiceProvider extends ServiceProvider /** * Includes the routes.php file * + * @param array $options * @return FastRouteDispatcher * @codeCoverageIgnore */ - function generateRouting() + protected function generateRouting(array $options = []) { - return \FastRoute\simpleDispatcher(function (RouteCollector $route) { + $routesFile = config_path('routes.php'); + $routesCacheFile = $this->app->get('path.cache.routes'); + + if ( + file_exists($routesCacheFile) + && filemtime($routesFile) > filemtime($routesCacheFile) + ) { + unlink($routesCacheFile); + } + + return \FastRoute\cachedDispatcher(function (RouteCollector $route) { require config_path('routes.php'); - }); + }, $options); } } |