rlerdorf/ZendOptimizerPlus — pull request #1 by Danack (2014-02-01 14:29:29+00:00)
Title: Add function to check if script is 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. 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.