From 222ca3d0877313e8fff24ff845c472c25a7753f0 Mon Sep 17 00:00:00 2001 From: Kennedy Tedesco Date: Tue, 20 Dec 2022 09:00:44 -0300 Subject: [PATCH] Use faster hash algorithm (xxh128) --- src/Illuminate/View/Compilers/Compiler.php | 2 +- .../Compilers/Concerns/CompilesComponents.php | 2 +- src/Illuminate/View/Component.php | 2 +- .../View/Concerns/ManagesLayouts.php | 2 +- tests/View/Blade/BladeComponentsTest.php | 8 +++--- tests/View/ComponentTest.php | 8 +++--- tests/View/ViewBladeCompilerTest.php | 26 +++++++++---------- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Illuminate/View/Compilers/Compiler.php b/src/Illuminate/View/Compilers/Compiler.php index 90c5d608d3ee..ea27917353df 100755 --- a/src/Illuminate/View/Compilers/Compiler.php +++ b/src/Illuminate/View/Compilers/Compiler.php @@ -81,7 +81,7 @@ public function __construct( */ public function getCompiledPath($path) { - return $this->cachePath.'/'.sha1('v2'.Str::after($path, $this->basePath)).'.'.$this->compiledExtension; + return $this->cachePath.'/'.hash('xxh128', 'v2'.Str::after($path, $this->basePath)).'.'.$this->compiledExtension; } /** diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php index de565c67edea..67b2d3beb0ca 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php @@ -46,7 +46,7 @@ protected function compileComponent($expression) */ public static function newComponentHash(string $component) { - static::$componentHashStack[] = $hash = sha1($component); + static::$componentHashStack[] = $hash = hash('xxh128', $component); return $hash; } diff --git a/src/Illuminate/View/Component.php b/src/Illuminate/View/Component.php index 949803056425..51d83e2e7b1b 100644 --- a/src/Illuminate/View/Component.php +++ b/src/Illuminate/View/Component.php @@ -187,7 +187,7 @@ protected function createBladeViewFromString($factory, $contents) $directory = Container::getInstance()['config']->get('view.compiled') ); - if (! is_file($viewFile = $directory.'/'.sha1($contents).'.blade.php')) { + if (! is_file($viewFile = $directory.'/'.hash('xxh128', $contents).'.blade.php')) { if (! is_dir($directory)) { mkdir($directory, 0755, true); } diff --git a/src/Illuminate/View/Concerns/ManagesLayouts.php b/src/Illuminate/View/Concerns/ManagesLayouts.php index f04512658761..38cc56c7d5ac 100644 --- a/src/Illuminate/View/Concerns/ManagesLayouts.php +++ b/src/Illuminate/View/Concerns/ManagesLayouts.php @@ -178,7 +178,7 @@ public static function parentPlaceholder($section = '') if (! isset(static::$parentPlaceholder[$section])) { $salt = static::parentPlaceholderSalt(); - static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($salt.$section).'##'; + static::$parentPlaceholder[$section] = '##parent-placeholder-'.hash('xxh128', $salt.$section).'##'; } return static::$parentPlaceholder[$section]; diff --git a/tests/View/Blade/BladeComponentsTest.php b/tests/View/Blade/BladeComponentsTest.php index 9df949f8ac03..406f9290965f 100644 --- a/tests/View/Blade/BladeComponentsTest.php +++ b/tests/View/Blade/BladeComponentsTest.php @@ -16,7 +16,7 @@ public function testComponentsAreCompiled() public function testClassComponentsAreCompiled() { - $this->assertSame(' + $this->assertSame(' "bar"] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?> withName(\'test\'); ?> shouldRender()): ?> @@ -36,9 +36,9 @@ public function testEndComponentClassesAreCompiled() $this->assertSame('renderComponent(); ?> - - - + + + ', $this->compiler->compileString('@endcomponentClass')); } diff --git a/tests/View/ComponentTest.php b/tests/View/ComponentTest.php index c94aa422bace..18318f0038a5 100644 --- a/tests/View/ComponentTest.php +++ b/tests/View/ComponentTest.php @@ -59,7 +59,7 @@ public function testInlineViewsGetCreated() $this->viewFactory->shouldReceive('addNamespace')->once()->with('__components', '/tmp'); $component = new TestInlineViewComponent; - $this->assertSame('__components::c6327913fef3fca4518bcd7df1d0ff630758e241', $component->resolveView()); + $this->assertSame('__components::57b7a54afa0eb51fd9b88eec031c9e9e', $component->resolveView()); } public function testRegularViewsGetReturnedUsingViewHelper() @@ -191,7 +191,7 @@ public function testBladeViewCacheWithInlineViewComponent() ->with('__components', '/tmp') ->twice(); - $compiledViewName = '__components::c6327913fef3fca4518bcd7df1d0ff630758e241'; + $compiledViewName = '__components::57b7a54afa0eb51fd9b88eec031c9e9e'; $contents = '::Hello {{ $title }}'; $cacheKey = $component::class.$contents; @@ -227,8 +227,8 @@ public function testBladeViewCacheWithInlineViewComponentWhereRenderDependsOnPro ->with('__components', '/tmp') ->twice(); - $compiledViewNameA = '__components::6dcd4ce23d88e2ee9568ba546c007c63d9131c1b'; - $compiledViewNameB = '__components::ae4f281df5a5d0ff3cad6371f76d5c29b6d953ec'; + $compiledViewNameA = '__components::9b0498cbe3839becd0d496e05c553485'; + $compiledViewNameB = '__components::9d1b9bc4078a3e7274d3766ca02423f3'; $cacheAKey = $componentA::class.'::A'; $cacheBKey = $componentB::class.'::B'; diff --git a/tests/View/ViewBladeCompilerTest.php b/tests/View/ViewBladeCompilerTest.php index 71f21265dcce..b7ef8a4ea75c 100644 --- a/tests/View/ViewBladeCompilerTest.php +++ b/tests/View/ViewBladeCompilerTest.php @@ -18,7 +18,7 @@ protected function tearDown(): void public function testIsExpiredReturnsTrueIfCompiledFileDoesntExist() { $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); - $files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(false); + $files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(false); $this->assertTrue($compiler->isExpired('foo')); } @@ -33,18 +33,18 @@ public function testCannotConstructWithBadCachePath() public function testIsExpiredReturnsTrueWhenModificationTimesWarrant() { $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); - $files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(true); + $files->shouldReceive('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(true); $files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100); - $files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(0); + $files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(0); $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('exists')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(true); $files->shouldReceive('lastModified')->once()->with('foo')->andReturn(0); - $files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(100); + $files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php')->andReturn(100); $this->assertFalse($compiler->isExpired('foo')); } @@ -57,7 +57,7 @@ public function testIsExpiredReturnsTrueWhenUseCacheIsFalse() public function testCompilePathIsProperlyCreated() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); - $this->assertEquals(__DIR__.'/'.sha1('v2foo').'.php', $compiler->getCompiledPath('foo')); + $this->assertEquals(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', $compiler->getCompiledPath('foo')); } public function testCompileCompilesFileAndReturnsContents() @@ -65,7 +65,7 @@ public function testCompileCompilesFileAndReturnsContents() $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); $files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World'); $files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true); - $files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World'); + $files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World'); $compiler->compile('foo'); } @@ -75,7 +75,7 @@ public function testCompileCompilesFileAndReturnsContentsCreatingDirectory() $files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World'); $files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(false); $files->shouldReceive('makeDirectory')->once()->with(__DIR__, 0777, true, true); - $files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World'); + $files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World'); $compiler->compile('foo'); } @@ -84,7 +84,7 @@ public function testCompileCompilesAndGetThePath() $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); $files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World'); $files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true); - $files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World'); + $files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World'); $compiler->compile('foo'); $this->assertSame('foo', $compiler->getPath()); } @@ -101,7 +101,7 @@ public function testCompileWithPathSetBefore() $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); $files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World'); $files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true); - $files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World'); + $files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', 'Hello World'); // set path before compilation $compiler->setPath('foo'); // trigger compilation with $path @@ -132,7 +132,7 @@ public function testIncludePathToTemplate($content, $compiled) $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); $files->shouldReceive('get')->once()->with('foo')->andReturn($content); $files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true); - $files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', $compiled); + $files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2foo').'.php', $compiled); $compiler->compile('foo'); } @@ -187,7 +187,7 @@ public function testDontIncludeEmptyPath() $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); $files->shouldReceive('get')->once()->with('')->andReturn('Hello World'); $files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true); - $files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World'); + $files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2').'.php', 'Hello World'); $compiler->setPath(''); $compiler->compile(); } @@ -197,7 +197,7 @@ public function testDontIncludeNullPath() $compiler = new BladeCompiler($files = $this->getFiles(), __DIR__); $files->shouldReceive('get')->once()->with(null)->andReturn('Hello World'); $files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true); - $files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World'); + $files->shouldReceive('put')->once()->with(__DIR__.'/'.hash('xxh128', 'v2').'.php', 'Hello World'); $compiler->setPath(null); $compiler->compile(); }