-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathRuntime.php
More file actions
99 lines (77 loc) · 2.03 KB
/
Runtime.php
File metadata and controls
99 lines (77 loc) · 2.03 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Bridges\CacheLatte;
use Latte;
use Nette;
use Nette\Caching\Cache;
use Nette\Caching\OutputHelper;
use function array_intersect_key, array_key_exists, array_merge, array_pop, count, end, is_file, range;
/**
* Runtime helpers for Latte v3.
* @internal
*/
class Runtime
{
/** @var array<int, OutputHelper|\stdClass> */
private array $stack = [];
public function __construct(
private Nette\Caching\Storage $storage,
) {
}
public function initialize(Latte\Runtime\Template $template): void
{
if ($this->stack) {
$file = (new \ReflectionClass($template))->getFileName();
if (@is_file($file)) { // @ - may trigger error
end($this->stack)->dependencies[Cache::Files][] = $file;
}
}
}
/**
* Starts the output cache. Returns true if buffering was started.
*/
public function createCache(string $key, ?array $args = null): bool
{
if ($args) {
if (array_key_exists('if', $args) && !$args['if']) {
$this->stack[] = new \stdClass;
return true;
}
$key = array_merge([$key], array_intersect_key($args, range(0, count($args))));
}
if ($this->stack) {
end($this->stack)->dependencies[Cache::Items][] = $key;
}
$cache = new Cache($this->storage, 'Nette.Templating.Cache');
if ($helper = $cache->capture($key)) {
$this->stack[] = $helper;
if (isset($args['dependencies'])) {
$args += $args['dependencies']();
}
$helper->dependencies[Cache::Tags] = $args['tags'] ?? null;
$helper->dependencies[Cache::Expire] = $args['expiration'] ?? $args['expire'] ?? '+ 7 days';
}
return (bool) $helper;
}
/**
* Ends the output cache.
*/
public function end(): void
{
$helper = array_pop($this->stack);
if ($helper instanceof OutputHelper) {
$helper->end();
}
}
public function rollback(): void
{
$helper = array_pop($this->stack);
if ($helper instanceof OutputHelper) {
$helper->rollback();
}
}
}