From fd34a000a6465b3e5b62f9f84dc3656e5d355e00 Mon Sep 17 00:00:00 2001 From: frytimo Date: Thu, 20 Feb 2025 14:24:39 -0400 Subject: [PATCH] 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 --- resources/classes/auto_loader.php | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/resources/classes/auto_loader.php b/resources/classes/auto_loader.php index fcb0b4fce3..85dbcdb261 100644 --- a/resources/classes/auto_loader.php +++ b/resources/classes/auto_loader.php @@ -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 ''; + } }