Skip to content

Commit bb9a897

Browse files
bernardwiesnerBernard Wiesnertaylorotwell
authored
[9.x] Add option to disable cached view (#41859)
* add option to disable cached views * add tests for isExpired for useCache * formatting Co-authored-by: Bernard Wiesner <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 644c9d8 commit bb9a897

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/Illuminate/View/Compilers/Compiler.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,25 @@ abstract class Compiler
2929
*/
3030
protected $basePath;
3131

32+
/**
33+
* Determines if compiled views should be cached.
34+
*
35+
* @var bool
36+
*/
37+
protected $shouldCache;
38+
3239
/**
3340
* Create a new compiler instance.
3441
*
3542
* @param \Illuminate\Filesystem\Filesystem $files
3643
* @param string $cachePath
3744
* @param string $basePath
45+
* @param bool $shouldCache
3846
* @return void
3947
*
4048
* @throws \InvalidArgumentException
4149
*/
42-
public function __construct(Filesystem $files, $cachePath, $basePath = '')
50+
public function __construct(Filesystem $files, $cachePath, $basePath = '', $shouldCache = true)
4351
{
4452
if (! $cachePath) {
4553
throw new InvalidArgumentException('Please provide a valid cache path.');
@@ -48,6 +56,7 @@ public function __construct(Filesystem $files, $cachePath, $basePath = '')
4856
$this->files = $files;
4957
$this->cachePath = $cachePath;
5058
$this->basePath = $basePath;
59+
$this->shouldCache = $shouldCache;
5160
}
5261

5362
/**
@@ -69,6 +78,10 @@ public function getCompiledPath($path)
6978
*/
7079
public function isExpired($path)
7180
{
81+
if (! $this->shouldCache) {
82+
return true;
83+
}
84+
7285
$compiled = $this->getCompiledPath($path);
7386

7487
// If the compiled file doesn't exist we will indicate that the view is expired

src/Illuminate/View/ViewServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function registerBladeCompiler()
8989
$app['files'],
9090
$app['config']['view.compiled'],
9191
$app['config']->get('view.relative_hash', false) ? $app->basePath() : '',
92+
$app['config']->get('view.cache', true),
9293
), function ($blade) {
9394
$blade->component('dynamic-component', DynamicComponent::class);
9495
});

tests/View/ViewBladeCompilerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
3939
$this->assertTrue($compiler->isExpired('foo'));
4040
}
4141

42+
public function testIsExpiredReturnsFalseWhenUseCacheIsTrueAndNoFileModification()
43+
{
44+
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
45+
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(true);
46+
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(0);
47+
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(100);
48+
$this->assertFalse($compiler->isExpired('foo'));
49+
}
50+
51+
public function testIsExpiredReturnsTrueWhenUseCacheIsFalse()
52+
{
53+
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__, $basePath = '', $useCache = false);
54+
$this->assertTrue($compiler->isExpired('foo'));
55+
}
56+
4257
public function testCompilePathIsProperlyCreated()
4358
{
4459
$compiler = new BladeCompiler($this->getFiles(), __DIR__);

0 commit comments

Comments
 (0)