Skip to content

Commit aa2a02d

Browse files
[9.x] Use Relative View Path Name When Hashing Compiled Views (#39642)
* [9.X] Use Relative View Path When Hashing Compiled Views * Set basePath default * formatting * add config option Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 8f9ddea commit aa2a02d

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/Illuminate/View/Compilers/Compiler.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,51 @@
33
namespace Illuminate\View\Compilers;
44

55
use Illuminate\Filesystem\Filesystem;
6+
use Illuminate\Support\Str;
67
use InvalidArgumentException;
78

89
abstract class Compiler
910
{
1011
/**
11-
* The Filesystem instance.
12+
* The filesystem instance.
1213
*
1314
* @var \Illuminate\Filesystem\Filesystem
1415
*/
1516
protected $files;
1617

1718
/**
18-
* Get the cache path for the compiled views.
19+
* The cache path for the compiled views.
1920
*
2021
* @var string
2122
*/
2223
protected $cachePath;
2324

25+
/**
26+
* The base path that should be removed from paths before hashing.
27+
*
28+
* @var string
29+
*/
30+
protected $basePath;
31+
2432
/**
2533
* Create a new compiler instance.
2634
*
2735
* @param \Illuminate\Filesystem\Filesystem $files
2836
* @param string $cachePath
37+
* @param string $basePath
2938
* @return void
3039
*
3140
* @throws \InvalidArgumentException
3241
*/
33-
public function __construct(Filesystem $files, $cachePath)
42+
public function __construct(Filesystem $files, $cachePath, $basePath = '')
3443
{
3544
if (! $cachePath) {
3645
throw new InvalidArgumentException('Please provide a valid cache path.');
3746
}
3847

3948
$this->files = $files;
4049
$this->cachePath = $cachePath;
50+
$this->basePath = $basePath;
4151
}
4252

4353
/**
@@ -48,7 +58,7 @@ public function __construct(Filesystem $files, $cachePath)
4858
*/
4959
public function getCompiledPath($path)
5060
{
51-
return $this->cachePath.'/'.sha1($path).'.php';
61+
return $this->cachePath.'/'.sha1(Str::after($path, $this->basePath)).'.php';
5262
}
5363

5464
/**

src/Illuminate/View/ViewServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ public function registerViewFinder()
8585
public function registerBladeCompiler()
8686
{
8787
$this->app->singleton('blade.compiler', function ($app) {
88-
return tap(new BladeCompiler($app['files'], $app['config']['view.compiled']), function ($blade) {
88+
return tap(new BladeCompiler(
89+
$app['files'],
90+
$app['config']['view.compiled'],
91+
$app['config']->get('view.relative_hash', false) ? $app->basePath() : '',
92+
), function ($blade) {
8993
$blade->component('dynamic-component', DynamicComponent::class);
9094
});
9195
});

0 commit comments

Comments
 (0)