Verified Commit 818ad553 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3375843 by lussoluca, e0ipso, smustgrave, DieterHolvoet: Allow other...

Issue #3375843 by lussoluca, e0ipso, smustgrave, DieterHolvoet: Allow other Twig node visitors to modify 'display_start' and 'display_end'

(cherry picked from commit a6aabb7f)
(cherry picked from commit ca19b50d)
parent 9538b30d
Loading
Loading
Loading
Loading
Loading
+18 −8
Original line number Diff line number Diff line
@@ -71,14 +71,24 @@ public function leaveNode(Node $node, Environment $env): ?Node {
      new Node([new ConstantExpression($component_id, $line)]),
      $line
    ), $line);
    foreach ($print_nodes as $index => $print_node) {
      $node->getNode('display_start')->setNode((string) $index, $print_node);
    }

    // Append the print nodes to the display_start node.
    $node->setNode(
      'display_start',
      new Node([
        $node->getNode('display_start'),
        ...$print_nodes,
      ]),
    );

    if ($env->isDebug()) {
      $node->getNode('display_end')
        ->setNode(
          '0',
          new PrintNode(new ConstantExpression(sprintf('<!-- %s Component end: %s -->', $emoji, $component_id), $line), $line)
      // Append the closing comment to the display_end node.
      $node->setNode(
        'display_end',
        new Node([
          new PrintNode(new ConstantExpression(sprintf('<!-- %s Component end: %s -->', $emoji, $component_id), $line), $line),
          $node->getNode('display_end'),
        ])
      );
    }
    // Slots can be validated at compile time, we don't need to add nodes to
+5 −0
Original line number Diff line number Diff line
name: 'Add another node visitor'
type: module
dependencies:
  - sdc:sdc
package: Testing
+5 −0
Original line number Diff line number Diff line
services:
  sdc_other_node_visitor.twig.extension.profiler:
    class: Drupal\sdc_other_node_visitor\Twig\Extension\TestProfilerExtension
    tags:
      - { name: twig.extension, priority: 100 }
+34 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\sdc_other_node_visitor\Twig\Extension;

use Drupal\sdc_other_node_visitor\Twig\NodeVisitor\TestNodeVisitor;
use Twig\Extension\AbstractExtension;

/**
 * Twig extension to add a test node visitor.
 */
class TestProfilerExtension extends AbstractExtension {

  /**
   * {@inheritdoc}
   */
  public function getNodeVisitors(): array {
    return [new TestNodeVisitor(static::class)];
  }

  /**
   * Dummy function called when a Twig template is entered.
   */
  public function enter() {
    // NOOP.
  }

  /**
   * Dummy function called when a Twig template is left.
   */
  public function leave() {
    // NOOP.
  }

}
+67 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\sdc_other_node_visitor\Twig\NodeVisitor;

use Drupal\sdc_other_node_visitor\Twig\Profiler\EnterProfileNode;
use Drupal\sdc_other_node_visitor\Twig\Profiler\LeaveProfileNode;
use Twig\Environment;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\NodeVisitor\NodeVisitorInterface;

/**
 * A node visitor that adds nodes to the Twig template.
 *
 * Most of this code is copied from
 * Twig\Profiler\NodeVisitor\ProfilerNodeVisitor.
 */
final class TestNodeVisitor implements NodeVisitorInterface {

  private string $extensionName;

  private string $varName;

  /**
   * TestNodeVisitor constructor.
   *
   * @param string $extensionName
   *   The name of the extension.
   */
  public function __construct(string $extensionName) {
    $this->extensionName = $extensionName;
    $this->varName = sprintf('__internal_%s', hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $extensionName));
  }

  /**
   * {@inheritdoc}
   */
  public function enterNode(Node $node, Environment $env): Node {
    return $node;
  }

  /**
   * {@inheritdoc}
   */
  public function leaveNode(Node $node, Environment $env): ?Node {
    if ($node instanceof ModuleNode) {
      $node->setNode('display_start', new Node([
        new EnterProfileNode($this->extensionName, $this->varName),
        $node->getNode('display_start'),
      ]));
      $node->setNode('display_end', new Node([
        new LeaveProfileNode($this->varName),
        $node->getNode('display_end'),
      ]));
    }

    return $node;
  }

  /**
   * {@inheritdoc}
   */
  public function getPriority(): int {
    return 0;
  }

}
Loading