Skip to content

Commit a37d340

Browse files
authored
Merge pull request #37 from PhilETaylor/opcache
Propose Opcache invalidation on file operations
2 parents c60a0f0 + aa36b25 commit a37d340

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/File.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public static function copy($src, $dest, $path = null, $useStreams = false)
9191
throw new FilesystemException(sprintf('%1$s(%2$s, %3$s): %4$s', __METHOD__, $src, $dest, $stream->getError()));
9292
}
9393

94+
self::invalidateFileCache($dest);
95+
9496
return true;
9597
}
9698

@@ -99,6 +101,8 @@ public static function copy($src, $dest, $path = null, $useStreams = false)
99101
throw new FilesystemException(__METHOD__ . ': Copy failed.');
100102
}
101103

104+
self::invalidateFileCache($dest);
105+
102106
return true;
103107
}
104108

@@ -136,6 +140,8 @@ public static function delete($file)
136140
{
137141
throw new FilesystemException(__METHOD__ . ': Failed deleting ' . $filename);
138142
}
143+
144+
self::invalidateFileCache($file);
139145
}
140146

141147
return true;
@@ -177,6 +183,8 @@ public static function move($src, $dest, $path = '', $useStreams = false)
177183
throw new FilesystemException(__METHOD__ . ': ' . $stream->getError());
178184
}
179185

186+
self::invalidateFileCache($dest);
187+
180188
return true;
181189
}
182190

@@ -185,6 +193,8 @@ public static function move($src, $dest, $path = '', $useStreams = false)
185193
throw new FilesystemException(__METHOD__ . ': Rename failed.');
186194
}
187195

196+
self::invalidateFileCache($dest);
197+
188198
return 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::invalidateFileCache($file);
232+
221233
return 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
227239
if ($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::invalidateFileCache($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)
266284
throw new FilesystemException(sprintf('%1$s(%2$s, %3$s): %4$s', __METHOD__, $src, $dest, $stream->getError()));
267285
}
268286

287+
self::invalidateFileCache($dest);
288+
269289
return true;
270290
}
271291

@@ -274,6 +294,8 @@ public static function upload($src, $dest, $useStreams = false)
274294
// Short circuit to prevent file permission errors
275295
if (Path::setPermissions($dest))
276296
{
297+
self::invalidateFileCache($dest);
298+
277299
return true;
278300
}
279301

@@ -282,4 +304,25 @@ public static function upload($src, $dest, $useStreams = false)
282304

283305
throw 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 invalidateFileCache($file)
316+
{
317+
if (function_exists('opcache_invalidate'))
318+
{
319+
$info = pathinfo($file);
320+
321+
if (isset($info['extension']) && $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

Comments
 (0)