Skip to content

[3.x] Port improvements for opcache invalidation from CMS - redo PR #38 #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 3.x-dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
class File
{
/**
* @var boolean true if OPCache enabled, and we have permission to invalidate files
* @since 3.2.0
*/
protected static $canFlushFileCache;

/**
* Gets the extension of a file name
*
Expand Down Expand Up @@ -159,17 +165,25 @@ public static function delete($file)
$file = Path::clean($file);
$filename = basename($file);

// Try making the file writable first. If it's read-only, it can't be deleted
// on Windows, even if the parent folder is writable
/**
* Try making the file writable first. If it's read-only, it can't be deleted
* on Windows, even if the parent folder is writable
*/
@chmod($file, 0777);

// In case of restricted permissions we zap it one way or the other
// as long as the owner is either the webserver or the ftp
/**
* Invalidate the OPCache for the file before actually deleting it
* @link https://www.php.net/manual/en/function.opcache-invalidate.php#116372
*/
self::invalidateFileCache($file);

/**
* In case of restricted permissions we zap it one way or the other
* as long as the owner is either the webserver or the ftp
*/
if (!@ unlink($file)) {
throw new FilesystemException(__METHOD__ . ': Failed deleting ' . $filename);
}

self::invalidateFileCache($file);
}

return true;
Expand Down Expand Up @@ -200,6 +214,8 @@ public static function move($src, $dest, $path = '', $useStreams = false)
return 'Cannot find source file.';
}

self::invalidateFileCache($src);

if ($useStreams) {
$stream = Stream::getStream();

Expand Down Expand Up @@ -294,6 +310,8 @@ public static function upload($src, $dest, $useStreams = false)
Folder::create($baseDir);
}

self::invalidateFileCache($src);

if ($useStreams) {
$stream = Stream::getStream();

Expand Down Expand Up @@ -321,21 +339,37 @@ public static function upload($src, $dest, $useStreams = false)
}

/**
* Invalidate any opcache for a newly written file immediately, if opcache* functions exist and if this was a PHP file.
* Invalidate any opcache immediately for a file if opcache* functions are enabled and the file is a PHP file.
*
* @param string $file The path to the file just written to, to flush from opcache
*
* @return void
*/
public static function invalidateFileCache($file)
{
if (function_exists('opcache_invalidate')) {
$info = pathinfo($file);
/**
* Check if we can invalidate the opcache. This is the case if
* - opcache is enabled, and
* - the opcache_invalidate function is available, and
* - calling opcache_invalidate is not restricted by opcache.restrict_api
* or it is restricted to allow the currently executing script
*/
if (!isset(static::$canFlushFileCache)) {
static::$canFlushFileCache
= ini_get('opcache.enable')
&& \function_exists('opcache_invalidate')
&& (!ini_get('opcache.restrict_api') || str_starts_with(realpath($_SERVER['SCRIPT_FILENAME']), ini_get('opcache.restrict_api')));
}

if (isset($info['extension']) && $info['extension'] === 'php') {
// Force invalidation to be absolutely sure the opcache is cleared for this file.
opcache_invalidate($file, true);
}
if (!static::$canFlushFileCache) {
return;
}

$info = pathinfo($file);

if (isset($info['extension']) && $info['extension'] === 'php') {
// Force invalidation to be absolutely sure the opcache is cleared for this file.
opcache_invalidate($file, true);
}
}
}