| 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 |
* put your comment there... |
| 19 |
* |
| 20 |
* @var mixed |
| 21 |
*/ |
| 22 |
protected $map; |
| 23 |
|
| 24 |
/** |
| 25 |
* put your comment there... |
| 26 |
* |
| 27 |
* @var mixed |
| 28 |
*/ |
| 29 |
protected $path = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* put your comment there... |
| 33 |
* |
| 34 |
* @var mixed |
| 35 |
*/ |
| 36 |
protected $prefix = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* put your comment there... |
| 40 |
* |
| 41 |
* @param mixed $prefix |
| 42 |
* @param mixed $path |
| 43 |
* @return CJT_Autoload |
| 44 |
*/ |
| 45 |
public function __construct($prefix, $path) { |
| 46 |
// Initialize. |
| 47 |
$this->prefix = $prefix; |
| 48 |
$this->path = $path; |
| 49 |
$this->map = new ArrayObject(array()); |
| 50 |
// Resgister SPL auto load function. |
| 51 |
spl_autoload_register(array($this, 'loadClass')); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* put your comment there... |
| 57 |
* |
| 58 |
* @param mixed $prefix |
| 59 |
* @param mixed $path |
| 60 |
*/ |
| 61 |
public static function autoLoad($prefix = null, $path = null) { |
| 62 |
// Hold all auto load instances. |
| 63 |
static $instances = array(); |
| 64 |
// Identify instances by prefixes! |
| 65 |
if (!isset($instances[$prefix])) { |
| 66 |
$instances[$prefix] = new CJT_Framework_Autoload_Loader($prefix, $path); |
| 67 |
} |
| 68 |
return $instances[$prefix]; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* put your comment there... |
| 73 |
* |
| 74 |
* @param mixed $clsName |
| 75 |
*/ |
| 76 |
public function getClassComponent($clsName) { |
| 77 |
// Initialize. |
| 78 |
$component = (object) array(); |
| 79 |
// Lowerize! |
| 80 |
$clsName = strtolower($clsName); |
| 81 |
// Get class frags by gettign all names between _ character. |
| 82 |
$fragments = explode('_', $clsName); |
| 83 |
// Get components / exclude the prefix @ index [0]. |
| 84 |
// Name. |
| 85 |
$component->name = $fragments[1]; |
| 86 |
// Get path (after the prefix and just before the name!). |
| 87 |
$component->path = array(); |
| 88 |
for ($frag = 1; $frag < (count($fragments) - 1); $frag++) { |
| 89 |
$component->path[] = $fragments[$frag] ; |
| 90 |
} |
| 91 |
$component->path = join(DIRECTORY_SEPARATOR, $component->path); |
| 92 |
// Name |
| 93 |
$component->name = end($fragments); |
| 94 |
// Fulle absolute path. |
| 95 |
return $component; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* put your comment there... |
| 100 |
* |
| 101 |
* @param mixed $classInfo |
| 102 |
*/ |
| 103 |
public function getClassAbsolutePath($classInfo) { |
| 104 |
// fullpath = abspath + classpath + classname + .php |
| 105 |
$fullPath = $this->path . DIRECTORY_SEPARATOR . |
| 106 |
$classInfo->path . DIRECTORY_SEPARATOR . $classInfo->name . '.php'; |
| 107 |
return $fullPath; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* put your comment there... |
| 112 |
* |
| 113 |
* @param mixed $name |
| 114 |
*/ |
| 115 |
public function getClassFile($name, $file) { |
| 116 |
// Get class component. |
| 117 |
$component = $this->getClassComponent($name); |
| 118 |
// Get class absolute path. |
| 119 |
$path = $this->path . DIRECTORY_SEPARATOR . $component->path; |
| 120 |
return $path . DIRECTORY_SEPARATOR . $file; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* put your comment there... |
| 125 |
* |
| 126 |
* @param mixed $class |
| 127 |
*/ |
| 128 |
public function loadClass($class) { |
| 129 |
$classFile = ''; |
| 130 |
// First, check the map! |
| 131 |
if ($this->map()->offsetExists($class)) { |
| 132 |
$classMappedPath = $this->map()->offsetGet($class); |
| 133 |
$classFile = $this->path . DIRECTORY_SEPARATOR . $classMappedPath; |
| 134 |
} |
| 135 |
// Any class start with our prefix should be auto loaded! |
| 136 |
else if (strpos($class, "{$this->prefix}_") === 0) { |
| 137 |
// Get class paths. |
| 138 |
$classFile = $this->getClassAbsolutePath($this->getClassComponent($class)); |
| 139 |
} |
| 140 |
// Whatever a class file is set, import it! |
| 141 |
if ($classFile) { |
| 142 |
require $classFile; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* put your comment there... |
| 148 |
* |
| 149 |
*/ |
| 150 |
public function map() { |
| 151 |
return $this->map; |
| 152 |
} |
| 153 |
|
| 154 |
} // End class. |
| 155 |
|