Added caching for templating and routing

See #486 (Implement caching)
main
Igor Scheller 6 years ago committed by msquare
parent 944c29b964
commit f845a5ab8b

@ -132,7 +132,7 @@ function UserWorkLog_new($userId)
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time())); $work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time()));
/** @var Carbon $buildup */ /** @var Carbon $buildup */
$buildup = $buildup = config('buildup_start'); $buildup = config('buildup_start');
if (!empty($buildup)) { if (!empty($buildup)) {
$work_date = $buildup->format('Y-m-d H:i'); $work_date = $buildup->format('Y-m-d H:i');
} }

@ -108,8 +108,12 @@ class Application extends Container
$this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config'); $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config');
$this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources'); $this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources');
$this->instance('path.assets', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'assets'); $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.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');
} }
/** /**

@ -2,6 +2,7 @@
namespace Engelsystem\Middleware; namespace Engelsystem\Middleware;
use Engelsystem\Config\Config;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use FastRoute\Dispatcher as FastRouteDispatcher; use FastRoute\Dispatcher as FastRouteDispatcher;
use FastRoute\RouteCollector; use FastRoute\RouteCollector;
@ -11,13 +12,24 @@ class RouteDispatcherServiceProvider extends ServiceProvider
{ {
public function register() 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->alias(RouteDispatcher::class, 'route.dispatcher');
$this->app $this->app
->when(RouteDispatcher::class) ->when(RouteDispatcher::class)
->needs(FastRouteDispatcher::class) ->needs(FastRouteDispatcher::class)
->give(function () { ->give(function () use ($options) {
return $this->generateRouting(); return $this->generateRouting($options);
}); });
$this->app $this->app
@ -29,13 +41,24 @@ class RouteDispatcherServiceProvider extends ServiceProvider
/** /**
* Includes the routes.php file * Includes the routes.php file
* *
* @param array $options
* @return FastRouteDispatcher * @return FastRouteDispatcher
* @codeCoverageIgnore * @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'); require config_path('routes.php');
}); }, $options);
} }
} }

@ -62,7 +62,21 @@ class TwigServiceProvider extends ServiceProvider
$this->app->instance(TwigLoaderInterface::class, $twigLoader); $this->app->instance(TwigLoaderInterface::class, $twigLoader);
$this->app->instance('twig.loader', $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::class, $twig);
$this->app->instance('twig.environment', $twig); $this->app->instance('twig.environment', $twig);

@ -0,0 +1 @@
/routes.cache.php

@ -0,0 +1,2 @@
/*
!.gitignore

@ -51,6 +51,10 @@ class ApplicationTest extends TestCase
$this->assertTrue($app->has('path.lang')); $this->assertTrue($app->has('path.lang'));
$this->assertTrue($app->has('path.resources')); $this->assertTrue($app->has('path.resources'));
$this->assertTrue($app->has('path.views')); $this->assertTrue($app->has('path.views'));
$this->assertTrue($app->has('path.storage'));
$this->assertTrue($app->has('path.cache'));
$this->assertTrue($app->has('path.cache.routes'));
$this->assertTrue($app->has('path.cache.views'));
$this->assertEquals(realpath('.'), $app->path()); $this->assertEquals(realpath('.'), $app->path());
$this->assertEquals(realpath('.') . '/config', $app->get('path.config')); $this->assertEquals(realpath('.') . '/config', $app->get('path.config'));

@ -2,6 +2,7 @@
namespace Engelsystem\Test\Unit\Middleware; namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Config\Config;
use Engelsystem\Middleware\LegacyMiddleware; use Engelsystem\Middleware\LegacyMiddleware;
use Engelsystem\Middleware\RouteDispatcher; use Engelsystem\Middleware\RouteDispatcher;
use Engelsystem\Middleware\RouteDispatcherServiceProvider; use Engelsystem\Middleware\RouteDispatcherServiceProvider;
@ -18,10 +19,18 @@ class RouteDispatcherServiceProviderTest extends ServiceProviderTest
*/ */
public function testRegister() public function testRegister()
{ {
/** @var ContextualBindingBuilder|MockObject $bindingBuilder */
$bindingBuilder = $this->createMock(ContextualBindingBuilder::class); $bindingBuilder = $this->createMock(ContextualBindingBuilder::class);
/** @var FastRouteDispatcher|MockObject $routeDispatcher */
$routeDispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class); $routeDispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class);
$config = new Config(['environment' => 'development']);
$app = $this->getApp(['alias', 'when']); $app = $this->getApp(['alias', 'when', 'get']);
$app->expects($this->exactly(2))
->method('get')
->withConsecutive(['config'], ['path.cache.routes'])
->willReturn($config, '/foo/routes.cache');
$app->expects($this->once()) $app->expects($this->once())
->method('alias') ->method('alias')

@ -91,7 +91,7 @@ class TwigServiceProviderTest extends ServiceProviderTest
/** /**
* @covers \Engelsystem\Renderer\TwigServiceProvider::registerTwigEngine * @covers \Engelsystem\Renderer\TwigServiceProvider::registerTwigEngine
*/ */
public function testRegisterTWigEngine() public function testRegisterTwigEngine()
{ {
/** @var TwigEngine|MockObject $htmlEngine */ /** @var TwigEngine|MockObject $htmlEngine */
$twigEngine = $this->createMock(TwigEngine::class); $twigEngine = $this->createMock(TwigEngine::class);
@ -114,7 +114,7 @@ class TwigServiceProviderTest extends ServiceProviderTest
->method('make') ->method('make')
->withConsecutive( ->withConsecutive(
[TwigLoader::class, ['paths' => $viewsPath]], [TwigLoader::class, ['paths' => $viewsPath]],
[Twig::class], [Twig::class, ['options' => ['cache' => false, 'auto_reload' => true, 'strict_variables' => true]]],
[TwigEngine::class] [TwigEngine::class]
)->willReturnOnConsecutiveCalls( )->willReturnOnConsecutiveCalls(
$twigLoader, $twigLoader,
@ -133,17 +133,17 @@ class TwigServiceProviderTest extends ServiceProviderTest
['renderer.twigEngine', $twigEngine] ['renderer.twigEngine', $twigEngine]
); );
$app->expects($this->exactly(2)) $app->expects($this->exactly(3))
->method('get') ->method('get')
->withConsecutive(['path.views'], ['config']) ->withConsecutive(['path.views'], ['config'], ['path.cache.views'])
->willReturnOnConsecutiveCalls($viewsPath, $config); ->willReturnOnConsecutiveCalls($viewsPath, $config, 'cache/views');
$this->setExpects($app, 'tag', ['renderer.twigEngine', ['renderer.engine']]); $this->setExpects($app, 'tag', ['renderer.twigEngine', ['renderer.engine']]);
$config->expects($this->once()) $config->expects($this->exactly(3))
->method('get') ->method('get')
->with('timezone') ->withConsecutive(['environment'], ['environment'], ['timezone'])
->willReturn('The/World'); ->willReturnOnConsecutiveCalls('development', 'development', 'The/World');
$twig->expects($this->once()) $twig->expects($this->once())
->method('getExtension') ->method('getExtension')

Loading…
Cancel
Save