AutoLoad.php
Code (php)
1
2
3
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
2
3
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
Class AutoLoad {
private static $oInstance;
public static $aFolders = array ( 'Bin/',
'Application/',
'Library/' );
private static $aFiles = array( );
private static $sCacheFile = 'Cache/AutoLoad.cache.php';
/**
* Constructor
* Registers the autoloader to this class
*
* @acces private
*
* @returns void
**/
private function __construct() {
spl_autoload_register(__CLASS__ .'::load');
}
/**
* initialise
* Singleton pattern, makes sure class can be loaded once.
*
* @acces public static
*
* @returns object
**/
public static function initialise() {
if (is_null(self::$oInstance)) {
$c = __CLASS__;
self::$oInstance = new $c;
}
return self::$oInstance;
}
/**
* load
* function requires the needed file for using the class
*
* @acces private
*
* @returns void
**/
public static function load($sClassName) {
if(file_exists(self::$sCacheFile)) {
$aCache = unserialize(file_get_contents( self::$sCacheFile ));
if(file_exists($aCache[$sClassName].$sClassName.'.php')) {
require_once($aCache[$sClassName].$sClassName.'.php');
} else {
self::iterator();
self::writeCache();
$aCache = unserialize(file_get_contents( self::$sCacheFile ));
if(file_exists($aCache[$sClassName].$sClassName.'.php')) {
require_once($aCache[$sClassName].$sClassName.'.php');
} else {
trigger_error('Class '.$sClassName.' doesn\'t exists');
}
}
}
}
/**
* writeCache
* writes indexed files to cachefile
*
* @acces private
*
* @returns void
**/
private static function writeCache() {
$sContents = serialize(self::$aFiles);
$oHandle = fopen(self::$sCacheFile, 'w');
fwrite($oHandle, $sContents);
fclose($oHandle);
}
/**
* iterator
* indexes all files in folders used by framework
*
* @acces private
*
* @returns void
**/
private static function iterator() {
foreach(self::$aFolders as $sFolder) {
$aFramework = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sFolder), RecursiveIteratorIterator::SELF_FIRST);
foreach($aFramework as $sFrameworkFile) {
if(is_file($sFrameworkFile)) {
$aFrameworkFile = explode('/', $sFrameworkFile);
$sFileName = end($aFrameworkFile);
$aFileName = explode('.', $sFileName);
unset($aFileName[count($aFileName) - 1]);
$sFileName = implode('.', $aFileName);
unset($aFrameworkFile[count($aFrameworkFile) - 1]);
$sFilePath = implode('/', $aFrameworkFile);
self::$aFiles[$sFileName] = $sFilePath.'/';
}
}
}
}
/**
* clone
* throws exception
*
* @acces public
*
* @returns void
**/
public function __clone() {
}
/**
* destruct
* throws exception
*
* @acces public
*
* @returns void
**/
public function __destruct() {
}
}
?>
Class AutoLoad {
private static $oInstance;
public static $aFolders = array ( 'Bin/',
'Application/',
'Library/' );
private static $aFiles = array( );
private static $sCacheFile = 'Cache/AutoLoad.cache.php';
/**
* Constructor
* Registers the autoloader to this class
*
* @acces private
*
* @returns void
**/
private function __construct() {
spl_autoload_register(__CLASS__ .'::load');
}
/**
* initialise
* Singleton pattern, makes sure class can be loaded once.
*
* @acces public static
*
* @returns object
**/
public static function initialise() {
if (is_null(self::$oInstance)) {
$c = __CLASS__;
self::$oInstance = new $c;
}
return self::$oInstance;
}
/**
* load
* function requires the needed file for using the class
*
* @acces private
*
* @returns void
**/
public static function load($sClassName) {
if(file_exists(self::$sCacheFile)) {
$aCache = unserialize(file_get_contents( self::$sCacheFile ));
if(file_exists($aCache[$sClassName].$sClassName.'.php')) {
require_once($aCache[$sClassName].$sClassName.'.php');
} else {
self::iterator();
self::writeCache();
$aCache = unserialize(file_get_contents( self::$sCacheFile ));
if(file_exists($aCache[$sClassName].$sClassName.'.php')) {
require_once($aCache[$sClassName].$sClassName.'.php');
} else {
trigger_error('Class '.$sClassName.' doesn\'t exists');
}
}
}
}
/**
* writeCache
* writes indexed files to cachefile
*
* @acces private
*
* @returns void
**/
private static function writeCache() {
$sContents = serialize(self::$aFiles);
$oHandle = fopen(self::$sCacheFile, 'w');
fwrite($oHandle, $sContents);
fclose($oHandle);
}
/**
* iterator
* indexes all files in folders used by framework
*
* @acces private
*
* @returns void
**/
private static function iterator() {
foreach(self::$aFolders as $sFolder) {
$aFramework = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sFolder), RecursiveIteratorIterator::SELF_FIRST);
foreach($aFramework as $sFrameworkFile) {
if(is_file($sFrameworkFile)) {
$aFrameworkFile = explode('/', $sFrameworkFile);
$sFileName = end($aFrameworkFile);
$aFileName = explode('.', $sFileName);
unset($aFileName[count($aFileName) - 1]);
$sFileName = implode('.', $aFileName);
unset($aFrameworkFile[count($aFrameworkFile) - 1]);
$sFilePath = implode('/', $aFrameworkFile);
self::$aFiles[$sFileName] = $sFilePath.'/';
}
}
}
}
/**
* clone
* throws exception
*
* @acces public
*
* @returns void
**/
public function __clone() {
}
/**
* destruct
* throws exception
*
* @acces public
*
* @returns void
**/
public function __destruct() {
}
}
?>