-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoLoad.php
181 lines (160 loc) · 5.61 KB
/
CoLoad.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/*/
author: Oliver Anan <[email protected]>
package: CoLoad
license: lgpl 3 <http://www.gnu.org/licenses/lgpl-3.0.en.html
tags: [psr-0, pear2, autoloader]
type: class
version: 0.2.1.0
================================================================================
class daliaIT\coload\CoLoad
================================================================================
PSR-0 compliant hybrid autoloader.
All namesace seperators and underscores are replaced with directory seperators.
This way PSR-0 and PEAR2 naming style is supported.
CoLoad uses a map to determinate which file to include.
If an map entry is wrong, or no map exists, CoLoad will look for a matching file all source
directories.
At the end of the script execution the map file is updated, or created.
By default the loader will search for files ending with '.php' and '.class.php'
but you can change this behaviour by editing the property 'extensions'.
To enable the CoLoad autoloader create and instance and call the method
'register'.
Exammples
--------------------------------------------------------------------------------
### Edit Extensions
$myLoader = new CoLoad('myMapFile.json');
$myLodder->extensins[] = '.def.php';
$mLoader->extensions = array('.php');
### Enable Autloader
$myLoader = new CoLoad('myMapFile.json');
$myLoader
->addSource('relative/path/to/src/dir')
->addSource('/absolute/path/to/other/src/dir)
->register();
Source
--------------------------------------------------------------------------------
/*/
namespace daliaIT\CoLoad;
use InvalidArgumentException;
class CoLoad
{
public
#>array
$map = array(),
$extensions = array('.php','.class.php');
#<
protected
#:array
$sources = array(),
#:string
$mapFile;
private
#:bool
$saveOnShutdownSet;
#:this
public function __construct(
$mapFile = null,
$sources=array(),
$autoRegister=false
){
$this->mapFile = $mapFile;
$this->sources = $sources;
if($mapFile && is_readable($mapFile)){
$this->loadMap();
}
if($autoRegister){
$this->register();
}
}
#:this
public function register(){
$loader = $this;
spl_autoload_register( function($name) use ($loader){
return $loader->loadSourceCode($name);
});
return $this;
}
#:this
public function addSource($src){
if(array_search($src, $this->sources) === false){
$this->sources[] = $src;
}
return $this;
}
#:array
public function getSources(){
return $this->sources;
}
#:this
public function loadMap(){
if(!is_readable($this->mapFile)){
throw new InvalidArgumentException(
"Can not read file '{$this->mapFile}'"
);
}
$this->map = json_decode(
file_get_contents($this->mapFile),
true
);
return $this;
}
#:this
public function saveMap(){
$mapDir = dirname($this->mapFile);
if(! file_exists($mapDir) ){
mkdir($mapDir, 0777, true);
}
file_put_contents(
$this->mapFile,
json_encode($this->map)
);
return $this;
}
#:bool
public function loadSourceCode($name){
if(isset($this->map[$name]) && is_readable($this->map[$name])){
require $this->map[$name];
} else {
$path = $this->search($name);
if($path && is_readable($path)){
$this->map[$name] = $path;
require $path;
if(!$this->saveOnShutdownSet){
$this->saveOnShutdownSet = true;
$loader = $this;
register_shutdown_function(
function() use ($loader){
$loader->saveMap();
}
);
}
return true;
} else {
return false;
}
}
}
#:string|null
public function search($name){
$normalized = str_replace(
array('\\','_'),
DIRECTORY_SEPARATOR,
$name
);
foreach($this->sources as $source){
$pathWithoutExtension =
$source . DIRECTORY_SEPARATOR . $normalized;
$pathWithoutExtension = preg_replace(
'|/+|','/', $pathWithoutExtension
);
foreach($this->extensions as $extension){
$path = $pathWithoutExtension . $extension;
if(is_readable($path)){
return $path;
}
}
}
return null;
}
}