From f845a5ab8b67e1d0b11779f1754bcc3f2193d67b Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 28 Oct 2018 12:59:49 +0100 Subject: Added caching for templating and routing See #486 (Implement caching) --- src/Application.php | 6 ++++- src/Middleware/RouteDispatcherServiceProvider.php | 33 +++++++++++++++++++---- src/Renderer/TwigServiceProvider.php | 16 ++++++++++- 3 files changed, 48 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Application.php b/src/Application.php index d12d3e31..02518423 100644 --- a/src/Application.php +++ b/src/Application.php @@ -108,8 +108,12 @@ class Application extends Container $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config'); $this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources'); $this->instance('path.assets', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'assets'); - $this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views'); $this->instance('path.lang', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'lang'); + $this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views'); + $this->instance('path.storage', $appPath . DIRECTORY_SEPARATOR . 'storage'); + $this->instance('path.cache', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'cache'); + $this->instance('path.cache.routes', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'routes.cache.php'); + $this->instance('path.cache.views', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'views'); } /** 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); } } diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php index 57ebe9e5..d7b6bd09 100644 --- a/src/Renderer/TwigServiceProvider.php +++ b/src/Renderer/TwigServiceProvider.php @@ -62,7 +62,21 @@ class TwigServiceProvider extends ServiceProvider $this->app->instance(TwigLoaderInterface::class, $twigLoader); $this->app->instance('twig.loader', $twigLoader); - $twig = $this->app->make(Twig::class); + $cache = $this->app->get('path.cache.views'); + if ($config->get('environment') == 'development') { + $cache = false; + } + + $twig = $this->app->make( + Twig::class, + [ + 'options' => [ + 'cache' => $cache, + 'auto_reload' => true, + 'strict_variables' => ($config->get('environment') == 'development'), + ], + ] + ); $this->app->instance(Twig::class, $twig); $this->app->instance('twig.environment', $twig); -- cgit v1.2.3-70-g09d2