44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
PHP
|
<?php
|
||
|
namespace JPT\SocketFramework\Core;
|
||
|
|
||
|
/**
|
||
|
* Simple class to capsule the classloader so more than one autoload function can be used.
|
||
|
*
|
||
|
* @author JPT
|
||
|
*/
|
||
|
class ClassLoader {
|
||
|
|
||
|
/**
|
||
|
* Autoloader function.
|
||
|
* All classes will be in Classes/$Package$/$ClassName$.php
|
||
|
*
|
||
|
* @throws \Exception
|
||
|
* @param string $className
|
||
|
* @return void
|
||
|
*/
|
||
|
public function loadClass($className) {
|
||
|
if(substr($className, 0, 19) !== "JPT\\SocketFramework") return;
|
||
|
$className = substr($className, 20);
|
||
|
$fileName = "SocketFramework" . \DIRECTORY_SEPARATOR . "Classes";
|
||
|
$fileName .= \DIRECTORY_SEPARATOR . str_replace("\\", \DIRECTORY_SEPARATOR, $className) . ".php";
|
||
|
|
||
|
if(file_exists($fileName) === TRUE) {
|
||
|
require_once($fileName);
|
||
|
} else {
|
||
|
throw new \Exception("Could not load class: '" . $className . "' - File does not exist: '" . $fileName . "'!", 1289659295);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Simple function that registers loadClass as an autoloader.
|
||
|
*
|
||
|
* @return void
|
||
|
* @throws Exception in case spl_autoload_register fails.
|
||
|
*/
|
||
|
public function initialize() {
|
||
|
spl_autoload_register(array($this, 'loadClass'), TRUE, TRUE);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
?>
|