mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Better Twig 3 support
This commit is contained in:
parent
243053659c
commit
beba9c029d
|
|
@ -13,6 +13,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Twig\DeferredExtension;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\Template;
|
||||
|
||||
|
|
@ -27,6 +28,11 @@ final class DeferredExtension extends AbstractExtension
|
|||
|
||||
public function getNodeVisitors() : array
|
||||
{
|
||||
if (Environment::VERSION_ID < 20000) {
|
||||
// Twig 1.x support
|
||||
return [new DeferredNodeVisitorCompat()];
|
||||
}
|
||||
|
||||
return [new DeferredNodeVisitor()];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ final class DeferredNodeVisitor implements NodeVisitorInterface
|
|||
{
|
||||
private $hasDeferred = false;
|
||||
|
||||
public function enterNode(\Twig_NodeInterface $node, Environment $env) : Node
|
||||
public function enterNode(Node $node, Environment $env) : Node
|
||||
{
|
||||
if (!$this->hasDeferred && $node instanceof DeferredBlockNode) {
|
||||
$this->hasDeferred = true;
|
||||
|
|
@ -31,7 +31,7 @@ final class DeferredNodeVisitor implements NodeVisitorInterface
|
|||
return $node;
|
||||
}
|
||||
|
||||
public function leaveNode(\Twig_NodeInterface $node, Environment $env) : ?Node
|
||||
public function leaveNode(Node $node, Environment $env) : ?Node
|
||||
{
|
||||
if ($this->hasDeferred && $node instanceof ModuleNode) {
|
||||
$node->setNode('constructor_end', new Node([new DeferredExtensionNode(), $node->getNode('constructor_end')]));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the rybakit/twig-deferred-extension package.
|
||||
*
|
||||
* (c) Eugene Leonovich <gen.work@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Twig\DeferredExtension;
|
||||
|
||||
use Twig\Environment;
|
||||
use Twig\Node\ModuleNode;
|
||||
use Twig\Node\Node;
|
||||
use Twig\NodeVisitor\NodeVisitorInterface;
|
||||
|
||||
final class DeferredNodeVisitorCompat implements NodeVisitorInterface
|
||||
{
|
||||
private $hasDeferred = false;
|
||||
|
||||
public function enterNode(\Twig_NodeInterface $node, Environment $env) : Node
|
||||
{
|
||||
if (!$this->hasDeferred && $node instanceof DeferredBlockNode) {
|
||||
$this->hasDeferred = true;
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function leaveNode(\Twig_NodeInterface $node, Environment $env) : ?Node
|
||||
{
|
||||
if ($this->hasDeferred && $node instanceof ModuleNode) {
|
||||
$node->setNode('constructor_end', new Node([new DeferredExtensionNode(), $node->getNode('constructor_end')]));
|
||||
$node->setNode('display_end', new Node([new DeferredNode(), $node->getNode('display_end')]));
|
||||
$this->hasDeferred = false;
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function getPriority() : int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user