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
    static private $aAbbreviations  = array();
13
/**
14
 * Register WB - CoreAutoloader as SPL autoloader
15
 * @param array $aAbbreviations list of 'directory'=>'shortKey'
16
 */
17
    static public function doRegister(array $aAbbreviations)
18
    {
19
        if (!sizeof(self::$aSearchpatterns)) {
20
            if (sizeof($aAbbreviations > 0)) {
21
                self::$aAbbreviations = $aAbbreviations;
22
                self::$aSearchpatterns[] = '/(^.[^_].*$)/i';
23
                self::$aReplacements[] = basename(__DIR__).'_\1';
24
                foreach ($aAbbreviations as $shortKey => $value) {
25
                    self::$aSearchpatterns[] = '/^'.$shortKey.'_/i';
26
                    self::$aReplacements[] = $value.'_';
27
                }
28
            }
29
        }
30
        spl_autoload_register(array(new self, 'CoreAutoloader'));
31
    }
32
/**
33
 * tries autoloading the given class
34
 * @param  string $sClassName
35
 */
36
    static public function CoreAutoloader($sClassName)
37
    {
38
        $sFileName = '';
39
        $sBaseDir = trim(str_replace('\\', '/', dirname(__DIR__)), '/').'/';
40
        $sClassName = preg_replace(self::$aSearchpatterns, self::$aReplacements, $sClassName);
41
        $sFileName = $sBaseDir.str_replace('_', '/', $sClassName);
42
        if (! is_readable($sFileName.'.php')) {
43
        // alternatively search for file with prefix 'class.'
44
            $sFileName = dirname($sFileName).'/class.'.basename($sFileName);
45
            if (! is_readable($sFileName.'.php')) {
46
                $sFileName = '';
47
            }
48
        }
49
        if ($sFileName) { include($sFileName.'.php'); }
50
    }
51
/**
52
 *
53
 * @return array list of abbreviations
54
 */
55
    static public function getAbbreviations()
56
    {
57
        return self::$aAbbreviations;
58
    }
59
} // end class Autoloader
(20-20/40)