@@ -28,9 +28,13 @@ trait PhpFilesTrait
28
28
private $ includeHandler ;
29
29
private $ zendDetectUnicode ;
30
30
31
+ private static $ startTime ;
32
+
31
33
public static function isSupported ()
32
34
{
33
- return function_exists ('opcache_invalidate ' ) && ini_get ('opcache.enable ' );
35
+ static ::$ startTime = static ::$ startTime ?? $ _SERVER ['REQUEST_TIME ' ] ?? time ();
36
+
37
+ return \function_exists ('opcache_invalidate ' ) && ini_get ('opcache.enable ' );
34
38
}
35
39
36
40
/**
@@ -40,7 +44,7 @@ public function prune()
40
44
{
41
45
$ time = time ();
42
46
$ pruned = true ;
43
- $ allowCompile = ' cli ' !== PHP_SAPI || ini_get ('opcache.enable_cli ' );
47
+ $ allowCompile = \function_exists ( ' opcache_invalidate ' ) && ( ' cli ' !== \ PHP_SAPI || ini_get ('opcache.enable_cli ' ) );
44
48
45
49
set_error_handler ($ this ->includeHandler );
46
50
try {
@@ -119,7 +123,7 @@ protected function doSave(array $values, $lifetime)
119
123
{
120
124
$ ok = true ;
121
125
$ data = array ($ lifetime ? time () + $ lifetime : PHP_INT_MAX , '' );
122
- $ allowCompile = ' cli ' !== PHP_SAPI || ini_get ('opcache.enable_cli ' );
126
+ $ allowCompile = \function_exists ( ' opcache_invalidate ' ) && ( ' cli ' !== \ PHP_SAPI || ini_get ('opcache.enable_cli ' ) );
123
127
124
128
foreach ($ values as $ key => $ value ) {
125
129
if (null === $ value || \is_object ($ value )) {
@@ -142,7 +146,8 @@ protected function doSave(array $values, $lifetime)
142
146
143
147
$ data [1 ] = $ value ;
144
148
$ file = $ this ->getFile ($ key , true );
145
- $ ok = $ this ->write ($ file , '<?php return ' .var_export ($ data , true ).'; ' ) && $ ok ;
149
+ // Since OPcache only compiles files older than the script execution start, set the file's mtime in the past
150
+ $ ok = $ this ->write ($ file , '<?php return ' .var_export ($ data , true ).'; ' , self ::$ startTime - 10 ) && $ ok ;
146
151
147
152
if ($ allowCompile ) {
148
153
@opcache_invalidate ($ file , true );
0 commit comments