-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
384 additions
and
171 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/lib/Drupal/Core/Template/RemoveCheckToStringNodeVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Core\Template; | ||
|
||
use Twig\Environment; | ||
use Twig\Node\CheckToStringNode; | ||
use Twig\Node\Node; | ||
use Twig\NodeVisitor\NodeVisitorInterface; | ||
|
||
/** | ||
* Defines a TwigNodeVisitor that replaces CheckToStringNodes. | ||
* | ||
* Twig 3.14.1 resulted in a performance regression in Drupal due to checking if | ||
* __toString is an allowed method on objects. __toString is allowed on all | ||
* objects when Drupal's default SandboxPolicy is active. Therefore, Twig's | ||
* SandboxExtension checks are unnecessary. | ||
*/ | ||
final class RemoveCheckToStringNodeVisitor implements NodeVisitorInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function enterNode(Node $node, Environment $env): Node { | ||
if ($node instanceof CheckToStringNode) { | ||
// Replace CheckToStringNode with the faster equivalent, __toString is an | ||
// allowed method so any checking of __toString on a per-object basis is | ||
// performance overhead. | ||
$new = new TwigSimpleCheckToStringNode($node->getNode('expr')); | ||
// @todo https://www.drupal.org/project/drupal/issues/3488584 Update for | ||
// Twig 4 as the spread attribute has been removed there. | ||
if ($node->hasAttribute('spread')) { | ||
$new->setAttribute('spread', $node->getAttribute('spread')); | ||
} | ||
return $new; | ||
} | ||
return $node; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function leaveNode(Node $node, Environment $env): ?Node { | ||
return $node; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPriority() { | ||
// Runs after sandbox visitor. | ||
return 1; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/lib/Drupal/Core/Template/TwigSimpleCheckToStringNode.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Core\Template; | ||
|
||
use Twig\Compiler; | ||
use Twig\Node\CheckToStringNode; | ||
|
||
/** | ||
* Defines a twig node for simplifying CheckToStringNode. | ||
* | ||
* Drupal's sandbox policy is very permissive with checking whether an object | ||
* can be converted to a string. We allow any object with a __toString method. | ||
* This means that the array traversal in the default SandboxExtension | ||
* implementation added by the parent class is a performance overhead we don't | ||
* need. | ||
* | ||
* @see \Drupal\Core\Template\TwigSandboxPolicy | ||
* @see \Drupal\Core\Template\RemoveCheckToStringNodeVisitor | ||
*/ | ||
final class TwigSimpleCheckToStringNode extends CheckToStringNode { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function compile(Compiler $compiler): void { | ||
$expr = $this->getNode('expr'); | ||
$compiler | ||
->subcompile($expr); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.