1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\Cache\Node;
use Twig\Compiler;
use Twig\Node\CaptureNode;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Node;
class CacheNode extends AbstractExpression
{
public function __construct(AbstractExpression $key, ?AbstractExpression $ttl, ?AbstractExpression $tags, Node $body, int $lineno)
{
$body = new CaptureNode($body, $lineno);
$body->setAttribute('raw', true);
$nodes = ['key' => $key, 'body' => $body];
if (null !== $ttl) {
$nodes['ttl'] = $ttl;
}
if (null !== $tags) {
$nodes['tags'] = $tags;
}
parent::__construct($nodes, [], $lineno);
}
public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->raw('$this->env->getRuntime(\'Twig\Extra\Cache\CacheRuntime\')->getCache()->get(')
->subcompile($this->getNode('key'))
->raw(", function (\Symfony\Contracts\Cache\ItemInterface \$item) use (\$context, \$macros, \$blocks) {\n")
->indent()
;
if ($this->hasNode('tags')) {
$compiler
->write('$item->tag(')
->subcompile($this->getNode('tags'))
->raw(");\n")
;
}
if ($this->hasNode('ttl')) {
$compiler
->write('$item->expiresAfter(')
->subcompile($this->getNode('ttl'))
->raw(");\n")
;
}
$compiler
->write('return ')
->subcompile($this->getNode('body'))
->raw(";\n")
->outdent()
->write("})\n")
;
}
}
|