[TASK] Finalized modifications on ClientManager.
[TASK] Added support for further class paths to ClassLoader. [TASK] Started to implement the irc client dispatcher. (partially works now)
This commit is contained in:
@@ -8,20 +8,51 @@ namespace JPT\SocketFramework\Core;
|
||||
*/
|
||||
class ClassLoader {
|
||||
|
||||
/**
|
||||
* Contains class name prefix and path prefix for all class paths it shall handle
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $classNameMapping = array();
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
* Registers the main class path for the socket framework.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->classNameMapping = array(
|
||||
'JPT\SocketFramework' => 'SocketFramework' . \DIRECTORY_SEPARATOR . 'Classes'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new class path for the autoloader
|
||||
*
|
||||
* @param string $classNamePrefix
|
||||
* @param string $pathPrefix
|
||||
*/
|
||||
public function addClassPathMapping($classNamePrefix, $pathPrefix) {
|
||||
$this->classNameMapping[$classNamePrefix] = $pathPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
$matchingClassNamePrefix = FALSE;
|
||||
foreach($this->classNameMapping AS $classNamePrefix => $pathPrefix) {
|
||||
if(substr($className, 0, strlen($classNamePrefix)) !== $classNamePrefix) continue;
|
||||
$matchingClassNamePrefix = TRUE;
|
||||
$classNameSuffix = substr($className, strlen($classNamePrefix), strlen($className));
|
||||
$fileName = $this->classNameMapping[$classNamePrefix];
|
||||
$fileName .= \DIRECTORY_SEPARATOR . str_replace('\\', \DIRECTORY_SEPARATOR, $classNameSuffix) . ".php";
|
||||
break;
|
||||
}
|
||||
if($matchingClassNamePrefix === FALSE) return;
|
||||
if(file_exists($fileName) === TRUE) {
|
||||
require_once($fileName);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user