TrackManiaControl/application/core/Libs/FML/autoload.php

85 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* FancyManiaLinks - Automatic ManiaLink Generator Framework
*
2014-05-14 23:24:00 +02:00
* @author steeffeen
* @version 1.2
* @link http://github.com/steeffeen/FancyManiaLinks
2014-04-13 18:21:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
if (!defined('FML_PATH')) {
2014-04-15 15:47:57 +02:00
define('FML_PATH', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
}
2014-01-03 17:18:14 +01:00
if (!defined('FML_VERSION')) {
2014-05-14 23:24:00 +02:00
define('FML_VERSION', '1.2');
2014-01-03 17:18:14 +01:00
}
2014-04-15 15:47:57 +02:00
if (!defined('FML_SIMPLE_CLASSES')) {
define('FML_SIMPLE_CLASSES', false);
}
2014-01-21 20:30:40 +01:00
/*
* Autoload Function that loads FML Class Files on Demand
*/
2014-04-15 15:47:57 +02:00
if (!defined('FML_AUTOLOAD_DEFINED')) {
define('FML_AUTOLOAD_DEFINED', true);
spl_autoload_register(function ($className) {
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className);
2014-05-14 23:24:00 +02:00
$filePath = FML_PATH . $classPath . '.php';
2014-04-15 15:47:57 +02:00
if (file_exists($filePath)) {
// Load with FML namespace
require_once $filePath;
2014-05-14 23:24:00 +02:00
} else if (FML_SIMPLE_CLASSES) {
2014-04-15 15:47:57 +02:00
// Load as simple class name
if (!function_exists('loadSimpleClass')) {
2014-04-27 14:44:40 +02:00
/**
* Load FML Class Files from the given Directory
*
2014-05-14 23:24:00 +02:00
* @param string $className Class to load
* @param string $directory Directory to open
* @param array $excludes File Names to ignore
2014-04-27 14:44:40 +02:00
* @param string $baseNamespace Base Namespace
* @return bool
*/
2014-04-15 15:47:57 +02:00
function loadSimpleClass($className, $directory, $excludes, $baseNamespace) {
if ($dirHandle = opendir($directory)) {
2014-05-14 23:24:00 +02:00
$classParts = explode('\\', $className);
2014-04-15 15:47:57 +02:00
$simpleClassName = end($classParts);
2014-05-14 23:24:00 +02:00
$classFileName = $simpleClassName . '.php';
2014-04-15 15:47:57 +02:00
while ($fileName = readdir($dirHandle)) {
if (in_array($fileName, $excludes)) {
continue;
}
$filePath = $directory . $fileName;
if (is_dir($filePath)) {
$subDirectory = $filePath . DIRECTORY_SEPARATOR;
2014-05-14 23:24:00 +02:00
$namespace = $baseNamespace . $fileName . '\\';
$success = loadSimpleClass($className, $subDirectory, $excludes, $namespace);
if ($success) {
return true;
}
2014-04-15 15:47:57 +02:00
continue;
}
if (is_file($filePath)) {
if ($fileName == $classFileName) {
require_once $filePath;
class_alias($baseNamespace . $simpleClassName, $className, false);
return true;
}
continue;
}
}
closedir($dirHandle);
}
return false;
}
}
2014-05-14 23:24:00 +02:00
$excludes = array('.', '..');
2014-04-15 15:47:57 +02:00
$baseNamespace = 'FML\\';
loadSimpleClass($className, FML_PATH . 'FML' . DIRECTORY_SEPARATOR, $excludes, $baseNamespace);
}
});
}