Added caching for templating and routing

See #486 (Implement caching)
This commit is contained in:
Igor Scheller 2018-10-28 12:59:49 +01:00 committed by msquare
parent 944c29b964
commit f845a5ab8b
9 changed files with 74 additions and 17 deletions

View File

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

View File

@ -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');
}
/**

View File

@ -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);
}
}

View File

@ -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);

1
storage/cache/.gitignore vendored Normal file
View File

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

2
storage/cache/views/.gitignore vendored Normal file
View File

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

View File

@ -51,6 +51,10 @@ class ApplicationTest extends TestCase
$this->assertTrue($app->has('path.lang'));
$this->assertTrue($app->has('path.resources'));
$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('.') . '/config', $app->get('path.config'));

View File

@ -2,6 +2,7 @@
namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Config\Config;
use Engelsystem\Middleware\LegacyMiddleware;
use Engelsystem\Middleware\RouteDispatcher;
use Engelsystem\Middleware\RouteDispatcherServiceProvider;
@ -18,10 +19,18 @@ class RouteDispatcherServiceProviderTest extends ServiceProviderTest
*/
public function testRegister()
{
/** @var ContextualBindingBuilder|MockObject $bindingBuilder */
$bindingBuilder = $this->createMock(ContextualBindingBuilder::class);
/** @var FastRouteDispatcher|MockObject $routeDispatcher */
$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())
->method('alias')

View File

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