Refactor autoload path creation in autoload.php

In this commit, code in autoload.php has been refactored to improve readability by using DIRECTORY_SEPARATOR for creating file paths. This ensures cross-platform compatibility. Additionally, added 'function' directory to class autoload procedure to facilitate future function based autoloads.
This commit is contained in:
Ivor Barhansky 2023-12-09 15:51:08 +02:00
parent a9265dc4d8
commit 38c0e292ac

View File

@ -9,13 +9,14 @@ const GAME_VERSION = 'alpha-7.4';
require_once 'mysql_override.php';
spl_autoload_register(function (string $className) {
$rootdir = $_SERVER['DOCUMENT_ROOT'] . '/_incl_data';
$rootdir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '_incl_data' . DIRECTORY_SEPARATOR;
# 1 with namespaces
# 2 without
$fileName = [
$rootdir . '/class/' . str_replace('\\', DIRECTORY_SEPARATOR, $className . '.php'),
$rootdir . '/class/' . $className . '.php',
$rootdir . '/vendor/' . $className . '.php',
$rootdir . 'class' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className . '.php'),
$rootdir . 'class' . DIRECTORY_SEPARATOR . $className . '.php',
$rootdir . 'function' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className . '.php'),
$rootdir . 'vendor' . DIRECTORY_SEPARATOR . $className . '.php',
];
foreach ($fileName as $file) {
if (file_exists($file)) {
@ -26,7 +27,7 @@ spl_autoload_register(function (string $className) {
});
spl_autoload_register(function (string $classname) {
$rootdir = $_SERVER['DOCUMENT_ROOT'] . '/_incl_data';
$rootdir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '_incl_data';
$classMap = [
'NewCombats' => $rootdir . '/class/',
'Insallah' => $rootdir . '/class/Insallah/',