1
|
|
|
<?php |
2
|
|
|
namespace Suricate; |
3
|
|
|
|
4
|
|
|
class AutoLoader |
5
|
|
|
{ |
6
|
2 |
|
public static function autoload($class) |
7
|
|
|
{ |
8
|
|
|
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class); |
9
|
|
|
|
10
|
|
|
$filename = dirname(__DIR__) . DIRECTORY_SEPARATOR . $class . '.php'; |
11
|
|
|
|
12
|
|
|
if (is_file($filename)) { |
13
|
|
|
include $filename; |
14
|
|
|
} else { |
|
|
|
|
15
|
|
|
//throw new \Exception("Object '$class' not found", 2); |
|
|
|
|
16
|
2 |
|
} |
17
|
|
|
|
18
|
|
|
/* |
|
|
|
|
19
|
|
|
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class); |
20
|
|
|
$convertedClassName = preg_replace('|([a-z])([A-Z])|', '$1_$2', $class); |
21
|
|
|
|
22
|
|
|
$trimmedClass = ltrim($class, DIRECTORY_SEPARATOR); |
23
|
|
|
|
24
|
|
|
if (($sepPos = strpos($trimmedClass, DIRECTORY_SEPARATOR)) !== false) { |
25
|
|
|
$classRootNameSpace = substr($trimmedClass, 0, $sepPos); |
26
|
|
|
$convertedClassName = substr( |
27
|
|
|
$trimmedClass, |
28
|
|
|
strrpos($trimmedClass, DIRECTORY_SEPARATOR) + 1 |
29
|
|
|
); |
30
|
|
|
} else { |
31
|
|
|
$classRootNameSpace = ''; |
32
|
|
|
} |
33
|
|
|
$splitted = explode('_', $convertedClassName); |
34
|
|
|
$classType = strtolower(array_pop($splitted)); |
35
|
|
|
$classNameSpace = strtolower(array_shift($splitted)); |
36
|
|
|
$dir = ''; |
37
|
|
|
$thisClass = str_replace(__NAMESPACE__.'\\', '', __CLASS__) . "<br/>"; |
38
|
|
|
echo $thisClass; |
39
|
|
|
|
40
|
|
|
if (isset(self::$dirMap[$classType])) { |
41
|
|
|
$dir = self::$dirMap[$classType]; |
42
|
|
|
$filename = self::$root . '/' . $dir . '/' . str_replace(ucFirst($classType), '', $class) . '.php'; |
43
|
|
|
} elseif (isset(self::$dirMap[$classNameSpace])) { |
44
|
|
|
$dir = self::$dirMap[$classNameSpace]; |
45
|
|
|
$filename = self::$root . '/' . $dir . '/' . $class . '.php'; |
46
|
|
|
} elseif (isset(self::$dirMap[$classRootNameSpace])) { |
47
|
|
|
$dir = self::$dirMap[$classRootNameSpace]; |
48
|
|
|
$filename = self::$root . '/' . $dir . '/' . str_replace(ucFirst($classRootNameSpace), '', $class) . '.php'; |
49
|
|
|
} elseif (isset(self::$dirMap['default'])) { |
50
|
|
|
$dir = self::$dirMap['default']; |
51
|
|
|
$filename = self::$root . '/' . $dir . '/' . $class . '.php'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($dir != '') { |
55
|
|
|
if (is_file($filename)) { |
56
|
|
|
include $filename; |
57
|
|
|
} else { |
58
|
|
|
throw new \Exception("Object '$class' not found", 2); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
throw new \Exception("Unknown object type / No directory found", 1); |
62
|
|
|
} |
63
|
|
|
*/ |
64
|
|
|
|
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
public static function loadClass($class) |
68
|
|
|
{ |
69
|
|
|
if (!class_exists($class, false) && !interface_exists($class, false)) { |
70
|
|
|
self::autoload($class); |
71
|
|
|
/* |
|
|
|
|
72
|
|
|
if (!class_exists($class, false) && !interface_exists($class, false)) { |
73
|
|
|
throw new \Exception("File loaded, but '$class' does not exist"); |
74
|
|
|
} |
75
|
|
|
*/ |
76
|
|
|
} |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
public static function register() |
80
|
|
|
{ |
81
|
|
|
ini_set('unserialize_callback_func', 'spl_autoload_call'); |
82
|
|
|
spl_autoload_register(__NAMESPACE__ .'\Autoloader::loadClass'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function unRegister() |
86
|
|
|
{ |
87
|
|
|
spl_autoload_unregister(__NAMESPACE__ .'\Autoloader::loadClass'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.