浏览代码

Use an array in auto_loader

- Added a search for interfaces
- Updated the indentation
FusionPBX 1 年之前
父节点
当前提交
35af844636
共有 1 个文件被更改,包括 42 次插入55 次删除
  1. 42 55
      resources/classes/auto_loader.php

+ 42 - 55
resources/classes/auto_loader.php

@@ -17,7 +17,7 @@
 
 	The Initial Developer of the Original Code is
 	Mark J Crane <[email protected]>
-	Portions created by the Initial Developer are Copyright (C) 2008-2021
+	Portions created by the Initial Developer are Copyright (C) 2008-2024
 	the Initial Developer. All Rights Reserved.
 
 	Contributor(s):
@@ -30,71 +30,58 @@ class auto_loader {
 		spl_autoload_register(array($this, 'loader'));
 	}
 
-	private function loader($class_name) {
-		//set the default value
-			$class_found = false;
+	public static function autoload_search($array) : string {
+		if (!is_array($array) && count($path) != 0) {
+			return '';
+		}
+		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 '';
+	}
+
+	private function loader($class_name) : bool {
 
 		//sanitize the class name
-			$class_name = preg_replace('[^a-zA-Z0-9_]', '', $class_name);
+		$class_name = preg_replace('[^a-zA-Z0-9_]', '', $class_name);
 
-		//save the log to the syslog server
-			if (isset($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
-				openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
-			}
+		//use glob for a more extensive search for the classes (note: GLOB_BRACE doesn't work on some systems)
+		if (!class_exists($class_name)) {
+			//build the search path array
+			$search_path[] = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/classes/".$class_name.".php");
+			$search_path[] = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/interfaces/".$class_name.".php");
+			$search_path[] = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/resources/classes/".$class_name.".php");
+			$search_path[] = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/resources/interfaces/".$class_name.".php");
 
-		//find the most relevant class name
-			if (!$class_found && file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/classes/".$class_name.".php")) {
-				//first priority
-				$path = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/classes/".$class_name.".php";
-				$class_found = true;
-				if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
-					syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$path.", line: ".__line__);
-				}
-				include $path;
-			}
-			elseif (!$class_found && file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/core/".$class_name."/resources/classes/".$class_name.".php")) {
-				//second priority
-				$path = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/core/".$class_name."/resources/classes/".$class_name.".php";
-				$class_found = true;
-				if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
-					syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$path.", line: ".__line__);
-				}
-				include $path;
-			}
-			elseif (!$class_found && file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/app/".$class_name."/resources/classes/".$class_name.".php")) {
-				//third priority
-				$path = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/app/".$class_name."/resources/classes/".$class_name.".php";
-				$class_found = true;
+			//find the path
+			$path = self::autoload_search($search_path);
+			if (!empty($path)) {
+				//send to syslog
 				if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
+					openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
 					syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$path.", line: ".__line__);
+					closelog();
 				}
+
+				//include the class or interface
 				include $path;
-			}
 
-		//use glob for a more exensive search for the classes (note: GLOB_BRACE doesn't work on some systems)
-			if (!$class_found && !class_exists($class_name)) {
-				//fourth priority
-				$results_1 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/resources/classes/".$class_name.".php");
-				$results_2 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/resources/classes/".$class_name.".php");
-				$results = array_merge((array)$results_1,(array)$results_2);
-				unset($results_1, $results_2);
-				foreach ($results as &$class_file) {
-					if (!$class_found) {
-						$class_found = true;
-						if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
-							syslog(LOG_WARNING, "[php][autoloader] name: ".$class_name.", path: ".$class_file.", line: ".__line__);
-						}
-						include $class_file;
-						break;
-					}
-				}
-				unset($results);
+				//return boolean
+				return true;
 			}
+		}
 
-		//save the log to the syslog server
-			if (isset($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
-				closelog();
-			}
+		//return boolean
+		return false;
 	}
 }