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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

@GromNaN GromNaN Nov 23, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class name is hashed twice at runtime, for every template load. Once in FilesystemCache::generateKey and the second here.

}

/**
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Profiler/NodeVisitor/ProfilerNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Test/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/Cache/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tests/Fixtures/functions/template_from_string_error.test
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions tests/Fixtures/functions/template_from_string_error_php80.test
Original file line number Diff line number Diff line change
@@ -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.