|
18 | 18 | */ |
19 | 19 | class File |
20 | 20 | { |
| 21 | + /** |
| 22 | + * @var boolean true if OPCache enabled, and we have permission to invalidate files |
| 23 | + * @since 3.2.0 |
| 24 | + */ |
| 25 | + protected static $canFlushFileCache; |
| 26 | + |
21 | 27 | /** |
22 | 28 | * Gets the extension of a file name |
23 | 29 | * |
@@ -159,17 +165,25 @@ public static function delete($file) |
159 | 165 | $file = Path::clean($file); |
160 | 166 | $filename = basename($file); |
161 | 167 |
|
162 | | - // Try making the file writable first. If it's read-only, it can't be deleted |
163 | | - // on Windows, even if the parent folder is writable |
| 168 | + /** |
| 169 | + * Try making the file writable first. If it's read-only, it can't be deleted |
| 170 | + * on Windows, even if the parent folder is writable |
| 171 | + */ |
164 | 172 | @chmod($file, 0777); |
165 | 173 |
|
166 | | - // In case of restricted permissions we zap it one way or the other |
167 | | - // as long as the owner is either the webserver or the ftp |
| 174 | + /** |
| 175 | + * Invalidate the OPCache for the file before actually deleting it |
| 176 | + * @link https://www.php.net/manual/en/function.opcache-invalidate.php#116372 |
| 177 | + */ |
| 178 | + self::invalidateFileCache($file); |
| 179 | + |
| 180 | + /** |
| 181 | + * In case of restricted permissions we zap it one way or the other |
| 182 | + * as long as the owner is either the webserver or the ftp |
| 183 | + */ |
168 | 184 | if (!@ unlink($file)) { |
169 | 185 | throw new FilesystemException(__METHOD__ . ': Failed deleting ' . $filename); |
170 | 186 | } |
171 | | - |
172 | | - self::invalidateFileCache($file); |
173 | 187 | } |
174 | 188 |
|
175 | 189 | return true; |
@@ -200,6 +214,8 @@ public static function move($src, $dest, $path = '', $useStreams = false) |
200 | 214 | return 'Cannot find source file.'; |
201 | 215 | } |
202 | 216 |
|
| 217 | + self::invalidateFileCache($src); |
| 218 | + |
203 | 219 | if ($useStreams) { |
204 | 220 | $stream = Stream::getStream(); |
205 | 221 |
|
@@ -294,6 +310,8 @@ public static function upload($src, $dest, $useStreams = false) |
294 | 310 | Folder::create($baseDir); |
295 | 311 | } |
296 | 312 |
|
| 313 | + self::invalidateFileCache($src); |
| 314 | + |
297 | 315 | if ($useStreams) { |
298 | 316 | $stream = Stream::getStream(); |
299 | 317 |
|
@@ -321,21 +339,37 @@ public static function upload($src, $dest, $useStreams = false) |
321 | 339 | } |
322 | 340 |
|
323 | 341 | /** |
324 | | - * Invalidate any opcache for a newly written file immediately, if opcache* functions exist and if this was a PHP file. |
| 342 | + * Invalidate any opcache immediately for a file if opcache* functions are enabled and the file is a PHP file. |
325 | 343 | * |
326 | 344 | * @param string $file The path to the file just written to, to flush from opcache |
327 | 345 | * |
328 | 346 | * @return void |
329 | 347 | */ |
330 | 348 | public static function invalidateFileCache($file) |
331 | 349 | { |
332 | | - if (function_exists('opcache_invalidate')) { |
333 | | - $info = pathinfo($file); |
| 350 | + /** |
| 351 | + * Check if we can invalidate the opcache. This is the case if |
| 352 | + * - opcache is enabled, and |
| 353 | + * - the opcache_invalidate function is available, and |
| 354 | + * - calling opcache_invalidate is not restricted by opcache.restrict_api |
| 355 | + * or it is restricted to allow the currently executing script |
| 356 | + */ |
| 357 | + if (!isset(static::$canFlushFileCache)) { |
| 358 | + static::$canFlushFileCache |
| 359 | + = ini_get('opcache.enable') |
| 360 | + && \function_exists('opcache_invalidate') |
| 361 | + && (!ini_get('opcache.restrict_api') || str_starts_with(realpath($_SERVER['SCRIPT_FILENAME']), ini_get('opcache.restrict_api'))); |
| 362 | + } |
334 | 363 |
|
335 | | - if (isset($info['extension']) && $info['extension'] === 'php') { |
336 | | - // Force invalidation to be absolutely sure the opcache is cleared for this file. |
337 | | - opcache_invalidate($file, true); |
338 | | - } |
| 364 | + if (!static::$canFlushFileCache) { |
| 365 | + return; |
| 366 | + } |
| 367 | + |
| 368 | + $info = pathinfo($file); |
| 369 | + |
| 370 | + if (isset($info['extension']) && $info['extension'] === 'php') { |
| 371 | + // Force invalidation to be absolutely sure the opcache is cleared for this file. |
| 372 | + opcache_invalidate($file, true); |
339 | 373 | } |
340 | 374 | } |
341 | 375 | } |
0 commit comments