Update auto_loader to load each class file in the project

Update the auto_loader class to use an include statement on each file in the project to load the class within the file. This will allow mismatched names within the file to be loaded and mapped according to the declaration instead of the filename. The class is then checked against the parsed classes from the PHP engine so that namespaces are available and mapped to the file they were declared in. An update was also made to the search algorithm used to find a file that was not already loaded by collapsing the array to have only valid matches to increase performance on a cache miss. Logging within the auto_loader has been moved to a function.
Multiple files were modified to allow the include statement. When the class has the `if(class_exists())` statement, the auto_loader is called to check for the class. This caused an infinite loop scenario so all wrappers have been removed. The auto_loader will now break the loop by directly modifying the internal classes array instead of trying to restart with the 'reload_classes' method.

- APCu is used to cache classes so any loading of the classes is done only once. To clear the APCu cache, restart php-fpm or call the auto_loader::clear_cache() function.
- Cache file is used when APCu is not available. To clear the cache remove it from the tmp folder or call the auto_loader::clear_cache() function.
- All classes must no longer have a class_exists wrapper to benefit from the performance boost.
- Classes should not be directly included when the auto_loader is used.
This commit is contained in:
Tim Fry 2025-03-12 15:30:20 -03:00
parent 58518d4e9d
commit 1aaf522e21
1 changed files with 178 additions and 85 deletions

View File

@ -1,29 +1,37 @@
<?php
/*
FusionPBX
Version: MPL 1.1
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2024
the Initial Developer. All Rights Reserved.
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2024
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
/**
* Auto Loader class
* Searches for project files when a class is required. Debugging mode can be set using:
* - export DEBUG=1
* OR
* - debug=true is appended to the url
*/
class auto_loader {
const FILE = 'autoloader_cache.php';
@ -37,11 +45,20 @@ class auto_loader {
*/
private $apcu_enabled;
/**
* Cache path and file name
* @var string
*/
private static $cache_file = null;
public function __construct($project_path = '') {
//set if we can use RAM cache
$this->apcu_enabled = function_exists('apcu_enabled') && apcu_enabled();
//set cache location
self::$cache_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::FILE;
//classes must be loaded before this object is registered
if (!$this->load_cache()) {
//cache miss so load them
@ -61,12 +78,14 @@ class auto_loader {
//update RAM cache when available
if ($this->apcu_enabled) {
apcu_store(self::CACHE_KEY, $this->classes);
$success = apcu_store(self::CACHE_KEY, $this->classes);
//do not save to drive when we are using apcu
if ($success) return true;
}
//ensure we have somewhere to put the file
if (empty($file)) {
$file = sys_get_temp_dir() . '/' . self::FILE;
$file = self::$cache_file;
}
//export the classes array using PHP engine
@ -77,13 +96,10 @@ class auto_loader {
if ($result !== false) {
return true;
}
//file failed to save - send error to syslog when debugging
$error_array = error_get_last();
//send to syslog when debugging
if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog(LOG_WARNING, "[php][auto_loader] " . $error_array['message']);
closelog();
}
self::log(LOG_WARNING, $error_array['message'] ?? '');
return false;
}
@ -99,21 +115,21 @@ class auto_loader {
//use a standard file
if (empty($file)) {
$file = sys_get_temp_dir() . '/'. self::FILE;
$file = self::$cache_file;
}
//use PHP engine to parse it
if (file_exists($file)) {
$this->classes = include $file;
}
//assign to an array
if (!empty($this->classes)) {
//cache edge case of first time using apcu cache
if ($this->apcu_enabled) {
apcu_store(self::CACHE_KEY, $this->classes);
}
return true;
//catch edge case of first time using apcu cache
if ($this->apcu_enabled) {
apcu_store(self::CACHE_KEY, $this->classes);
}
return false;
//return true when we have classes and false if the array is still empty
return !empty($this->classes);
}
public function reload_classes($project_path = '') {
@ -122,26 +138,87 @@ class auto_loader {
$project_path = dirname(__DIR__, 2);
}
//build the array of all classes
$search_path = [];
$search_path = array_merge($search_path, glob($project_path . '/resources/classes/*.php'));
$search_path = array_merge($search_path, glob($project_path . '/resources/interfaces/*.php'));
$search_path = array_merge($search_path, glob($project_path . '/resources/traits/*.php'));
$search_path = array_merge($search_path, glob($project_path . '/*/*/resources/classes/*.php'));
$search_path = array_merge($search_path, glob($project_path . '/*/*/resources/interfaces/*.php'));
$search_path = array_merge($search_path, glob($project_path . '/*/*/resources/traits/*.php'));
//build the array of all locations for classes in specific order
$search_path = [
$project_path . '/resources/interfaces/*.php',
$project_path . '/resources/traits/*.php',
$project_path . '/resources/classes/*.php',
$project_path . '/*/*/resources/interfaces/*.php',
$project_path . '/*/*/resources/traits/*.php',
$project_path . '/*/*/resources/classes/*.php',
$project_path . '/core/authentication/resources/classes/plugins/*.php',
];
//get all php files for each path
$files = [];
foreach ($search_path as $path) {
$files = array_merge($files, glob($path));
}
//reset the current array
$this->classes = [];
//store the class name (key) and the path (value)
foreach ($search_path as $path) {
$this->classes[basename($path, '.php')] = $path;
}
//store PHP language declared classes, interfaces, and traits
$curr_classes = get_declared_classes();
$curr_interfaces = get_declared_interfaces();
$curr_traits = get_declared_traits();
//store the class name (key) and the path (value)
foreach ($files as $file) {
//include the new class
try {
include_once $file;
} catch (Exception $e) {
//report the error
self::log(LOG_ERR, "Exception while trying to include file '$file': " . $e->getMessage());
continue;
}
//get the new classes
$new_classes = get_declared_classes();
$new_interfaces = get_declared_interfaces();
$new_traits = get_declared_traits();
//check for a new class
$classes = array_diff($new_classes, $curr_classes);
if (!empty($classes)) {
foreach ($classes as $class) {
$this->classes[$class] = $file;
}
//overwrite previous array with new values
$curr_classes = $new_classes;
}
//check for a new interface
$interfaces = array_diff($new_interfaces, $curr_interfaces);
if (!empty($interfaces)) {
foreach ($interfaces as $interface) {
$this->classes[$interface] = $file;
}
//overwrite previous array with new values
$curr_interfaces = $new_interfaces;
}
//check for a new trait
$traits = array_diff($new_traits, $curr_traits);
if (!empty($traits)) {
foreach ($traits as $trait) {
$this->classes[$trait] = $file;
}
//overwrite previous array with new values
$curr_traits = $new_traits;
}
}
}
private function loader($class_name) : bool {
/**
* The loader is set to private because only the PHP engine should be calling this method
* @param string $class_name The class name that needs to be loaded
* @return bool True if the class is loaded or false when the class is not found
* @access private
*/
private function loader($class_name): bool {
//sanitize the class name
$class_name = preg_replace('[^a-zA-Z0-9_]', '', $class_name);
@ -161,32 +238,29 @@ class auto_loader {
}
//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();
}
self::log(LOG_WARNING, "class '$class_name' not found in cache");
//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");
$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");
$search_path[] = glob($project_path . "/*/*/resources/classes/" . $class_name . ".php");
//find the path
$path = self::autoload_search($search_path);
if (!empty($path)) {
//collapse all entries to only the matched entry
$matches = array_filter($search_path);
if (!empty($matches)) {
$path = array_pop($matches)[0];
//include the class or interface
include $path;
//include the class, interface, or trait
include_once $path;
//make sure to reload the cache after we found a new class
$this->reload_classes();
//inject the class in to the array
$this->classes[$class_name] = $path;
//update the cache with new classes
$this->update_cache();
@ -196,35 +270,54 @@ class auto_loader {
}
//send to syslog when debugging
if (!empty($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') {
openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog(LOG_WARNING, "[php][auto_loader] class not found name: ".$class_name);
closelog();
}
self::log(LOG_ERR, "class '$class_name' not found name");
//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;
}
/**
* Returns a list of classes loaded by the auto_loader. If no classes have been loaded an empty array is returned.
* @return array List of classes loaded by the auto_loader or empty array
*/
public function get_class_list(): array {
if (!empty($this->classes)) {
return $this->classes;
}
return '';
return [];
}
public static function clear_cache() {
public static function clear_cache(string $file = '') {
//check for apcu cache
if (function_exists('apcu_enabled') && apcu_enabled()) {
apcu_delete(self::CACHE_KEY);
}
//set default file
if (empty(self::$cache_file)) {
self::$cache_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::FILE;
}
//set file to clear
if (empty($file)) {
$file = self::$cache_file;
}
//remove the file when it exists
if (file_exists($file)) {
@unlink($file);
$error_array = error_get_last();
//send to syslog when debugging with either environment variable or debug in the url
self::log(LOG_WARNING, $error_array['message'] ?? '');
}
}
private static function log(int $level, string $message): void {
if (filter_var($_REQUEST['debug'] ?? false, FILTER_VALIDATE_BOOL) || filter_var(getenv('DEBUG') ?? false, FILTER_VALIDATE_BOOL)) {
openlog("PHP", LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog($level, "[auto_loader] " . $message);
closelog();
}
}
}