Skip to content

Commit ceb814d

Browse files
committed
Latte: runtime helpers moved to Runtime class
1 parent a9b3ffe commit ceb814d

File tree

7 files changed

+165
-138
lines changed

7 files changed

+165
-138
lines changed

src/Bridges/CacheLatte/CacheExtension.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getPasses(): array
5353
return [
5454
'cacheInitialization' => function (TemplateNode $node): void {
5555
if ($this->used) {
56-
$node->head->append(new AuxiliaryNode(fn() => Nodes\CacheNode::class . '::initRuntime($this);'));
56+
$node->head->append(new AuxiliaryNode(fn() => '$this->global->cache->initialize($this);'));
5757
}
5858
},
5959
];
@@ -63,7 +63,13 @@ public function getPasses(): array
6363
public function getProviders(): array
6464
{
6565
return [
66-
'cacheStorage' => $this->storage,
66+
'cache' => new Runtime($this->storage),
6767
];
6868
}
69+
70+
71+
public function getCacheKey(Latte\Engine $engine): array
72+
{
73+
return ['version' => 2];
74+
}
6975
}

src/Bridges/CacheLatte/Nodes/CacheNode.php

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99

1010
namespace Nette\Bridges\CacheLatte\Nodes;
1111

12-
use Latte;
1312
use Latte\Compiler\Nodes\AreaNode;
1413
use Latte\Compiler\Nodes\Php\Expression\ArrayNode;
1514
use Latte\Compiler\Nodes\StatementNode;
1615
use Latte\Compiler\Position;
1716
use Latte\Compiler\PrintContext;
1817
use Latte\Compiler\Tag;
19-
use Nette;
20-
use Nette\Caching\Cache;
2118

2219

2320
/**
@@ -45,17 +42,18 @@ public function print(PrintContext $context): string
4542
{
4643
return $context->format(
4744
<<<'XX'
48-
if (Nette\Bridges\CacheLatte\Nodes\CacheNode::createCache($this->global->cacheStorage, %dump, $this->global->cacheStack, %node?)) %line
45+
if ($this->global->cache->createCache(%dump, %node?)) %line
4946
try {
5047
%node
51-
Nette\Bridges\CacheLatte\Nodes\CacheNode::endCache($this->global->cacheStack) %line;
48+
$this->global->cache->end() %line;
5249
} catch (\Throwable $ʟ_e) {
53-
Nette\Bridges\CacheLatte\Nodes\CacheNode::rollback($this->global->cacheStack); throw $ʟ_e;
50+
$this->global->cache->rollback();
51+
throw $ʟ_e;
5452
}
5553

5654

5755
XX,
58-
Nette\Utils\Random::generate(),
56+
base64_encode(random_bytes(10)),
5957
$this->args,
6058
$this->position,
6159
$this->content,
@@ -69,81 +67,4 @@ public function &getIterator(): \Generator
6967
yield $this->args;
7068
yield $this->content;
7169
}
72-
73-
74-
/********************* run-time helpers ****************d*g**/
75-
76-
77-
public static function initRuntime(Latte\Runtime\Template $template): void
78-
{
79-
if (!empty($template->global->cacheStack)) {
80-
$file = (new \ReflectionClass($template))->getFileName();
81-
if (@is_file($file)) { // @ - may trigger error
82-
end($template->global->cacheStack)->dependencies[Cache::Files][] = $file;
83-
}
84-
}
85-
}
86-
87-
88-
/**
89-
* Starts the output cache. Returns Nette\Caching\OutputHelper object if buffering was started.
90-
* @return Nette\Caching\OutputHelper|\stdClass
91-
*/
92-
public static function createCache(
93-
Nette\Caching\Storage $cacheStorage,
94-
string $key,
95-
?array &$parents,
96-
?array $args = null,
97-
) {
98-
if ($args) {
99-
if (array_key_exists('if', $args) && !$args['if']) {
100-
return $parents[] = new \stdClass;
101-
}
102-
103-
$key = array_merge([$key], array_intersect_key($args, range(0, count($args))));
104-
}
105-
106-
if ($parents) {
107-
end($parents)->dependencies[Cache::Items][] = $key;
108-
}
109-
110-
$cache = new Cache($cacheStorage, 'Nette.Templating.Cache');
111-
if ($helper = $cache->capture($key)) {
112-
$parents[] = $helper;
113-
114-
if (isset($args['dependencies'])) {
115-
$args += $args['dependencies']();
116-
}
117-
118-
$helper->dependencies[Cache::Tags] = $args['tags'] ?? null;
119-
$helper->dependencies[Cache::Expire] = $args['expiration'] ?? $args['expire'] ?? '+ 7 days';
120-
}
121-
122-
return $helper;
123-
}
124-
125-
126-
/**
127-
* Ends the output cache.
128-
* @param Nette\Caching\OutputHelper[] $parents
129-
*/
130-
public static function endCache(array &$parents): void
131-
{
132-
$helper = array_pop($parents);
133-
if ($helper instanceof Nette\Caching\OutputHelper) {
134-
$helper->end();
135-
}
136-
}
137-
138-
139-
/**
140-
* @param Nette\Caching\OutputHelper[] $parents
141-
*/
142-
public static function rollback(array &$parents): void
143-
{
144-
$helper = array_pop($parents);
145-
if ($helper instanceof Nette\Caching\OutputHelper) {
146-
$helper->rollback();
147-
}
148-
}
14970
}

src/Bridges/CacheLatte/Runtime.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Latte (https://latte.nette.org)
5+
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Bridges\CacheLatte;
11+
12+
use Latte;
13+
use Nette;
14+
use Nette\Caching\Cache;
15+
use Nette\Caching\OutputHelper;
16+
17+
18+
/**
19+
* Runtime helpers for Latte v3.
20+
* @internal
21+
*/
22+
class Runtime
23+
{
24+
/** @var array<int, OutputHelper|\stdClass> */
25+
private array $stack = [];
26+
27+
28+
public function __construct(
29+
private Nette\Caching\Storage $storage,
30+
) {
31+
}
32+
33+
34+
public function initialize(Latte\Runtime\Template $template): void
35+
{
36+
if ($this->stack) {
37+
$file = (new \ReflectionClass($template))->getFileName();
38+
if (@is_file($file)) { // @ - may trigger error
39+
end($this->stack)->dependencies[Cache::Files][] = $file;
40+
}
41+
}
42+
}
43+
44+
45+
/**
46+
* Starts the output cache. Returns true if buffering was started.
47+
*/
48+
public function createCache(string $key, ?array $args = null): bool
49+
{
50+
if ($args) {
51+
if (array_key_exists('if', $args) && !$args['if']) {
52+
$this->stack[] = new \stdClass;
53+
return true;
54+
}
55+
56+
$key = array_merge([$key], array_intersect_key($args, range(0, count($args))));
57+
}
58+
59+
if ($this->stack) {
60+
end($this->stack)->dependencies[Cache::Items][] = $key;
61+
}
62+
63+
$cache = new Cache($this->storage, 'Nette.Templating.Cache');
64+
if ($helper = $cache->capture($key)) {
65+
$this->stack[] = $helper;
66+
67+
if (isset($args['dependencies'])) {
68+
$args += $args['dependencies']();
69+
}
70+
71+
$helper->dependencies[Cache::Tags] = $args['tags'] ?? null;
72+
$helper->dependencies[Cache::Expire] = $args['expiration'] ?? $args['expire'] ?? '+ 7 days';
73+
}
74+
75+
return (bool) $helper;
76+
}
77+
78+
79+
/**
80+
* Ends the output cache.
81+
*/
82+
public function end(): void
83+
{
84+
$helper = array_pop($this->stack);
85+
if ($helper instanceof OutputHelper) {
86+
$helper->end();
87+
}
88+
}
89+
90+
91+
public function rollback(): void
92+
{
93+
$helper = array_pop($this->stack);
94+
if ($helper instanceof OutputHelper) {
95+
$helper->rollback();
96+
}
97+
}
98+
}

tests/Bridges.Latte3/CacheNode.phpt

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/Bridges.Latte3/Runtime.phpt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Bridges\CacheLatte\Runtime;
6+
use Nette\Caching\Cache;
7+
use Nette\Caching\Storages\DevNullStorage;
8+
use Tester\Assert;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
if (version_compare(Latte\Engine::VERSION, '3', '<')) {
13+
Tester\Environment::skip('Test for Latte 3');
14+
}
15+
16+
17+
test('', function () {
18+
$runtime = new Runtime(new DevNullStorage);
19+
$dp = [Cache::Tags => ['rum', 'cola']];
20+
Assert::true($runtime->createCache('test', $dp));
21+
$stack = Assert::with($runtime, fn() => $this->stack);
22+
$runtime->end();
23+
Assert::same($dp + [Cache::Expire => '+ 7 days'], $stack[0]->dependencies);
24+
});
25+
26+
test('', function () {
27+
$runtime = new Runtime(new DevNullStorage);
28+
$dp = [Cache::Tags => ['rum', 'cola']];
29+
$dpFallback = fn() => $dp;
30+
Assert::true($runtime->createCache('test', ['dependencies' => $dpFallback]));
31+
$stack = Assert::with($runtime, fn() => $this->stack);
32+
$runtime->end();
33+
Assert::same($dp + [Cache::Expire => '+ 7 days'], $stack[0]->dependencies);
34+
});
35+
36+
test('', function () {
37+
$runtime = new Runtime(new DevNullStorage);
38+
$dp = [
39+
Cache::Tags => ['rum', 'cola'],
40+
Cache::Expire => '+ 1 days',
41+
];
42+
$dpFallback = fn() => $dp;
43+
Assert::true($runtime->createCache('test', ['dependencies' => $dpFallback]));
44+
$stack = Assert::with($runtime, fn() => $this->stack);
45+
$runtime->end();
46+
Assert::same($dp, $stack[0]->dependencies);
47+
});
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22
%A%
3-
if (Nette\Bridges\CacheLatte\Nodes\CacheNode::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack)) /* line %d% */
3+
if ($this->global->cache->createCache('%a%')) /* line %d% */
44
try {
55
echo ' ';
66
echo LR\Filters::escapeHtmlText(($this->filters->lower)($title)) /* line %d% */;
77
echo "\n";
88

9-
Nette\Bridges\CacheLatte\Nodes\CacheNode::endCache($this->global->cacheStack) /* line %d% */;
9+
$this->global->cache->end() /* line %d% */;
1010
} catch (\Throwable $ʟ_e) {
11-
Nette\Bridges\CacheLatte\Nodes\CacheNode::rollback($this->global->cacheStack);
11+
$this->global->cache->rollback();
1212
throw $ʟ_e;
1313
}
1414
%A%

tests/Bridges.Latte3/expected/cache.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
echo 'Noncached content
44
55
';
6-
if (Nette\Bridges\CacheLatte\Nodes\CacheNode::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack, [$id, 'tags' => 'mytag'])) /* line %d% */
6+
if ($this->global->cache->createCache('%a%', [$id, 'tags' => 'mytag'])) /* line %d% */
77
try {
88
echo '
99
<h1>';
@@ -14,9 +14,9 @@
1414
$this->createTemplate('include.cache.latte', ['localvar' => 11] + $this->params, 'include')->renderToContentType('html') /* line %d% */;
1515
echo "\n";
1616

17-
Nette\Bridges\CacheLatte\Nodes\CacheNode::endCache($this->global->cacheStack) /* line %d% */;
17+
$this->global->cache->end() /* line %d% */;
1818
} catch (\Throwable $ʟ_e) {
19-
Nette\Bridges\CacheLatte\Nodes\CacheNode::rollback($this->global->cacheStack);
19+
$this->global->cache->rollback();
2020
throw $ʟ_e;
2121
}
2222
}
@@ -25,5 +25,5 @@
2525
public function prepare(): array
2626
{
2727
%A%
28-
Nette\Bridges\CacheLatte\Nodes\CacheNode::initRuntime($this);
28+
$this->global->cache->initialize($this);
2929
%A%

0 commit comments

Comments
 (0)