@@ -35,12 +35,13 @@ trait PhpFilesTrait
35
35
private $ files = [];
36
36
37
37
private static $ startTime ;
38
+ private static $ valuesCache = [];
38
39
39
40
public static function isSupported ()
40
41
{
41
42
self ::$ startTime = self ::$ startTime ?? $ _SERVER ['REQUEST_TIME ' ] ?? time ();
42
43
43
- return \function_exists ('opcache_invalidate ' ) && ( ' cli ' !== \ PHP_SAPI || filter_var (ini_get ('opcache.enable_cli ' ), FILTER_VALIDATE_BOOLEAN )) && filter_var (ini_get ('opcache.enable ' ), FILTER_VALIDATE_BOOLEAN );
44
+ return \function_exists ('opcache_invalidate ' ) && filter_var (ini_get ('opcache.enable ' ), FILTER_VALIDATE_BOOLEAN ) && (! \in_array (\ PHP_SAPI , [ ' cli ' , ' phpdbg ' ], true ) || filter_var (ini_get ('opcache.enable_cli ' ), FILTER_VALIDATE_BOOLEAN ) );
44
45
}
45
46
46
47
/**
@@ -54,7 +55,7 @@ public function prune()
54
55
55
56
set_error_handler ($ this ->includeHandler );
56
57
try {
57
- foreach (new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ this ->directory , \FilesystemIterator::SKIP_DOTS ), \RecursiveIteratorIterator::LEAVES_ONLY ) as $ file ) {
58
+ foreach (new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ this ->directory , \FilesystemIterator::SKIP_DOTS | \FilesystemIterator:: CURRENT_AS_PATHNAME ), \RecursiveIteratorIterator::LEAVES_ONLY ) as $ file ) {
58
59
try {
59
60
if (\is_array ($ expiresAt = include $ file )) {
60
61
$ expiresAt = $ expiresAt [0 ];
@@ -100,7 +101,6 @@ protected function doFetch(array $ids)
100
101
} elseif (!\is_object ($ value )) {
101
102
$ values [$ id ] = $ value ;
102
103
} elseif (!$ value instanceof LazyValue) {
103
- // calling a Closure is for @deprecated BC and should be removed in Symfony 5.0
104
104
$ values [$ id ] = $ value ();
105
105
} elseif (false === $ values [$ id ] = include $ value ->file ) {
106
106
unset($ values [$ id ], $ this ->values [$ id ]);
@@ -123,14 +123,18 @@ protected function doFetch(array $ids)
123
123
try {
124
124
$ file = $ this ->files [$ id ] ?? $ this ->files [$ id ] = $ this ->getFile ($ id );
125
125
126
- if (\is_array ($ expiresAt = include $ file )) {
126
+ $ expiresAt = $ this ->appendOnly && isset (self ::$ valuesCache [$ file ]) ? self ::$ valuesCache [$ file ] : include $ file ;
127
+
128
+ if (\is_array ($ expiresAt )) {
127
129
[$ expiresAt , $ this ->values [$ id ]] = $ expiresAt ;
128
130
} elseif ($ now < $ expiresAt ) {
129
131
$ this ->values [$ id ] = new LazyValue ($ file );
130
132
}
131
133
132
134
if ($ now >= $ expiresAt ) {
133
- unset($ this ->values [$ id ], $ missingIds [$ k ]);
135
+ unset($ this ->values [$ id ], $ missingIds [$ k ], self ::$ valuesCache [$ file ]);
136
+ } elseif ($ this ->appendOnly && !isset (self ::$ valuesCache [$ file ]) && !$ this ->values [$ id ] instanceof LazyValue) {
137
+ self ::$ valuesCache [$ file ] = [$ expiresAt , $ this ->values [$ id ]];
134
138
}
135
139
} catch (\ErrorException $ e ) {
136
140
unset($ missingIds [$ k ]);
@@ -159,7 +163,9 @@ protected function doHave($id)
159
163
$ file = $ this ->files [$ id ] ?? $ this ->files [$ id ] = $ this ->getFile ($ id );
160
164
$ getExpiry = true ;
161
165
162
- if (\is_array ($ expiresAt = include $ file )) {
166
+ $ expiresAt = $ this ->appendOnly && isset (self ::$ valuesCache [$ file ]) ? self ::$ valuesCache [$ file ] : include $ file ;
167
+
168
+ if (\is_array ($ expiresAt )) {
163
169
[$ expiresAt , $ value ] = $ expiresAt ;
164
170
} elseif ($ this ->appendOnly ) {
165
171
$ value = new LazyValue ($ file );
@@ -172,6 +178,10 @@ protected function doHave($id)
172
178
if ($ this ->appendOnly ) {
173
179
$ now = 0 ;
174
180
$ this ->values [$ id ] = $ value ;
181
+
182
+ if ($ this ->appendOnly && !isset (self ::$ valuesCache [$ file ]) && !$ value instanceof LazyValue) {
183
+ self ::$ valuesCache [$ file ] = [$ expiresAt , $ value ];
184
+ }
175
185
} else {
176
186
$ now = time ();
177
187
}
@@ -211,12 +221,14 @@ protected function doSave(array $values, $lifetime)
211
221
$ value = var_export ($ value , true );
212
222
}
213
223
214
- if (!$ isStaticValue ) {
224
+ if ($ isStaticValue ) {
225
+ $ value = "<?php return [ {$ expiry }, {$ value }]; \n" ;
226
+ } elseif ($ this ->appendOnly ) {
227
+ $ value = "<?php return [ {$ expiry }, static function () { return {$ value }; }]; \n" ;
228
+ } else {
215
229
// We cannot use a closure here because of https://bugs.php.net/76982
216
230
$ value = str_replace ('\Symfony\Component\VarExporter\Internal \\' , '' , $ value );
217
231
$ value = "<?php \n\nnamespace Symfony\Component\VarExporter\Internal; \n\nreturn \$getExpiry ? {$ expiry } : {$ value }; \n" ;
218
- } else {
219
- $ value = "<?php return [ {$ expiry }, {$ value }]; \n" ;
220
232
}
221
233
222
234
$ file = $ this ->files [$ key ] = $ this ->getFile ($ key , true );
@@ -227,6 +239,7 @@ protected function doSave(array $values, $lifetime)
227
239
@opcache_invalidate ($ file , true );
228
240
@opcache_compile_file ($ file );
229
241
}
242
+ unset(self ::$ valuesCache [$ file ]);
230
243
}
231
244
232
245
if (!$ ok && !is_writable ($ this ->directory )) {
@@ -260,6 +273,8 @@ protected function doDelete(array $ids)
260
273
261
274
protected function doUnlink ($ file )
262
275
{
276
+ unset(self ::$ valuesCache [$ file ]);
277
+
263
278
if (self ::isSupported ()) {
264
279
@opcache_invalidate ($ file , true );
265
280
}
0 commit comments