| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
|
| 7 |
// Disallow direct access. |
| 8 |
defined('ABSPATH') or die("Access denied"); |
| 9 |
|
| 10 |
/** |
| 11 |
* Auto load CJT classes. |
| 12 |
* |
| 13 |
* @since 6.2 |
| 14 |
*/ |
| 15 |
class CJT_Framework_Autoload_Loader { |
| 16 |
|
| 17 |
/** |
| 18 |
* |
| 19 |
* |
| 20 |
* @var mixed |
| 21 |
*/ |
| 22 |
protected static $instances = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* put your comment there... |
| 26 |
* |
| 27 |
* @var mixed |
| 28 |
*/ |
| 29 |
protected $map; |
| 30 |
|
| 31 |
/** |
| 32 |
* put your comment there... |
| 33 |
* |
| 34 |
* @var mixed |
| 35 |
*/ |
| 36 |
protected $path = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* put your comment there... |
| 40 |
* |
| 41 |
* @var mixed |
| 42 |
*/ |
| 43 |
protected $prefix = null; |
| 44 |
|
| 45 |
/** |
| 46 |
* put your comment there... |
| 47 |
* |
| 48 |
* @param mixed $prefix |
| 49 |
* @param mixed $path |
| 50 |
* @return CJT_Autoload |
| 51 |
*/ |
| 52 |
public function __construct($prefix, $path) { |
| 53 |
// Initialize. |
| 54 |
$this->prefix = $prefix; |
| 55 |
$this->path = $path; |
| 56 |
$this->map = new ArrayObject(array()); |
| 57 |
// Resgister SPL auto load function. |
| 58 |
spl_autoload_register(array($this, 'loadClass')); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* put your comment there... |
| 64 |
* |
| 65 |
* @param mixed $prefix |
| 66 |
* @param mixed $path |
| 67 |
*/ |
| 68 |
public static function autoLoad($prefix = null, $path = null) { |
| 69 |
// Identify instances by prefixes! |
| 70 |
if (!isset(self::$instances[$prefix])) { |
| 71 |
self::$instances[$prefix] = new CJT_Framework_Autoload_Loader($prefix, $path); |
| 72 |
} |
| 73 |
return isset(self::$instances[$prefix]) ? self::$instances[$prefix] : null; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get an already registered loader by its exact prefix. |
| 78 |
* |
| 79 |
* Unlike autoLoad() this never registers anything, so it is safe to call |
| 80 |
* with a prefix that came from the request. |
| 81 |
* |
| 82 |
* @param string $prefix Loader prefix to look up. |
| 83 |
* @return CJT_Framework_Autoload_Loader|null Loader when registered, NULL otherwise. |
| 84 |
*/ |
| 85 |
public static function getRegisteredLoader($prefix) { |
| 86 |
return isset(self::$instances[$prefix]) ? self::$instances[$prefix] : null; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Find ClassAutoloader instance used for loading specific class. |
| 91 |
* |
| 92 |
* The method is using Prefix algorithm for finding class autoloader |
| 93 |
* |
| 94 |
* @param mixed $className |
| 95 |
*/ |
| 96 |
public static function & findClassLoader($className) { |
| 97 |
# Getting class prefix |
| 98 |
$classComponents = explode('_', $className); |
| 99 |
$prefix = $classComponents[0]; |
| 100 |
# Find autoloader |
| 101 |
$instance = isset(self::$instances[$prefix]) ? self::$instances[$prefix] : null; |
| 102 |
return $instance; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* put your comment there... |
| 107 |
* |
| 108 |
* @param mixed $clsName |
| 109 |
*/ |
| 110 |
public function getClassComponent($clsName) { |
| 111 |
// Initialize. |
| 112 |
$component = (object) array(); |
| 113 |
// Lowerize! |
| 114 |
$clsName = strtolower($clsName); |
| 115 |
// Get class frags by gettign all names between _ character. |
| 116 |
$fragments = explode('_', $clsName); |
| 117 |
// Get components / exclude the prefix @ index [0]. |
| 118 |
// Name. |
| 119 |
$component->name = $fragments[1]; |
| 120 |
// Get path (after the prefix and just before the name!). |
| 121 |
$component->path = array(); |
| 122 |
for ($frag = 1; $frag < (count($fragments) - 1); $frag++) { |
| 123 |
$component->path[] = $fragments[$frag] ; |
| 124 |
} |
| 125 |
$component->path = join(DIRECTORY_SEPARATOR, $component->path); |
| 126 |
// Name |
| 127 |
$component->name = end($fragments); |
| 128 |
// Fulle absolute path. |
| 129 |
return $component; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* put your comment there... |
| 134 |
* |
| 135 |
* @param mixed $classInfo |
| 136 |
*/ |
| 137 |
public function getClassAbsolutePath($classInfo) { |
| 138 |
// fullpath = abspath + classpath + classname + .php |
| 139 |
$fullPath = $this->path . DIRECTORY_SEPARATOR . |
| 140 |
$classInfo->path . DIRECTORY_SEPARATOR . $classInfo->name . '.php'; |
| 141 |
return $fullPath; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* put your comment there... |
| 146 |
* |
| 147 |
* @param mixed $name |
| 148 |
*/ |
| 149 |
public function getClassFile($name, $file) { |
| 150 |
// Get class component. |
| 151 |
$component = $this->getClassComponent($name); |
| 152 |
// Get class absolute path. |
| 153 |
$path = $this->path . DIRECTORY_SEPARATOR . $component->path; |
| 154 |
return $path . DIRECTORY_SEPARATOR . $file; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* put your comment there... |
| 159 |
* |
| 160 |
*/ |
| 161 |
public function getPath() { |
| 162 |
return $this->path; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* put your comment there... |
| 167 |
* |
| 168 |
*/ |
| 169 |
public function getPrefix() { |
| 170 |
return $this->prefix; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* put your comment there... |
| 175 |
* |
| 176 |
* @param mixed $class |
| 177 |
*/ |
| 178 |
public function loadClass($class) { |
| 179 |
$classFile = ''; |
| 180 |
// First, check the map! |
| 181 |
if ($this->map()->offsetExists($class)) { |
| 182 |
$classMappedPath = $this->map()->offsetGet($class); |
| 183 |
$classFile = $this->path . DIRECTORY_SEPARATOR . $classMappedPath; |
| 184 |
} |
| 185 |
// Any class start with our prefix should be auto loaded! |
| 186 |
else if (strpos($class, "{$this->prefix}_") === 0) { |
| 187 |
// Get class paths. |
| 188 |
$classFile = $this->getClassAbsolutePath($this->getClassComponent($class)); |
| 189 |
} |
| 190 |
// Whatever a class file is set, import it! |
| 191 |
if ($classFile && file_exists($classFile)) { |
| 192 |
require $classFile; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* put your comment there... |
| 198 |
* |
| 199 |
*/ |
| 200 |
public function map() { |
| 201 |
return $this->map; |
| 202 |
} |
| 203 |
|
| 204 |
} // End class. |
| 205 |
|