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\CacheContent; |
|
|
|
|
11
|
|
|
use Yiisoft\View\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
|
|
|
final class FragmentCache extends Widget |
20
|
|
|
{ |
21
|
|
|
private ?string $id = null; |
22
|
|
|
private CacheInterface $cache; |
23
|
|
|
private ?Dependency $dependency = null; |
24
|
|
|
private int $ttl = 60; |
25
|
|
|
private array $variations = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array<string, DynamicContent> |
29
|
|
|
*/ |
30
|
|
|
private array $dynamicContents = []; |
31
|
|
|
|
32
|
5 |
|
public function __construct(CacheInterface $cache) |
33
|
|
|
{ |
34
|
5 |
|
$this->cache = $cache; |
35
|
5 |
|
} |
36
|
5 |
|
|
37
|
|
|
/** |
38
|
|
|
* Initializes the FragmentCache object. |
39
|
|
|
*/ |
40
|
|
|
public function begin(): ?string |
41
|
5 |
|
{ |
42
|
|
|
if ($this->id === null) { |
43
|
5 |
|
throw new RuntimeException('You must assign the "id" using the "id()" setter.'); |
44
|
5 |
|
} |
45
|
5 |
|
|
46
|
5 |
|
parent::begin(); |
47
|
5 |
|
ob_start(); |
48
|
|
|
PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0); |
49
|
5 |
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Marks the end of content to be cached. |
54
|
|
|
* |
55
|
|
|
* Content displayed before this method call and after {@see init()} will be captured and saved in cache. |
56
|
|
|
* |
57
|
|
|
* This method does nothing if valid content is already found in cache. |
58
|
|
|
* |
59
|
|
|
* @return string The result of widget execution to be outputted. |
60
|
|
|
*/ |
61
|
5 |
|
protected function run(): string |
62
|
|
|
{ |
63
|
5 |
|
$cacheContent = new CacheContent($this->id, $this->cache, $this->dynamicContents, $this->variations); |
64
|
|
|
$content = $cacheContent->cachedContent(); |
65
|
5 |
|
|
66
|
4 |
|
if ($content !== null) { |
67
|
|
|
ob_end_clean(); |
68
|
|
|
return $content; |
69
|
5 |
|
} |
70
|
|
|
|
71
|
5 |
|
$content = ob_get_clean(); |
72
|
|
|
|
73
|
5 |
|
if ($content === false || $content === '') { |
74
|
|
|
return ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
return $cacheContent->cache($content, $this->ttl, $this->dependency); |
78
|
|
|
} |
79
|
5 |
|
|
80
|
|
|
/** |
81
|
5 |
|
* @param string $value The unique identifier of the cache fragment. |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
|
|
public function id(string $value): self |
86
|
|
|
{ |
87
|
|
|
$this->id = $value; |
88
|
|
|
return $this; |
89
|
5 |
|
} |
90
|
|
|
|
91
|
5 |
|
/** |
92
|
5 |
|
* @param Dependency $value The dependency that the cached content depends on. |
93
|
|
|
* |
94
|
5 |
|
* This can be either a {@see Dependency} object or a configuration array for creating the dependency object. |
95
|
5 |
|
* |
96
|
5 |
|
* Would make the output cache depends on the last modified time of all posts. If any post has its modification time |
97
|
|
|
* changed, the cached content would be invalidated. |
98
|
|
|
* |
99
|
4 |
|
* @return self |
100
|
|
|
*/ |
101
|
4 |
|
public function dependency(Dependency $value): self |
102
|
|
|
{ |
103
|
4 |
|
$this->dependency = $value; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int $value The number of seconds that the data can remain valid in cache. |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
|
|
public function ttl(int $value): self |
113
|
5 |
|
{ |
114
|
|
|
$this->ttl = $value; |
115
|
5 |
|
return $this; |
116
|
|
|
} |
117
|
5 |
|
|
118
|
|
|
/** |
119
|
5 |
|
* @param DynamicContent ...$value The dynamic content instances. |
120
|
|
|
* |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
|
|
public function dynamicContents(DynamicContent ...$value): self |
124
|
|
|
{ |
125
|
|
|
foreach ($value as $dynamicContent) { |
126
|
|
|
$this->dynamicContents[$dynamicContent->id()] = $dynamicContent; |
127
|
|
|
} |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string ...$value The factors that would cause the variation of the content being cached. |
133
|
|
|
* |
134
|
|
|
* Each factor is a string representing a variation (e.g. the language, a GET parameter). The following variation |
135
|
|
|
* setting will cause the content to be cached in different versions according to the current application language: |
136
|
|
|
* |
137
|
|
|
* ```php |
138
|
|
|
* $fragmentCache->variations('en'); |
139
|
|
|
* ``` |
140
|
|
|
* |
141
|
|
|
* @return self |
142
|
|
|
*/ |
143
|
|
|
public function variations(string ...$value): self |
144
|
|
|
{ |
145
|
|
|
$this->variations = $value; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths