1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: jensk |
4
|
|
|
* Date: 13-3-2017 |
5
|
|
|
* Time: 15:04 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace library\cc; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class AutoloadUtil |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The decoupled autoload function for actually loading the classes |
15
|
|
|
* |
16
|
|
|
* @param $class |
17
|
|
|
* @param bool $throwException |
18
|
|
|
* |
19
|
|
|
* @return bool |
20
|
|
|
* @throws \Exception |
21
|
|
|
*/ |
22
|
|
|
public static function autoLoad($class, $throwException = true) |
23
|
|
|
{ |
24
|
|
|
if (class_exists($class, false)) { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
global $rootPath; |
29
|
|
|
$file = $rootPath . str_replace('\\', '/', $class) . ".php"; |
30
|
|
|
|
31
|
|
|
if (file_exists($file)) { |
32
|
|
|
return self::loadClassInExistingFile($class, $throwException, $file); |
33
|
|
|
} else { |
34
|
|
|
return self::handleUnloadableClass($class, $throwException, $rootPath, $file); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private static function handleUnloadableClass($class, $throwException, $rootPath, $file) |
39
|
|
|
{ |
40
|
|
|
$debug_backtrace = debug_backtrace(); |
41
|
|
|
if (isset($debug_backtrace[2]) && isset($debug_backtrace[2]['file']) && isset($debug_backtrace[2]['line'])) { |
42
|
|
|
return self::handleUnloadableClassWithBacktrace($class, $throwException, $rootPath, $file, $debug_backtrace); |
43
|
|
|
} else { |
44
|
|
|
return self::handleUnloadableClassWithoutBacktrace($class, $throwException, $file); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $class |
51
|
|
|
* @param $throwException |
52
|
|
|
* @param $file |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
|
|
private static function loadClassInExistingFile($class, $throwException, $file) |
58
|
|
|
{ |
59
|
|
|
require_once($file); |
60
|
|
|
if ($throwException) { |
61
|
|
|
if (class_exists($class, false) === false && interface_exists($class, false) === false) { |
62
|
|
|
throw new \Exception('Could not load class "' . $class . '" in file ' . $file); |
63
|
|
|
} else { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return class_exists($class, false) || interface_exists($class, false); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $class |
73
|
|
|
* @param $throwException |
74
|
|
|
* @param $rootPath |
75
|
|
|
* @param $file |
76
|
|
|
* @param $debug_backtrace |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
private static function handleUnloadableClassWithBacktrace($class, $throwException, $rootPath, $file, $debug_backtrace) |
81
|
|
|
{ |
82
|
|
|
if ($throwException) { |
83
|
|
|
errorHandler(0, 'Could not load class \'' . $class . '\' in file ' . $rootPath . $file, $debug_backtrace[2]['file'], $debug_backtrace[2]['line']); |
84
|
|
|
} else { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $class |
91
|
|
|
* @param $throwException |
92
|
|
|
* @param $file |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
private static function handleUnloadableClassWithoutBacktrace($class, $throwException, $file) |
98
|
|
|
{ |
99
|
|
|
if ($throwException) { |
100
|
|
|
throw new \Exception('Could not load class "' . $class . '" in file ' . $file . "\n" . 'Called from unknown origin.'); |
101
|
|
|
} else { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |