Add old search method for auto loading classes (#7261)

Adding this commit to ensure backwards compatibility and a fail-safe mechanism for loading a class
This commit is contained in:
frytimo 2025-02-20 14:24:39 -04:00 committed by GitHub
parent 37840abb98
commit fd34a000a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 51 additions and 0 deletions

View File

@ -125,6 +125,41 @@ class auto_loader {
return true;
}
//cache miss
if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog(LOG_WARNING, "[php][auto_loader] class not found in cache: ".$class_name);
closelog();
}
//set project path using magic dir constant
$project_path = dirname(__DIR__, 2);
//build the search path array
$search_path[] = glob($project_path . "/resources/classes/".$class_name.".php");
$search_path[] = glob($project_path . "/resources/interfaces/".$class_name.".php");
$search_path[] = glob($project_path . "/resources/traits/".$class_name.".php");
$search_path[] = glob($project_path . "/*/*/resources/classes/".$class_name.".php");
$search_path[] = glob($project_path . "/*/*/resources/interfaces/".$class_name.".php");
$search_path[] = glob($project_path . "/*/*/resources/traits/".$class_name.".php");
//find the path
$path = self::autoload_search($search_path);
if (!empty($path)) {
//include the class or interface
include $path;
//make sure to reload the cache after we found a new class
$this->reload_classes();
//update the cache with new classes
$this->update_cache();
//return boolean
return true;
}
//send to syslog when debugging
if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
@ -135,4 +170,20 @@ class auto_loader {
//return boolean
return false;
}
public static function autoload_search($array) : string {
foreach($array as $path) {
if (is_array($path) && count($path) != 0) {
foreach($path as $sub_path) {
if (!empty($sub_path) && file_exists($sub_path)) {
return $sub_path;
}
}
}
elseif (!empty($path) && file_exists($path)) {
return $path;
}
}
return '';
}
}