Project

General

Profile

1 1686 darkviper
<?php
2
/*
3
 * To change this template, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
7
class WbAutoloader
8
{
9
10
	static private $_aSearchpatterns = array();
11
	static private $_aReplacements = array();
12
13
/**
14
 * Register WB - CoreAutoloader as SPL autoloader
15
 * @param array $aDirectories list of 'directory'=>'shortKey'
16
 */
17 1870 Luisehahne
	static public function doRegister(array $aDirectories)
18
	{
19
		if(!sizeof(self::$_aSearchpatterns)) {
20
			if(sizeof($aDirectories > 0)) {
21
				self::$_aSearchpatterns[] = '/(^.[^_].*$)/i';
22
				self::$_aReplacements[] = basename(dirname(__FILE__)).'_$1';
23
				foreach($aDirectories as $value => $shortKey) {
24
					self::$_aSearchpatterns[] = '/^'.$shortKey.'_/i';
25
					self::$_aReplacements[] = $value.'_';
26
				}
27 1686 darkviper
			}
28
		}
29 1870 Luisehahne
		spl_autoload_register(array(new self, 'CoreAutoloader'));
30
	}
31 1686 darkviper
/**
32
 * tries autoloading the given class
33
 * @param  string $sClassName
34
 */
35
	static public function CoreAutoloader($sClassName)
36
	{
37 1689 darkviper
		if($sClassName == 'database'){
38
			$sFileName = dirname(__FILE__).'/class.database.php';
39
			if(is_file($sFileName)) { include($sFileName); }
40
		}else {
41
			$sClassName = preg_replace(self::$_aSearchpatterns, self::$_aReplacements, $sClassName);
42
			$sFileName = dirname(dirname(__FILE__)).'/'.str_replace('_', '/', $sClassName).'.php';
43
			if(is_file($sFileName = dirname(dirname(__FILE__)).'/'.str_replace('_', '/', $sClassName).'.php')) {
44
				include($sFileName);
45
			}
46 1686 darkviper
		}
47
	}
48
} // end class Autoloader