zendtech/ZendOptimizerPlus — pull request #166 by Danack (2014-02-15 17:44:50+00:00)
Title: Added function to check if OPCache already has a script cached

The PR is to be able to check to see if OPCache already has a script cached - as it's useful information for autoloaders.

For some class autoloaders (including Composer) it is possible for a script to exist in one of several possible directores. To load that file the autoloader needs to call file_exists e.g. from the composer autoloader:

```
foreach ($this->prefixes[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
return $dir . DIRECTORY_SEPARATOR . $classPath;
}
}
}
}
```
As PHP doesn't cache the result when the file doesn't exist, this can easily cause several hits to the file system.

Adding the function opcache_script_cached allows the autoloader to check to see if OPCache has already loaded the file in all of the possible locations that where a script could exist, before hitting the filesystem.

```
foreach ($this->prefixes[$first] as $prefix => $dirs) {
//Check all possible paths in OPCache before checking the file system
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
$filename = $dir.DIRECTORY_SEPARATOR.$classPath;
if (opcache_script_cached($filename) == true) {
return $filename;
}
}

foreach ($dirs as $dir) {
$filename = $dir.DIRECTORY_SEPARATOR.$classPath;
if (file_exists($filename)) {
return $filename;
}
}
}
}
```

i.e. you can use the information from opcache_script_cached to avoid any filesystem hit, once the script is loaded into OPCache.

Although this information is retrievable through opcache_get_status the user has to do a lot more work to use it, searching through the large array returned, and manually correcting the paths.