Revision 2124
Added by darkviper over 9 years ago
WbAutoloader.php | ||
---|---|---|
1 | 1 |
<?php |
2 |
/*
|
|
2 |
/* |
|
3 | 3 |
* To change this template, choose Tools | Templates |
4 | 4 |
* and open the template in the editor. |
5 | 5 |
*/ |
... | ... | |
7 | 7 |
class WbAutoloader |
8 | 8 |
{ |
9 | 9 |
|
10 |
static private $_aSearchpatterns = array();
|
|
11 |
static private $_aReplacements = array();
|
|
12 |
|
|
10 |
static private $aSearchpatterns = array();
|
|
11 |
static private $aReplacements = array();
|
|
12 |
static private $aAbbreviations = array(); |
|
13 | 13 |
/** |
14 | 14 |
* Register WB - CoreAutoloader as SPL autoloader |
15 |
* @param array $aDirectories list of 'directory'=>'shortKey'
|
|
15 |
* @param array $aAbbreviations list of 'directory'=>'shortKey'
|
|
16 | 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 |
} |
|
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 |
} |
|
34 | 32 |
/** |
35 | 33 |
* tries autoloading the given class |
36 | 34 |
* @param string $sClassName |
37 | 35 |
*/ |
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 |
} |
|
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 |
} |
|
50 | 59 |
} // end class Autoloader |
Also available in: Unified diff
! /framework/initialize +
! /framework/WbAutoloader abbreviation list moved to initialize; added method getAbbreviations()