Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of Twig. |
| 5 | * |
| 6 | * (c) Fabien Potencier |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Twig\Profiler\NodeVisitor; |
| 13 | |
| 14 | use Twig\Environment; |
| 15 | use Twig\Node\BlockNode; |
| 16 | use Twig\Node\BodyNode; |
| 17 | use Twig\Node\MacroNode; |
| 18 | use Twig\Node\ModuleNode; |
| 19 | use Twig\Node\Node; |
| 20 | use Twig\NodeVisitor\NodeVisitorInterface; |
| 21 | use Twig\Profiler\Node\EnterProfileNode; |
| 22 | use Twig\Profiler\Node\LeaveProfileNode; |
| 23 | use Twig\Profiler\Profile; |
| 24 | |
| 25 | /** |
| 26 | * @author Fabien Potencier <fabien@symfony.com> |
| 27 | */ |
| 28 | final class ProfilerNodeVisitor implements NodeVisitorInterface |
| 29 | { |
| 30 | private $extensionName; |
| 31 | |
| 32 | public function __construct(string $extensionName) |
| 33 | { |
| 34 | $this->extensionName = $extensionName; |
| 35 | } |
| 36 | |
| 37 | public function enterNode(Node $node, Environment $env): Node |
| 38 | { |
| 39 | return $node; |
| 40 | } |
| 41 | |
| 42 | public function leaveNode(Node $node, Environment $env): ?Node |
| 43 | { |
| 44 | if ($node instanceof ModuleNode) { |
| 45 | $varName = $this->getVarName(); |
| 46 | $node->setNode('display_start', new Node([new EnterProfileNode($this->extensionName, Profile::TEMPLATE, $node->getTemplateName(), $varName), $node->getNode('display_start')])); |
| 47 | $node->setNode('display_end', new Node([new LeaveProfileNode($varName), $node->getNode('display_end')])); |
| 48 | } elseif ($node instanceof BlockNode) { |
| 49 | $varName = $this->getVarName(); |
| 50 | $node->setNode('body', new BodyNode([ |
| 51 | new EnterProfileNode($this->extensionName, Profile::BLOCK, $node->getAttribute('name'), $varName), |
| 52 | $node->getNode('body'), |
| 53 | new LeaveProfileNode($varName), |
| 54 | ])); |
| 55 | } elseif ($node instanceof MacroNode) { |
| 56 | $varName = $this->getVarName(); |
| 57 | $node->setNode('body', new BodyNode([ |
| 58 | new EnterProfileNode($this->extensionName, Profile::MACRO, $node->getAttribute('name'), $varName), |
| 59 | $node->getNode('body'), |
| 60 | new LeaveProfileNode($varName), |
| 61 | ])); |
| 62 | } |
| 63 | |
| 64 | return $node; |
| 65 | } |
| 66 | |
| 67 | private function getVarName(): string |
| 68 | { |
| 69 | return sprintf('__internal_%s', hash('sha256', $this->extensionName)); |
| 70 | } |
| 71 | |
| 72 | public function getPriority(): int |
| 73 | { |
| 74 | return 0; |
| 75 | } |
| 76 | } |