Skip to content

Improve changes to invalidating opcache #38

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

Closed
wants to merge 9 commits into from
Closed
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
90 changes: 73 additions & 17 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
*/
class File
{
/**
* @var boolean true if OPCache enabled, and we have permission to invalidate files
* @since __DEPLOY_VERSION__
*/
protected static $canFlushFileCache;

/**
* Strips the last extension off of a file name
*
Expand Down Expand Up @@ -130,18 +136,27 @@ public static function delete($file)
throw new FilesystemException(__METHOD__ . ': Failed deleting inaccessible file ' . $filename);
}

// 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
* @see https://github.com/joomla/joomla-cms/pull/32915#issuecomment-812865635
* @see https://www.php.net/manual/en/function.opcache-invalidate.php#116372
*/
self::invalidateFileCache($file);

/**
* In case of restricted permissions we delete 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 @@ -174,6 +189,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 @@ -275,6 +292,8 @@ public static function upload($src, $dest, $useStreams = false)
Folder::create($baseDir);
}

self::invalidateFileCache($src);

if ($useStreams)
{
$stream = Stream::getStream();
Expand Down Expand Up @@ -306,23 +325,60 @@ 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 opcache for a newly written/deleted file immediately, if opcache* functions exist and if this was a PHP file.
*
* @param string $filepath The path to the file just written to, to flush from opcache
* @param boolean $force If set to true, the script will be invalidated regardless of whether invalidation is necessary
*
* @param string $file The path to the file just written to, to flush from opcache
* @return boolean TRUE if the opcode cache for script was invalidated/nothing to invalidate,
* or FALSE if the opcode cache is disabled or other conditions returning
* FALSE from opcache_invalidate (like file not found).
*
* @return void
* @since __DEPLOY_VERSION__
*/
public static function invalidateFileCache($file)
public static function invalidateFileCache($filepath, $force = true)
{
if (function_exists('opcache_invalidate'))
if (self::canFlushFileCache() && '.php' === strtolower(substr($filepath, -4)))
{
$info = pathinfo($file);
return opcache_invalidate($filepath, $force);
}

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

/**
* First we check if opcache is enabled
* Then we check if the opcache_invalidate function is available
* Lastly we check if the host has restricted which scripts can use opcache_invalidate using opcache.restrict_api.
*
* `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()`
* is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI.
* If the host has this set, check whether the path in `opcache.restrict_api` matches
* the beginning of the path of the origin file.
*
* @return boolean TRUE if we can proceed to use opcache_invalidate to flush a file from the OPCache
*
* @since __DEPLOY_VERSION__
*/
public static function canFlushFileCache()
{
if (isset(static::$canFlushFileCache))
{
return static::$canFlushFileCache;
}

if (ini_get('opcache.enable')
&& function_exists('opcache_invalidate')
&& (!ini_get('opcache.restrict_api') || stripos(realpath($_SERVER['SCRIPT_FILENAME']), ini_get('opcache.restrict_api')) === 0)
)
{
static::$canFlushFileCache = true;
}
else
{
static::$canFlushFileCache = false;
}

return static::$canFlushFileCache;
}
}