Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add option to disable cached view #41859

Merged
merged 3 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Illuminate/View/Compilers/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,25 @@ abstract class Compiler
*/
protected $basePath;

/**
* Determines if compiled views should be cached.
*
* @var bool
*/
protected $shouldCache;

/**
* Create a new compiler instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $cachePath
* @param string $basePath
* @param bool $shouldCache
* @return void
*
* @throws \InvalidArgumentException
*/
public function __construct(Filesystem $files, $cachePath, $basePath = '')
public function __construct(Filesystem $files, $cachePath, $basePath = '', $shouldCache = true)
{
if (! $cachePath) {
throw new InvalidArgumentException('Please provide a valid cache path.');
Expand All @@ -48,6 +56,7 @@ public function __construct(Filesystem $files, $cachePath, $basePath = '')
$this->files = $files;
$this->cachePath = $cachePath;
$this->basePath = $basePath;
$this->shouldCache = $shouldCache;
}

/**
Expand All @@ -69,6 +78,10 @@ public function getCompiledPath($path)
*/
public function isExpired($path)
{
if (! $this->shouldCache) {
return true;
}

$compiled = $this->getCompiledPath($path);

// If the compiled file doesn't exist we will indicate that the view is expired
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/View/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.cache', true),
), function ($blade) {
$blade->component('dynamic-component', DynamicComponent::class);
});
Expand Down
15 changes: 15 additions & 0 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand Down