Project

General

Profile

1
<?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
	static public function doRegister(array $aDirectories)
18
	{
19
		if(!sizeof(self::$_aSearchpatterns))
20
		{
21
			if(sizeof($aDirectories > 0))
22
			{
23
				self::$_aSearchpatterns[] = '/(^.[^_].*$)/i';
24
				self::$_aReplacements[] = basename(dirname(__FILE__)).'_\1';
25
				foreach($aDirectories as $value => $shortKey)
26
				{
27
					self::$_aSearchpatterns[] = '/^'.$shortKey.'_/i';
28
					self::$_aReplacements[] = $value.'_';
29
				}
30
			}
31
		}
32
		spl_autoload_register(array(new self, 'CoreAutoloader'));
33
	}
34
/**
35
 * tries autoloading the given class
36
 * @param  string $sClassName
37
 */
38
	static public function CoreAutoloader($sClassName)
39
	{
40
		$sClassName = preg_replace(self::$_aSearchpatterns, self::$_aReplacements, $sClassName);
41
		$sFileName = dirname(dirname(__FILE__)).'/'.str_replace('_', '/', $sClassName).'.php';
42
		if (!is_file($sFileName = dirname(dirname(__FILE__)).'/'.str_replace('_', '/', $sClassName).'.php')) {
43
		// alternatively search for file with prefix 'class.'
44
			if (!is_file(($sFileName = dirname($sFileName).'/class.'.basename($sFileName)))) {
45
				return false;
46
			}
47
		}
48
		include($sFileName);
49
	}
50
} // end class Autoloader
(18-18/37)