From 8ad88c8ab8b563cc2f562c531c4dd258f96de7d0 Mon Sep 17 00:00:00 2001 From: Bogdan Kharchenko Date: Tue, 16 Nov 2021 21:38:17 -0500 Subject: [PATCH 1/4] [9.X] Use Relative View Path When Hashing Compiled Views --- src/Illuminate/View/Compilers/Compiler.php | 26 +++++++++++++++++++-- src/Illuminate/View/ViewServiceProvider.php | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index 2a943e0f6309..242b9c38c1d0 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -3,6 +3,7 @@ namespace Illuminate\View\Compilers; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Str; use InvalidArgumentException; abstract class Compiler @@ -21,6 +22,13 @@ abstract class Compiler */ protected $cachePath; + /** + * Get the base path of your application. + * + * @var string + */ + protected $basePath; + /** * Create a new compiler instance. * @@ -30,7 +38,7 @@ abstract class Compiler * * @throws \InvalidArgumentException */ - public function __construct(Filesystem $files, $cachePath) + public function __construct(Filesystem $files, $cachePath, $basePath) { if (! $cachePath) { throw new InvalidArgumentException('Please provide a valid cache path.'); @@ -38,6 +46,7 @@ public function __construct(Filesystem $files, $cachePath) $this->files = $files; $this->cachePath = $cachePath; + $this->basePath = $basePath; } /** @@ -48,7 +57,20 @@ public function __construct(Filesystem $files, $cachePath) */ public function getCompiledPath($path) { - return $this->cachePath.'/'.sha1($path).'.php'; + $relativePath = $this->getRelativeViewPath($path); + + return $this->cachePath.'/'.sha1($relativePath).'.php'; + } + + /** + * Get the relative path of a view. + * + * @param string $path + * @return string + */ + public function getRelativeViewPath($path) + { + return Str::after($path, $this->basePath); } /** diff --git a/src/Illuminate/View/ViewServiceProvider.php b/src/Illuminate/View/ViewServiceProvider.php index 7eb731f53cfb..ea47f7ade732 100755 --- a/src/Illuminate/View/ViewServiceProvider.php +++ b/src/Illuminate/View/ViewServiceProvider.php @@ -85,7 +85,7 @@ public function registerViewFinder() public function registerBladeCompiler() { $this->app->singleton('blade.compiler', function ($app) { - return tap(new BladeCompiler($app['files'], $app['config']['view.compiled']), function ($blade) { + return tap(new BladeCompiler($app['files'], $app['config']['view.compiled'], $app['path.base']), function ($blade) { $blade->component('dynamic-component', DynamicComponent::class); }); }); From 87349a33fdd6a59410e088447f2ec1cdb39e39f3 Mon Sep 17 00:00:00 2001 From: Bogdan Kharchenko Date: Tue, 16 Nov 2021 22:05:02 -0500 Subject: [PATCH 2/4] Set basePath default --- src/Illuminate/View/Compilers/Compiler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index 242b9c38c1d0..387a4e093186 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -38,7 +38,7 @@ abstract class Compiler * * @throws \InvalidArgumentException */ - public function __construct(Filesystem $files, $cachePath, $basePath) + public function __construct(Filesystem $files, $cachePath, $basePath = '') { if (! $cachePath) { throw new InvalidArgumentException('Please provide a valid cache path.'); From 3ff049db3d4a72f6b2f397a07bc0868f69ae4f4e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 17 Nov 2021 08:51:15 -0600 Subject: [PATCH 3/4] formatting --- src/Illuminate/View/Compilers/Compiler.php | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index 387a4e093186..30233884e9eb 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -9,21 +9,21 @@ abstract class Compiler { /** - * The Filesystem instance. + * The filesystem instance. * * @var \Illuminate\Filesystem\Filesystem */ protected $files; /** - * Get the cache path for the compiled views. + * The cache path for the compiled views. * * @var string */ protected $cachePath; /** - * Get the base path of your application. + * The base path that should be removed from paths before hashing. * * @var string */ @@ -34,6 +34,7 @@ abstract class Compiler * * @param \Illuminate\Filesystem\Filesystem $files * @param string $cachePath + * @param string $basePath * @return void * * @throws \InvalidArgumentException @@ -57,20 +58,7 @@ public function __construct(Filesystem $files, $cachePath, $basePath = '') */ public function getCompiledPath($path) { - $relativePath = $this->getRelativeViewPath($path); - - return $this->cachePath.'/'.sha1($relativePath).'.php'; - } - - /** - * Get the relative path of a view. - * - * @param string $path - * @return string - */ - public function getRelativeViewPath($path) - { - return Str::after($path, $this->basePath); + return $this->cachePath.'/'.sha1(Str::after($path, $this->basePath)).'.php'; } /** From 67b1190a65fd59626730f46ff7e7fdcb5e723f73 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 17 Nov 2021 08:53:50 -0600 Subject: [PATCH 4/4] add config option --- src/Illuminate/View/ViewServiceProvider.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/View/ViewServiceProvider.php b/src/Illuminate/View/ViewServiceProvider.php index ea47f7ade732..9baeb2c83777 100755 --- a/src/Illuminate/View/ViewServiceProvider.php +++ b/src/Illuminate/View/ViewServiceProvider.php @@ -85,7 +85,11 @@ public function registerViewFinder() public function registerBladeCompiler() { $this->app->singleton('blade.compiler', function ($app) { - return tap(new BladeCompiler($app['files'], $app['config']['view.compiled'], $app['path.base']), function ($blade) { + return tap(new BladeCompiler( + $app['files'], + $app['config']['view.compiled'], + $app['config']->get('view.relative_hash', false) ? $app->basePath() : '', + ), function ($blade) { $blade->component('dynamic-component', DynamicComponent::class); }); });