From 3a7d8a3f5fc0a5a86b093fe78da36ecf9f444d02 Mon Sep 17 00:00:00 2001 From: Bernard Wiesner Date: Thu, 7 Apr 2022 10:19:53 +0900 Subject: [PATCH 1/3] add option to disable cached views --- src/Illuminate/View/Compilers/Compiler.php | 15 ++++++++++++++- src/Illuminate/View/ViewServiceProvider.php | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index 42aa0f6d7be6..1d31cf0e8f64 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -29,17 +29,25 @@ abstract class Compiler */ protected $basePath; + /** + * Determines if cached view should be used. + * + * @var bool + */ + protected $useCache; + /** * Create a new compiler instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param string $cachePath * @param string $basePath + * @param bool $useCache * @return void * * @throws \InvalidArgumentException */ - public function __construct(Filesystem $files, $cachePath, $basePath = '') + public function __construct(Filesystem $files, $cachePath, $basePath = '', $useCache = true) { if (! $cachePath) { throw new InvalidArgumentException('Please provide a valid cache path.'); @@ -48,6 +56,7 @@ public function __construct(Filesystem $files, $cachePath, $basePath = '') $this->files = $files; $this->cachePath = $cachePath; $this->basePath = $basePath; + $this->useCache = $useCache; } /** @@ -69,6 +78,10 @@ public function getCompiledPath($path) */ public function isExpired($path) { + if (! $this->useCache) { + return true; + } + $compiled = $this->getCompiledPath($path); // If the compiled file doesn't exist we will indicate that the view is expired diff --git a/src/Illuminate/View/ViewServiceProvider.php b/src/Illuminate/View/ViewServiceProvider.php index 9baeb2c83777..d69a0a1834b9 100755 --- a/src/Illuminate/View/ViewServiceProvider.php +++ b/src/Illuminate/View/ViewServiceProvider.php @@ -89,6 +89,7 @@ public function registerBladeCompiler() $app['files'], $app['config']['view.compiled'], $app['config']->get('view.relative_hash', false) ? $app->basePath() : '', + $app['config']->get('view.use_cache', true), ), function ($blade) { $blade->component('dynamic-component', DynamicComponent::class); }); From 877f851e01dce61ca01c39eb1dca614093ac005c Mon Sep 17 00:00:00 2001 From: Bernard Wiesner Date: Thu, 7 Apr 2022 11:00:18 +0900 Subject: [PATCH 2/3] add tests for isExpired for useCache --- tests/View/ViewBladeCompilerTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/View/ViewBladeCompilerTest.php b/tests/View/ViewBladeCompilerTest.php index 2be9e8760fbf..e0841c949732 100644 --- a/tests/View/ViewBladeCompilerTest.php +++ b/tests/View/ViewBladeCompilerTest.php @@ -39,6 +39,21 @@ public function testIsExpiredReturnsTrueWhenModificationTimesWarrant() $this->assertTrue($compiler->isExpired('foo')); } + public function testIsExpiredReturnsFalseWhenUseCacheIsTrueAndNoFileModification() + { + $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); + $files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(true); + $files->shouldReceive('lastModified')->once()->with('foo')->andReturn(0); + $files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(100); + $this->assertFalse($compiler->isExpired('foo')); + } + + public function testIsExpiredReturnsTrueWhenUseCacheIsFalse() + { + $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__, $basePath = '', $useCache = false); + $this->assertTrue($compiler->isExpired('foo')); + } + public function testCompilePathIsProperlyCreated() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); From 46f317704e5977eb0c29f0e795b5d55a7c7aaeb2 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Apr 2022 14:54:59 -0500 Subject: [PATCH 3/3] formatting --- src/Illuminate/View/Compilers/Compiler.php | 12 ++++++------ src/Illuminate/View/ViewServiceProvider.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index 1d31cf0e8f64..3237d5aed926 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -30,11 +30,11 @@ abstract class Compiler protected $basePath; /** - * Determines if cached view should be used. + * Determines if compiled views should be cached. * * @var bool */ - protected $useCache; + protected $shouldCache; /** * Create a new compiler instance. @@ -42,12 +42,12 @@ abstract class Compiler * @param \Illuminate\Filesystem\Filesystem $files * @param string $cachePath * @param string $basePath - * @param bool $useCache + * @param bool $shouldCache * @return void * * @throws \InvalidArgumentException */ - public function __construct(Filesystem $files, $cachePath, $basePath = '', $useCache = true) + public function __construct(Filesystem $files, $cachePath, $basePath = '', $shouldCache = true) { if (! $cachePath) { throw new InvalidArgumentException('Please provide a valid cache path.'); @@ -56,7 +56,7 @@ public function __construct(Filesystem $files, $cachePath, $basePath = '', $useC $this->files = $files; $this->cachePath = $cachePath; $this->basePath = $basePath; - $this->useCache = $useCache; + $this->shouldCache = $shouldCache; } /** @@ -78,7 +78,7 @@ public function getCompiledPath($path) */ public function isExpired($path) { - if (! $this->useCache) { + if (! $this->shouldCache) { return true; } diff --git a/src/Illuminate/View/ViewServiceProvider.php b/src/Illuminate/View/ViewServiceProvider.php index d69a0a1834b9..5a53f422868e 100755 --- a/src/Illuminate/View/ViewServiceProvider.php +++ b/src/Illuminate/View/ViewServiceProvider.php @@ -89,7 +89,7 @@ public function registerBladeCompiler() $app['files'], $app['config']['view.compiled'], $app['config']->get('view.relative_hash', false) ? $app->basePath() : '', - $app['config']->get('view.use_cache', true), + $app['config']->get('view.cache', true), ), function ($blade) { $blade->component('dynamic-component', DynamicComponent::class); });