diff --git a/src/Cache/FilesystemCache.php b/src/Cache/FilesystemCache.php index cd6b663b168..c32c81884ba 100644 --- a/src/Cache/FilesystemCache.php +++ b/src/Cache/FilesystemCache.php @@ -35,7 +35,7 @@ public function __construct($directory, $options = 0) public function generateKey($name, $className) { - $hash = hash('sha256', $className); + $hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $className); return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php'; } diff --git a/src/Environment.php b/src/Environment.php index 894c862cb12..9a69d735880 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -298,7 +298,7 @@ public function getTemplateClass($name, $index = null) { $key = $this->getLoader()->getCacheKey($name).$this->optionsHash; - return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '___'.$index); + return $this->templateClassPrefix.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $key).(null === $index ? '' : '___'.$index); } /** @@ -445,7 +445,7 @@ public function loadClass($cls, $name, $index = null) */ public function createTemplate($template, string $name = null) { - $hash = hash('sha256', $template, false); + $hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $template, false); if (null !== $name) { $name = sprintf('%s (string template %s)', $name, $hash); } else { diff --git a/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php b/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php index 8e6028c6e0a..8b7c18adb67 100644 --- a/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php +++ b/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php @@ -33,7 +33,7 @@ final class ProfilerNodeVisitor extends AbstractNodeVisitor public function __construct(string $extensionName) { $this->extensionName = $extensionName; - $this->varName = sprintf('__internal_%s', hash('sha256', $extensionName)); + $this->varName = sprintf('__internal_%s', hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $extensionName)); } protected function doEnterNode(Node $node, Environment $env) diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index 544708986e4..df880a84128 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -186,7 +186,7 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e // avoid using the same PHP class name for different cases $p = new \ReflectionProperty($twig, 'templateClassPrefix'); $p->setAccessible(true); - $p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_'); + $p->setValue($twig, '__TwigTemplate_'.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', uniqid(mt_rand(), true), false).'_'); $deprecations = []; try { diff --git a/tests/Cache/FilesystemTest.php b/tests/Cache/FilesystemTest.php index 478ac355a4c..349869c26e3 100644 --- a/tests/Cache/FilesystemTest.php +++ b/tests/Cache/FilesystemTest.php @@ -23,7 +23,7 @@ class FilesystemTest extends TestCase protected function setUp(): void { - $nonce = hash('sha256', uniqid(mt_rand(), true)); + $nonce = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', uniqid(mt_rand(), true)); $this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$nonce; $this->directory = sys_get_temp_dir().'/twig-test'; $this->cache = new FilesystemCache($this->directory); diff --git a/tests/Fixtures/functions/template_from_string_error.test b/tests/Fixtures/functions/template_from_string_error.test index 900d238bd2b..1132296bc12 100644 --- a/tests/Fixtures/functions/template_from_string_error.test +++ b/tests/Fixtures/functions/template_from_string_error.test @@ -1,8 +1,10 @@ --TEST-- "template_from_string" function +--CONDITION-- +PHP_VERSION_ID >= 80100 --TEMPLATE-- {% include template_from_string("{{ not a Twig template ", "foo.twig") %} --DATA-- return [] --EXCEPTION-- -Twig\Error\SyntaxError: Unclosed "variable" in "foo.twig (string template 4900163d56b1af4b704c6b0afee7f98ba53418ce7a93d37a3af1882735baf9cd)" at line 1. +Twig\Error\SyntaxError: Unclosed "variable" in "foo.twig (string template 85e7b092afbbcd36f11981c2ef8f1569)" at line 1. diff --git a/tests/Fixtures/functions/template_from_string_error_php80.test b/tests/Fixtures/functions/template_from_string_error_php80.test new file mode 100644 index 00000000000..8ebe41f2cda --- /dev/null +++ b/tests/Fixtures/functions/template_from_string_error_php80.test @@ -0,0 +1,10 @@ +--TEST-- +"template_from_string" function +--CONDITION-- +PHP_VERSION_ID < 80100 +--TEMPLATE-- +{% include template_from_string("{{ not a Twig template ", "foo.twig") %} +--DATA-- +return [] +--EXCEPTION-- +Twig\Error\SyntaxError: Unclosed "variable" in "foo.twig (string template 4900163d56b1af4b704c6b0afee7f98ba53418ce7a93d37a3af1882735baf9cd)" at line 1.