1: <?php
2:
3: require_once $config->get('app.root').'alpha/util/AlphaErrorHandlers.inc';
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50:
51: class AlphaAutoLoader {
52: 53: 54: 55: 56:
57: private static $scannedDirs;
58:
59: 60: 61: 62: 63:
64: private static $logger;
65:
66: 67: 68: 69: 70:
71: private static function initFolderList() {
72: global $config;
73:
74: if(!isset(self::$logger) && class_exists('Logger'))
75: self::$logger = new Logger('AlphaAutoLoader');
76:
77: self::$scannedDirs = array(
78: $config->get('app.root').'alpha/controller/',
79: $config->get('app.root').'alpha/controller/front/',
80: $config->get('app.root').'alpha/exceptions/',
81: $config->get('app.root').'alpha/model/',
82: $config->get('app.root').'alpha/model/types/',
83: $config->get('app.root').'alpha/view/',
84: $config->get('app.root').'alpha/view/widgets/',
85: $config->get('app.root').'alpha/view/renderers/',
86: $config->get('app.root').'alpha/util/',
87: $config->get('app.root').'alpha/util/cache/',
88: $config->get('app.root').'alpha/util/codehighlight/',
89: $config->get('app.root').'alpha/util/convertors/',
90: $config->get('app.root').'alpha/util/feeds/',
91: $config->get('app.root').'alpha/util/graphs/',
92: $config->get('app.root').'alpha/util/helpers/',
93: $config->get('app.root').'alpha/util/metrics/',
94: $config->get('app.root').'alpha/util/filters/',
95: $config->get('app.root').'alpha/tests/',
96: $config->get('app.root').'controller/',
97: $config->get('app.root').'model/',
98: $config->get('app.root').'view/'
99: );
100: }
101:
102: 103: 104: 105: 106: 107:
108: public static function loadClass($className) {
109: global $config;
110:
111: if(!isset(self::$scannedDirs))
112: self::initFolderList();
113:
114:
115: $skipDirs = array();
116:
117: if(strpos($className, 'Controller') !== false) {
118: if(self::loadFile($config->get('app.root').'alpha/controller/', $className)) {
119:
120: return;
121: }else{
122:
123: array_push($skipDirs, $config->get('app.root').'alpha/controller/');
124: }
125:
126: if(self::loadFile($config->get('app.root').'alpha/controller/front/', $className)) {
127: return;
128: }else{
129: array_push($skipDirs, $config->get('app.root').'alpha/controller/front/');
130: }
131: }
132:
133: if(strpos($className, 'Exception') !== false) {
134: if(self::loadFile($config->get('app.root').'alpha/exceptions/', $className)) {
135: return;
136: }else{
137: array_push($skipDirs, $config->get('app.root').'alpha/exceptions/');
138: }
139: }
140:
141: if(strpos($className, 'DAO') !== false) {
142: if(self::loadFile($config->get('app.root').'alpha/model/', $className)) {
143: return;
144: }else{
145: array_push($skipDirs, $config->get('app.root').'alpha/model/');
146: }
147: }
148:
149: if(strpos($className, 'Object') !== false) {
150: if(self::loadFile($config->get('app.root').'alpha/model/', $className)) {
151: return;
152: }else{
153: array_push($skipDirs, $config->get('app.root').'alpha/model/');
154: }
155: }
156:
157: if(strpos($className, 'View') !== false) {
158: if(self::loadFile($config->get('app.root').'alpha/view/', $className)) {
159: return;
160: }else{
161: array_push($skipDirs, $config->get('app.root').'alpha/view/');
162: }
163: }
164:
165: if(strpos($className, 'Utils') !== false) {
166: if(self::loadFile($config->get('app.root').'alpha/util/', $className)) {
167: return;
168: }else{
169: array_push($skipDirs, $config->get('app.root').'alpha/util/');
170: }
171: }
172:
173:
174: foreach (self::$scannedDirs as $dir) {
175: if(!in_array($dir, $skipDirs)) {
176: $loaded = self::loadFile($dir, $className);
177:
178: if($loaded) {
179: if(isset(self::$logger))
180: self::$logger->debug('Had to do a full scan to find the class ['.$className.'] in the dir ['.$dir.']');
181:
182: return;
183: }
184: }
185: }
186:
187: if(isset(self::$logger))
188: self::$logger->fatal('Unable to load the required class ['.$className.']');
189: }
190:
191: 192: 193: 194: 195: 196: 197:
198: public static function loadLib($fileName) {
199: global $config;
200:
201:
202: $iterator = new RecursiveDirectoryIterator($config->get('app.root').'alpha/lib/');
203:
204: foreach (new RecursiveIteratorIterator($iterator) as $name => $file) {
205: if($file->getBaseName() == $fileName) {
206: if(isset(self::$logger))
207: self::$logger->debug('Found the library file ['.$fileName.'] in the location ['.$name.']');
208:
209: require_once $name;
210: return;
211: }
212: }
213:
214:
215: $iterator = new RecursiveDirectoryIterator($config->get('app.root').'lib/');
216:
217: foreach (new RecursiveIteratorIterator($iterator) as $name => $file) {
218: if($file->getBaseName() == $fileName) {
219: if(isset(self::$logger))
220: self::$logger->debug('Found the library file ['.$fileName.'] in the location ['.$name.']');
221:
222: require_once $name;
223: return;
224: }
225: }
226:
227: throw new LibraryNotInstalledException('Cannot find the library file ['.$fileName.'] in the lib dir!');
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237:
238: private static function loadFile($dir, $className) {
239: global $config;
240:
241:
242: $path = $dir.$className.'.inc';
243: if (file_exists($path) && is_readable($path)) {
244: if(isset(self::$logger))
245: self::$logger->debug('Loaded the class ['.$className.'] in the location ['.$path.']');
246: include_once $path;
247: return true;
248: }
249:
250: $path = $dir.$className.'.php';
251: if (file_exists($path) && is_readable($path)) {
252: if(isset(self::$logger))
253: self::$logger->debug('Loaded the class ['.$className.'] in the location ['.$path.']');
254: include_once $path;
255: return true;
256: }
257:
258: return false;
259: }
260: }
261:
262: spl_autoload_register('AlphaAutoLoader::loadClass');
263:
264: ?>
265: