1
|
|
|
<?php /** MicroAutoloader */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Base; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Autoload class file. |
7
|
|
|
* |
8
|
|
|
* @author Oleg Lunegov <[email protected]> |
9
|
|
|
* @link https://github.com/lugnsk/micro |
10
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
11
|
|
|
* @license /LICENSE |
12
|
|
|
* @package Micro |
13
|
|
|
* @subpackage Base |
14
|
|
|
* @version 1.0 |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class Autoload |
18
|
|
|
{ |
19
|
|
|
/** @var array $aliases Autoload aliases maps */ |
20
|
|
|
private static $aliases = []; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Setting or installing new alias |
25
|
|
|
* |
26
|
|
|
* @access public |
27
|
|
|
* |
28
|
|
|
* @param string $alias name for new alias |
29
|
|
|
* @param string $realPath path of alias |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
* @static |
33
|
|
|
*/ |
34
|
|
|
public static function setAlias($alias, $realPath) |
35
|
|
|
{ |
36
|
|
|
static::$aliases[strtolower($alias)][] = $realPath; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function getAlias($alias) |
40
|
|
|
{ |
41
|
|
|
$alias = strtolower($alias); |
42
|
|
|
|
43
|
|
|
return array_key_exists($alias, static::$aliases) ? static::$aliases[$alias] : []; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Loader classes |
48
|
|
|
* |
49
|
|
|
* @access public |
50
|
|
|
* |
51
|
|
|
* @param string $className search class name |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
* @static |
55
|
|
|
*/ |
56
|
|
|
public static function loader($className) |
57
|
|
|
{ |
58
|
|
|
if ($path = static::getClassPath(ltrim($className, '\\'))) { |
59
|
|
|
/** @noinspection PhpIncludeInspection */ |
60
|
|
|
require_once $path; |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get class path |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* |
73
|
|
|
* @param string $className search class name |
74
|
|
|
* @param string $extension extension of class |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
* @static |
78
|
|
|
*/ |
79
|
|
|
public static function getClassPath($className, $extension = '.php') |
80
|
|
|
{ |
81
|
|
|
$prefix = $className = static::CamelCaseToLowerNamespace(str_replace('_', '\\', $className)); |
|
|
|
|
82
|
|
|
|
83
|
|
|
while (false !== $position = strrpos($prefix, '\\')) { |
84
|
|
|
$prefix = substr($prefix, 0, $position); |
85
|
|
|
|
86
|
|
|
if (!array_key_exists($prefix, static::$aliases)) { |
|
|
|
|
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach (static::$aliases[$prefix] as $dir) { |
|
|
|
|
91
|
|
|
$path = $dir . '\\' . substr($className, mb_strlen($prefix) + 1); |
92
|
|
|
$absolutePath = str_replace('\\', DIRECTORY_SEPARATOR, $path) . $extension; |
93
|
|
|
|
94
|
|
|
if (is_readable($absolutePath)) { |
95
|
|
|
return $absolutePath; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Convert first symbols of namespace to lowercase |
105
|
|
|
* |
106
|
|
|
* @access protected |
107
|
|
|
* |
108
|
|
|
* @param string $path |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
* @static |
112
|
|
|
*/ |
113
|
|
|
private static function CamelCaseToLowerNamespace($path) |
114
|
|
|
{ |
115
|
|
|
$classNameArr = array_map(function ($val) { |
116
|
|
|
return lcfirst($val); |
117
|
|
|
}, explode('\\', $path)); |
118
|
|
|
|
119
|
|
|
$classNameArr[] = ucfirst(array_pop($classNameArr)); |
120
|
|
|
|
121
|
|
|
return implode('\\', $classNameArr); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: