game/_incl_data/autoload.php
Ivor Barhansky 8efb371909 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.
2023-12-09 14:14:33 +00:00

52 lines
1.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
//error_reporting(E_ALL);
const GAME = true; // Для совместимости с этой "защитой".
const GAME_VERSION = 'alpha-7.4';
// Новая автозагрузка.
// ВНИМАНИЕ! Не введено в эксплуатацию!
require_once 'mysql_override.php';
spl_autoload_register(function (string $className) {
$rootdir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '_incl_data' . DIRECTORY_SEPARATOR;
# 1 with namespaces
# 2 without
$fileName = [
$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)) {
require_once $file;
break;
}
}
});
spl_autoload_register(function (string $classname) {
$rootdir = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . '_incl_data';
$classMap = [
'NewCombats' => $rootdir . '/class/',
'Insallah' => $rootdir . '/class/Insallah/',
'DarksLight2' => $rootdir . '/class/DarksLight2/',
];
$parts = explode('\\', $classname);
$namespace = array_shift($parts);
$classFile = array_pop($parts) . '.php';
if (!array_key_exists($namespace, $classMap)) {
return;
}
$path = implode(DIRECTORY_SEPARATOR, $parts);
$file = $classMap[$namespace] . $path . DIRECTORY_SEPARATOR . $classFile;
if (!file_exists($file) && !class_exists($classname)) {
return;
}
require_once $file;
});