|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Widgets; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Yiisoft\Cache\CacheInterface; |
|
9
|
|
|
use Yiisoft\Cache\Dependency\Dependency; |
|
10
|
|
|
use Yiisoft\View\Cache\CachedContent; |
|
11
|
|
|
use Yiisoft\View\Cache\DynamicContent; |
|
12
|
|
|
use Yiisoft\Widget\Widget; |
|
13
|
|
|
|
|
14
|
|
|
use function ob_end_clean; |
|
15
|
|
|
use function ob_get_clean; |
|
16
|
|
|
use function ob_implicit_flush; |
|
17
|
|
|
use function ob_start; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* FragmentCache caches a fragment of content. |
|
21
|
|
|
* |
|
22
|
|
|
* @see CachedContent |
|
23
|
|
|
* @see DynamicContent |
|
24
|
|
|
* |
|
25
|
|
|
* Example of use: |
|
26
|
|
|
* |
|
27
|
|
|
* ```php |
|
28
|
|
|
* $dynamicContent = new Yiisoft\View\Cache\DynamicContent('dynamic-id', static function (array $parameters): string { |
|
29
|
|
|
* return strtoupper("{$parameters['a']} - {$parameters['b']}"); |
|
30
|
|
|
* }, ['a' => 'string-a', 'b' => 'string-b']); |
|
31
|
|
|
* |
|
32
|
|
|
* FragmentCache::widget()->id('cache-id')->ttl(30)->dynamicContents($dynamicContent)->begin(); |
|
33
|
|
|
* echo 'Content to be cached ...'; |
|
34
|
|
|
* echo $dynamicContent->placeholder(); |
|
35
|
|
|
* echo 'Content to be cached ...'; |
|
36
|
|
|
* FragmentCache::end(); |
|
37
|
|
|
* ``` |
|
38
|
|
|
*/ |
|
39
|
|
|
final class FragmentCache extends Widget |
|
40
|
|
|
{ |
|
41
|
|
|
private ?string $id = null; |
|
42
|
|
|
private CacheInterface $cache; |
|
43
|
|
|
private ?Dependency $dependency = null; |
|
44
|
|
|
private int $ttl = 60; |
|
45
|
|
|
private array $variations = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array<string, DynamicContent> |
|
49
|
|
|
*/ |
|
50
|
|
|
private array $dynamicContents = []; |
|
51
|
|
|
|
|
52
|
7 |
|
public function __construct(CacheInterface $cache) |
|
53
|
|
|
{ |
|
54
|
7 |
|
$this->cache = $cache; |
|
55
|
7 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns a new instance with the specified Widget ID. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $value The unique identifier of the cache fragment. |
|
61
|
|
|
* |
|
62
|
|
|
* @return self |
|
63
|
|
|
*/ |
|
64
|
6 |
|
public function id(string $value): self |
|
65
|
|
|
{ |
|
66
|
6 |
|
$new = clone $this; |
|
67
|
6 |
|
$new->id = $value; |
|
68
|
6 |
|
return $new; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns a new instance with the dependency. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Dependency $value The dependency that the cached content depends on. |
|
75
|
|
|
* |
|
76
|
|
|
* This can be either a {@see Dependency} object or a configuration array for creating the dependency object. |
|
77
|
|
|
* |
|
78
|
|
|
* Would make the output cache depends on the last modified time of all posts. If any post has its modification time |
|
79
|
|
|
* changed, the cached content would be invalidated. |
|
80
|
|
|
* |
|
81
|
|
|
* @return self |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function dependency(Dependency $value): self |
|
84
|
|
|
{ |
|
85
|
2 |
|
$new = clone $this; |
|
86
|
2 |
|
$new->dependency = $value; |
|
87
|
2 |
|
return $new; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns a new instance with the specified TTL. |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $value The number of seconds that the data can remain valid in cache. |
|
94
|
|
|
* |
|
95
|
|
|
* @return self |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function ttl(int $value): self |
|
98
|
|
|
{ |
|
99
|
2 |
|
$new = clone $this; |
|
100
|
2 |
|
$new->ttl = $value; |
|
101
|
2 |
|
return $new; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns a new instance with the specified dynamic contents. |
|
106
|
|
|
* |
|
107
|
|
|
* @param DynamicContent ...$value The dynamic content instances. |
|
108
|
|
|
* |
|
109
|
|
|
* @return self |
|
110
|
|
|
*/ |
|
111
|
4 |
|
public function dynamicContents(DynamicContent ...$value): self |
|
112
|
|
|
{ |
|
113
|
4 |
|
$new = clone $this; |
|
114
|
|
|
|
|
115
|
4 |
|
foreach ($value as $dynamicContent) { |
|
116
|
4 |
|
$new->dynamicContents[$dynamicContent->id()] = $dynamicContent; |
|
117
|
|
|
} |
|
118
|
4 |
|
return $new; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns a new instance with the specified variations. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string ...$value The factors that would cause the variation of the content being cached. |
|
125
|
|
|
* |
|
126
|
|
|
* Each factor is a string representing a variation (e.g. the language, a GET parameter). The following variation |
|
127
|
|
|
* setting will cause the content to be cached in different versions according to the current application language: |
|
128
|
|
|
* |
|
129
|
|
|
* ```php |
|
130
|
|
|
* $fragmentCache->variations('en'); |
|
131
|
|
|
* ``` |
|
132
|
|
|
* |
|
133
|
|
|
* @return self |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function variations(string ...$value): self |
|
136
|
|
|
{ |
|
137
|
2 |
|
$new = clone $this; |
|
138
|
2 |
|
$new->variations = $value; |
|
139
|
2 |
|
return $new; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Starts recording a fragment cache. |
|
144
|
|
|
*/ |
|
145
|
6 |
|
public function begin(): ?string |
|
146
|
|
|
{ |
|
147
|
6 |
|
parent::begin(); |
|
148
|
6 |
|
ob_start(); |
|
149
|
6 |
|
PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0); |
|
150
|
6 |
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Marks the end of content to be cached. |
|
155
|
|
|
* |
|
156
|
|
|
* Content displayed before this method call and after {@see begin()} will be captured and saved in cache. |
|
157
|
|
|
* |
|
158
|
|
|
* This method does nothing if valid content is already found in cache. |
|
159
|
|
|
* |
|
160
|
|
|
* @return string The result of widget execution to be outputted. |
|
161
|
|
|
*/ |
|
162
|
6 |
|
protected function run(): string |
|
163
|
|
|
{ |
|
164
|
6 |
|
if ($this->id === null) { |
|
165
|
1 |
|
ob_end_clean(); |
|
166
|
1 |
|
throw new RuntimeException('You must assign the "id" using the "id()" setter.'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
5 |
|
$cachedContent = new CachedContent($this->id, $this->cache, $this->dynamicContents, $this->variations); |
|
170
|
5 |
|
$content = $cachedContent->get(); |
|
171
|
|
|
|
|
172
|
5 |
|
if ($content !== null) { |
|
173
|
4 |
|
ob_end_clean(); |
|
174
|
4 |
|
return $content; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
5 |
|
$content = ob_get_clean(); |
|
178
|
|
|
|
|
179
|
5 |
|
if ($content === false || $content === '') { |
|
180
|
|
|
return ''; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
5 |
|
return $cachedContent->cache($content, $this->ttl, $this->dependency); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|