PluginProbe
CSS & JavaScript Toolbox / 11.5
CSS & JavaScript Toolbox v11.5
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / framework / autoload / loader.php

loader.php in CSS & JavaScript Toolbox 11.5, at framework/autoload/loader.php

192 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Find ClassAutoloader instance used for loading specific class.
78 *
79 * The method is using Prefix algorithm for finding class autoloader
80 *
81 * @param mixed $className
82 */
83 public static function & findClassLoader($className) {
84 # Getting class prefix
85 $classComponents = explode('_', $className);
86 $prefix = $classComponents[0];
87 # Find autoloader
88 $instance = isset(self::$instances[$prefix]) ? self::$instances[$prefix] : null;
89 return $instance;
90 }
91
92 /**
93 * put your comment there...
94 *
95 * @param mixed $clsName
96 */
97 public function getClassComponent($clsName) {
98 // Initialize.
99 $component = (object) array();
100 // Lowerize!
101 $clsName = strtolower($clsName);
102 // Get class frags by gettign all names between _ character.
103 $fragments = explode('_', $clsName);
104 // Get components / exclude the prefix @ index [0].
105 // Name.
106 $component->name = $fragments[1];
107 // Get path (after the prefix and just before the name!).
108 $component->path = array();
109 for ($frag = 1; $frag < (count($fragments) - 1); $frag++) {
110 $component->path[] = $fragments[$frag] ;
111 }
112 $component->path = join(DIRECTORY_SEPARATOR, $component->path);
113 // Name
114 $component->name = end($fragments);
115 // Fulle absolute path.
116 return $component;
117 }
118
119 /**
120 * put your comment there...
121 *
122 * @param mixed $classInfo
123 */
124 public function getClassAbsolutePath($classInfo) {
125 // fullpath = abspath + classpath + classname + .php
126 $fullPath = $this->path . DIRECTORY_SEPARATOR .
127 $classInfo->path . DIRECTORY_SEPARATOR . $classInfo->name . '.php';
128 return $fullPath;
129 }
130
131 /**
132 * put your comment there...
133 *
134 * @param mixed $name
135 */
136 public function getClassFile($name, $file) {
137 // Get class component.
138 $component = $this->getClassComponent($name);
139 // Get class absolute path.
140 $path = $this->path . DIRECTORY_SEPARATOR . $component->path;
141 return $path . DIRECTORY_SEPARATOR . $file;
142 }
143
144 /**
145 * put your comment there...
146 *
147 */
148 public function getPath() {
149 return $this->path;
150 }
151
152 /**
153 * put your comment there...
154 *
155 */
156 public function getPrefix() {
157 return $this->prefix;
158 }
159
160 /**
161 * put your comment there...
162 *
163 * @param mixed $class
164 */
165 public function loadClass($class) {
166 $classFile = '';
167 // First, check the map!
168 if ($this->map()->offsetExists($class)) {
169 $classMappedPath = $this->map()->offsetGet($class);
170 $classFile = $this->path . DIRECTORY_SEPARATOR . $classMappedPath;
171 }
172 // Any class start with our prefix should be auto loaded!
173 else if (strpos($class, "{$this->prefix}_") === 0) {
174 // Get class paths.
175 $classFile = $this->getClassAbsolutePath($this->getClassComponent($class));
176 }
177 // Whatever a class file is set, import it!
178 if ($classFile && file_exists($classFile)) {
179 require $classFile;
180 }
181 }
182
183 /**
184 * put your comment there...
185 *
186 */
187 public function map() {
188 return $this->map;
189 }
190
191 } // End class.
192