| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
|-------------------------------------------------------------------------- |
| 5 |
| Register AutoLoader |
| 6 |
|-------------------------------------------------------------------------- |
| 7 |
| Vafpress Framework has separated app and core directories, developers can |
| 8 |
| put their extension code and configuration at app folder, as everything |
| 9 |
| inside app will be loaded first and will override class with the same |
| 10 |
| name with core classes. |
| 11 |
*/ |
| 12 |
VP_AutoLoader::add_namespaces(VP_NAMESPACE); |
| 13 |
VP_AutoLoader::add_directories(VP_CLASSES_DIR, VP_NAMESPACE); |
| 14 |
VP_AutoLoader::register(); |
| 15 |
|
| 16 |
class VP_AutoLoader |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* Indicates if VP_AutoLoader has been registered. |
| 21 |
* |
| 22 |
* @var boolean |
| 23 |
*/ |
| 24 |
protected static $registered = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* The registered directories |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
protected static $directories = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* THe registered namespaces |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
protected static $namespaces = array(); |
| 39 |
|
| 40 |
/** |
| 41 |
* Autoloading logic |
| 42 |
* |
| 43 |
* @param String $class Class name |
| 44 |
* @return Boolean Whether the loading succeded. |
| 45 |
*/ |
| 46 |
public static function load($class) |
| 47 |
{ |
| 48 |
clearstatcache(); |
| 49 |
|
| 50 |
// figure out namespace and halt process if not in our namespace |
| 51 |
$namespace = self::discover_namespace($class); |
| 52 |
if($namespace === '') |
| 53 |
return; |
| 54 |
|
| 55 |
$class = self::normalize_class($class, $namespace); |
| 56 |
|
| 57 |
foreach (self::$directories[$namespace] as $dir) |
| 58 |
{ |
| 59 |
$file = $dir . DIRECTORY_SEPARATOR . $class; |
| 60 |
|
| 61 |
// if( $dir === end(self::$directories) ) |
| 62 |
// { |
| 63 |
// require $file; |
| 64 |
// return true; |
| 65 |
// } |
| 66 |
|
| 67 |
if (is_link($file)) |
| 68 |
{ |
| 69 |
$file = readlink($file); |
| 70 |
} |
| 71 |
|
| 72 |
// $real = realpath($file); |
| 73 |
// if($real) $file = $real; |
| 74 |
|
| 75 |
if(is_file($file)) |
| 76 |
{ |
| 77 |
require $file; |
| 78 |
return true; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Discover namespace from a string |
| 85 |
* |
| 86 |
* @param String $key A class name or namespaced key |
| 87 |
* @return String Namespace |
| 88 |
*/ |
| 89 |
public static function discover_namespace($key) |
| 90 |
{ |
| 91 |
$namespace = ''; |
| 92 |
foreach (self::$namespaces as $ns) |
| 93 |
{ |
| 94 |
if (strpos($key, $ns) === 0) |
| 95 |
{ |
| 96 |
$namespace = $ns; |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
return $namespace; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Register autoloader |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public static function register() |
| 109 |
{ |
| 110 |
if(self::$registered !== TRUE) |
| 111 |
{ |
| 112 |
spl_autoload_register(array('VP_AutoLoader', 'load')); |
| 113 |
} |
| 114 |
self::$registered = TRUE; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Add a namespace |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public static function add_namespaces($namespaces) |
| 123 |
{ |
| 124 |
self::$namespaces = array_merge(self::$namespaces, (array) $namespaces); |
| 125 |
self::$namespaces = array_unique(self::$namespaces); |
| 126 |
usort(self::$namespaces, array('self', 'sort')); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Sort by length |
| 131 |
*/ |
| 132 |
private static function sort($a, $b) |
| 133 |
{ |
| 134 |
return strlen($b) - strlen($a); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Add directories to the autoloader, loading process will be run in orderly fashion |
| 140 |
* of directory addition. |
| 141 |
* |
| 142 |
* @param String|Array $directories |
| 143 |
* @param String $namespace |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public static function add_directories($directories, $namespace) |
| 147 |
{ |
| 148 |
if(in_array($namespace, self::$namespaces)) |
| 149 |
{ |
| 150 |
if(!isset(self::$directories[$namespace])) |
| 151 |
self::$directories[$namespace] = array(); |
| 152 |
self::$directories[$namespace] = array_merge(self::$directories[$namespace], (array) $directories); |
| 153 |
self::$directories[$namespace] = array_unique(self::$directories[$namespace]); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Remove directories. |
| 159 |
* |
| 160 |
* @param String|Array $directories |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public static function remove_directories($directories = null, $namespace) |
| 164 |
{ |
| 165 |
// check if namespace existed |
| 166 |
if(!in_array($namespace, self::$namespaces)) |
| 167 |
return; |
| 168 |
|
| 169 |
// annihilate everything if none / null passed |
| 170 |
if(is_null($directories)) |
| 171 |
{ |
| 172 |
self::$directories[$namespace] = array(); |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
// prepare directories to be filtered |
| 177 |
$directories = (array) $directories; |
| 178 |
|
| 179 |
// do the filtering |
| 180 |
foreach (self::$directories[$namespace] as $key => $dir) |
| 181 |
{ |
| 182 |
if(in_array($dir, $directories)) |
| 183 |
{ |
| 184 |
unset(self::$directories[$namespace][$key]); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Normalize class to be loaded |
| 192 |
* |
| 193 |
* @param String $class Class name |
| 194 |
* @return String Normalized class name |
| 195 |
*/ |
| 196 |
public static function normalize_class($class, $namespace) |
| 197 |
{ |
| 198 |
$class = ltrim($class, '\\'); |
| 199 |
$class = str_replace($namespace, '', $class); |
| 200 |
$class = ltrim($class, '_'); |
| 201 |
$class = strtolower($class); |
| 202 |
return str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get all directories |
| 207 |
* |
| 208 |
* @return Array |
| 209 |
*/ |
| 210 |
public static function get_directories() |
| 211 |
{ |
| 212 |
return self::$directories; |
| 213 |
} |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* EOF |
| 219 |
*/ |