@@ -91,6 +91,8 @@ public static function copy($src, $dest, $path = null, $useStreams = false)
9191throw new FilesystemException (sprintf ('%1$s(%2$s, %3$s): %4$s ' , __METHOD__ , $ src , $ dest , $ stream ->getError ()));
9292}
9393
94+ self ::invalidateOpcache ($ dest );
95+
9496return true ;
9597}
9698
@@ -99,6 +101,8 @@ public static function copy($src, $dest, $path = null, $useStreams = false)
99101throw new FilesystemException (__METHOD__ . ': Copy failed. ' );
100102}
101103
104+ self ::invalidateOpcache ($ dest );
105+
102106return true ;
103107}
104108
@@ -136,6 +140,8 @@ public static function delete($file)
136140{
137141throw new FilesystemException (__METHOD__ . ': Failed deleting ' . $ filename );
138142}
143+
144+ self ::invalidateOpcache ($ file );
139145}
140146
141147return true ;
@@ -177,6 +183,8 @@ public static function move($src, $dest, $path = '', $useStreams = false)
177183throw new FilesystemException (__METHOD__ . ': ' . $ stream ->getError ());
178184}
179185
186+ self ::invalidateOpcache ($ dest );
187+
180188return true ;
181189}
182190
@@ -185,6 +193,8 @@ public static function move($src, $dest, $path = '', $useStreams = false)
185193throw new FilesystemException (__METHOD__ . ': Rename failed. ' );
186194}
187195
196+ self ::invalidateOpcache ($ dest );
197+
188198return true ;
189199}
190200
@@ -218,6 +228,8 @@ public static function write($file, &$buffer, $useStreams = false, $appendToFile
218228$ stream ->set ('chunksize ' , (1024 * 1024 ));
219229$ stream ->writeFile ($ file , $ buffer , $ appendToFile );
220230
231+ self ::invalidateOpcache ($ file );
232+
221233return true ;
222234}
223235
@@ -226,10 +238,16 @@ public static function write($file, &$buffer, $useStreams = false, $appendToFile
226238// Set the required flag to only append to the file and not overwrite it
227239if ($ appendToFile === true )
228240{
229- return \is_int (file_put_contents ($ file , $ buffer , \FILE_APPEND ));
241+ $ res = \is_int (file_put_contents ($ file , $ buffer , \FILE_APPEND ));
230242}
243+ else
244+ {
245+ $ res = \is_int (file_put_contents ($ file , $ buffer ));
246+ }
247+
248+ self ::invalidateOpcache ($ file );
231249
232- return \is_int ( file_put_contents ( $ file , $ buffer )) ;
250+ return $ res ;
233251}
234252
235253/**
@@ -266,6 +284,8 @@ public static function upload($src, $dest, $useStreams = false)
266284throw new FilesystemException (sprintf ('%1$s(%2$s, %3$s): %4$s ' , __METHOD__ , $ src , $ dest , $ stream ->getError ()));
267285}
268286
287+ self ::invalidateOpcache ($ dest );
288+
269289return true ;
270290}
271291
@@ -274,6 +294,8 @@ public static function upload($src, $dest, $useStreams = false)
274294// Short circuit to prevent file permission errors
275295if (Path::setPermissions ($ dest ))
276296{
297+ self ::invalidateOpcache ($ dest );
298+
277299return true ;
278300}
279301
@@ -282,4 +304,25 @@ public static function upload($src, $dest, $useStreams = false)
282304
283305throw new FilesystemException (__METHOD__ . ': Failed to move file. ' );
284306}
307+
308+ /**
309+ * Invalidate any opcache for a newly written file immediately, if opcache* functions exist and if this was a PHP file.
310+ *
311+ * @param string $file The path to the file just written to, to flush from opcache
312+ *
313+ * @return void
314+ */
315+ public static function invalidateOpcache ($ file )
316+ {
317+ if (function_exists ('opcache_invalidate ' ))
318+ {
319+ $ info = pathinfo ($ file );
320+
321+ if ($ info ['extension ' ] === 'php ' )
322+ {
323+ // Force invalidation to be absolutely sure the opcache is cleared for this file.
324+ opcache_invalidate ($ file , true );
325+ }
326+ }
327+ }
285328}
0 commit comments