|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Gorlum 18.06.2017 13:24 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Core; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Autoloader |
|
10
|
|
|
* |
|
11
|
|
|
* One of core class to supply autoload facilities to the engine |
|
12
|
|
|
* |
|
13
|
|
|
* @package Core |
|
14
|
|
|
*/ |
|
15
|
|
|
class Autoloader { |
|
16
|
|
|
const P_FOLDER = 'P_FOLDER'; |
|
17
|
|
|
const P_PREFIX = 'P_PREFIX'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string[][] $folders - [[P_FOLDER => (str)$absoluteFolder, P_PREFIX => (str)$prefixToIgnore]] |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $folders = []; |
|
23
|
|
|
|
|
24
|
|
|
protected static $autoloaderRegistered = false; |
|
25
|
|
|
|
|
26
|
1 |
|
protected static function _constructorStatic() { |
|
27
|
1 |
|
if(!static::$autoloaderRegistered) { |
|
28
|
|
|
spl_autoload_register(array(__CLASS__, 'autoloader')); |
|
29
|
|
|
static::$autoloaderRegistered = true; |
|
30
|
|
|
} |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $class - Fully-qualified path with namespaces |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public static function autoloader($class) { |
|
|
|
|
|
|
37
|
1 |
|
static::_constructorStatic(); |
|
38
|
|
|
|
|
39
|
1 |
|
foreach(static::$folders as $data) { |
|
40
|
1 |
|
$theClassFile = $class; |
|
41
|
|
|
|
|
42
|
1 |
|
if($data[static::P_PREFIX] && strrpos($class, $data[static::P_PREFIX]) !== false) { |
|
|
|
|
|
|
43
|
|
|
$theClassFile = substr($class, strlen($data[static::P_PREFIX])); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
$classFullFileName = str_replace('\\', '/', $data[static::P_FOLDER] . $theClassFile) . DOT_PHP_EX; |
|
|
|
|
|
|
47
|
1 |
|
if(file_exists($classFullFileName) && is_file($classFullFileName)) { |
|
48
|
1 |
|
require_once($classFullFileName); |
|
49
|
1 |
|
if(method_exists($class, '_constructorStatic')) { |
|
50
|
|
|
$class::_constructorStatic(); |
|
51
|
|
|
} |
|
52
|
1 |
|
} |
|
53
|
1 |
|
} |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $absoluteClassRoot - absolute path to root class folder |
|
58
|
|
|
* @param string $classPrefix - PHP class prefix to ignore. Can be whole namespace or part of it |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function register($absoluteClassRoot, $classPrefix = '') { |
|
61
|
|
|
static::_constructorStatic(); |
|
62
|
|
|
|
|
63
|
|
|
$absoluteClassRoot = str_replace('\\', '/', SN_ROOT_PHYSICAL . $absoluteClassRoot); |
|
64
|
|
|
|
|
65
|
|
|
if(!($absoluteClassRoot = realpath($absoluteClassRoot))) { |
|
66
|
|
|
// TODO - throw new \Exception("There is some error when installing autoloader for '{$absoluteClassRoot}' class prefix '{$classPrefix}'"); |
|
|
|
|
|
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
$absoluteClassRoot = str_replace('\\', '/', $absoluteClassRoot) . '/'; |
|
70
|
|
|
|
|
71
|
|
|
if($classPrefix && strrpos($classPrefix, 1) != '\\') { |
|
72
|
|
|
$classPrefix .= '\\'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
static::$folders[] = [ |
|
76
|
|
|
static::P_FOLDER => $absoluteClassRoot, |
|
77
|
|
|
static::P_PREFIX => $classPrefix, |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// /** |
|
82
|
|
|
// * @param string $relativeClassRoot - relative path to root class folder from game root (where index.php lies) |
|
83
|
|
|
// * @param string $classPrefix - PHP class prefix to ignore. Can be whole namespace or part of it |
|
84
|
|
|
// */ |
|
85
|
|
|
// public static function registerRelative($relativeClassRoot, $classPrefix = '') { |
|
86
|
|
|
// static::register(SN_ROOT_PHYSICAL . $relativeClassRoot, $classPrefix); |
|
87
|
|
|
// } |
|
88
|
|
|
|
|
89
|
|
|
public static function reset() { |
|
90
|
|
|
static::$folders = []; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|