Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dbdbc29

Browse files
committed
Use faster hash algorithm (xxh128) on PHP 8.1
1 parent 7e5ba95 commit dbdbc29

9 files changed

Lines changed: 20 additions & 8 deletions

File tree

src/Cache/FilesystemCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct($directory, $options = 0)
3535

3636
public function generateKey($name, $className)
3737
{
38-
$hash = hash('sha256', $className);
38+
$hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $className);
3939

4040
return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
4141
}

src/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public function outdent($step = 1)
281281

282282
public function getVarName()
283283
{
284-
return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->varNameSalt++));
284+
return sprintf('__internal_%s', hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', __METHOD__.$this->varNameSalt++));
285285
}
286286
}
287287

src/Environment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public function getTemplateClass($name, $index = null)
350350
{
351351
$key = $this->getLoader()->getCacheKey($name).$this->optionsHash;
352352

353-
return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '___'.$index);
353+
return $this->templateClassPrefix.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $key).(null === $index ? '' : '___'.$index);
354354
}
355355

356356
/**
@@ -525,7 +525,7 @@ public function loadClass($cls, $name, $index = null)
525525
*/
526526
public function createTemplate($template, $name = null)
527527
{
528-
$hash = hash('sha256', $template, false);
528+
$hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $template, false);
529529
if (null !== $name) {
530530
$name = sprintf('%s (string template %s)', $name, $hash);
531531
} else {

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getEnvironment()
6767

6868
public function getVarName()
6969
{
70-
return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->stream->getSourceContext()->getCode().$this->varNameSalt++));
70+
return sprintf('__internal_%s', hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', __METHOD__.$this->stream->getSourceContext()->getCode().$this->varNameSalt++));
7171
}
7272

7373
/**

src/Profiler/NodeVisitor/ProfilerNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function doLeaveNode(Node $node, Environment $env)
6868

6969
private function getVarName()
7070
{
71-
return sprintf('__internal_%s', hash('sha256', $this->extensionName));
71+
return sprintf('__internal_%s', hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $this->extensionName));
7272
}
7373

7474
public function getPriority()

src/Test/IntegrationTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e
185185

186186
$p = new \ReflectionProperty($twig, 'templateClassPrefix');
187187
$p->setAccessible(true);
188-
$p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_');
188+
$p->setValue($twig, '__TwigTemplate_'.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', uniqid(mt_rand(), true), false).'_');
189189

190190
try {
191191
$template = $twig->load('index.twig');

tests/Cache/FilesystemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FilesystemTest extends \PHPUnit\Framework\TestCase
2222

2323
protected function setUp(): void
2424
{
25-
$nonce = hash('sha256', uniqid(mt_rand(), true));
25+
$nonce = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', uniqid(mt_rand(), true));
2626
$this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$nonce;
2727
$this->directory = sys_get_temp_dir().'/twig-test';
2828
$this->cache = new FilesystemCache($this->directory);

tests/Fixtures/functions/template_from_string_error.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
"template_from_string" function
3+
--CONDITION--
4+
version_compare(phpversion(), '8.1.0-dev', '<')
35
--TEMPLATE--
46
{% include template_from_string("{{ not a Twig template ", "foo.twig") %}
57
--DATA--
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
"template_from_string" function
3+
--CONDITION--
4+
version_compare(phpversion(), '8.1.0-rc', '>=')
5+
--TEMPLATE--
6+
{% include template_from_string("{{ not a Twig template ", "foo.twig") %}
7+
--DATA--
8+
return []
9+
--EXCEPTION--
10+
Twig\Error\SyntaxError: Unclosed "variable" in "foo.twig (string template 85e7b092afbbcd36f11981c2ef8f1569)" at line 1.

0 commit comments

Comments
 (0)