1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* An example of a general-purpose implementation that includes the optional |
5
|
|
|
* functionality of allowing multiple base directories for a single namespace |
6
|
|
|
* prefix. |
7
|
|
|
* |
8
|
|
|
* Given a foo-bar package of classes in the file system at the following |
9
|
|
|
* paths ... |
10
|
|
|
* |
11
|
|
|
* /path/to/packages/foo-bar/ |
12
|
|
|
* src/ |
13
|
|
|
* Baz.php # Foo\Bar\Baz |
14
|
|
|
* Qux/ |
15
|
|
|
* Quux.php # Foo\Bar\Qux\Quux |
16
|
|
|
* tests/ |
17
|
|
|
* BazTest.php # Foo\Bar\BazTest |
18
|
|
|
* Qux/ |
19
|
|
|
* QuuxTest.php # Foo\Bar\Qux\QuuxTest |
20
|
|
|
* |
21
|
|
|
* ... add the path to the class files for the \Foo\Bar\ namespace prefix |
22
|
|
|
* as follows: |
23
|
|
|
* |
24
|
|
|
* <?php |
25
|
|
|
* // instantiate the loader |
26
|
|
|
* $loader = new \Example\Psr4AutoloaderClass; |
27
|
|
|
* |
28
|
|
|
* // register the autoloader |
29
|
|
|
* $loader->register(); |
30
|
|
|
* |
31
|
|
|
* // register the base directories for the namespace prefix |
32
|
|
|
* $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src'); |
33
|
|
|
* $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests'); |
34
|
|
|
* |
35
|
|
|
* The following line would cause the autoloader to attempt to load the |
36
|
|
|
* \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php: |
37
|
|
|
* |
38
|
|
|
* <?php |
39
|
|
|
* new \Foo\Bar\Qux\Quux; |
40
|
|
|
* |
41
|
|
|
* The following line would cause the autoloader to attempt to load the |
42
|
|
|
* \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php: |
43
|
|
|
* |
44
|
|
|
* <?php |
45
|
|
|
* new \Foo\Bar\Qux\QuuxTest; |
46
|
|
|
*/ |
47
|
|
|
class Psr4AutoloaderClass |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* An associative array where the key is a namespace prefix and the value |
51
|
|
|
* is an array of base directories for classes in that namespace. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $prefixes = array(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register loader with SPL autoloader stack. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function register() |
63
|
|
|
{ |
64
|
|
|
spl_autoload_register(array($this, 'loadClass')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Adds a base directory for a namespace prefix. |
69
|
|
|
* |
70
|
|
|
* @param string $prefix The namespace prefix. |
71
|
|
|
* @param string $base_dir A base directory for class files in the |
72
|
|
|
* namespace. |
73
|
|
|
* @param bool $prepend If true, prepend the base directory to the stack |
74
|
|
|
* instead of appending it; this causes it to be searched first rather |
75
|
|
|
* than last. |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function addNamespace($prefix, $base_dir, $prepend = false) |
79
|
|
|
{ |
80
|
|
|
// normalize namespace prefix |
81
|
|
|
$prefix = trim($prefix, '\\') . '\\'; |
82
|
|
|
|
83
|
|
|
// normalize the base directory with a trailing separator |
84
|
|
|
$base_dir = rtrim($base_dir, '/') . DIRECTORY_SEPARATOR; |
85
|
|
|
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; |
86
|
|
|
|
87
|
|
|
// initialize the namespace prefix array |
88
|
|
|
if (isset($this->prefixes[$prefix]) === false) { |
89
|
|
|
$this->prefixes[$prefix] = array(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// retain the base directory for the namespace prefix |
93
|
|
|
if ($prepend) { |
94
|
|
|
array_unshift($this->prefixes[$prefix], $base_dir); |
95
|
|
|
} else { |
96
|
|
|
array_push($this->prefixes[$prefix], $base_dir); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Loads the class file for a given class name. |
102
|
|
|
* |
103
|
|
|
* @param string $class The fully-qualified class name. |
104
|
|
|
* @return string|false The mapped file name on success, or boolean false on |
105
|
|
|
* failure. |
106
|
|
|
*/ |
107
|
|
|
public function loadClass($class) |
108
|
|
|
{ |
109
|
|
|
// the current namespace prefix |
110
|
|
|
$prefix = $class; |
111
|
|
|
|
112
|
|
|
// work backwards through the namespace names of the fully-qualified |
113
|
|
|
// class name to find a mapped file name |
114
|
|
|
while (false !== $pos = strrpos($prefix, '\\')) { |
115
|
|
|
|
116
|
|
|
// retain the trailing namespace separator in the prefix |
117
|
|
|
$prefix = substr($class, 0, $pos + 1); |
118
|
|
|
|
119
|
|
|
// the rest is the relative class name |
120
|
|
|
$relative_class = substr($class, $pos + 1); |
121
|
|
|
|
122
|
|
|
// try to load a mapped file for the prefix and relative class |
123
|
|
|
$mapped_file = $this->loadMappedFile($prefix, $relative_class); |
124
|
|
|
if ($mapped_file) { |
125
|
|
|
return $mapped_file; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// remove the trailing namespace separator for the next iteration |
129
|
|
|
// of strrpos() |
130
|
|
|
$prefix = rtrim($prefix, '\\'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// never found a mapped file |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Load the mapped file for a namespace prefix and relative class. |
139
|
|
|
* |
140
|
|
|
* @param string $prefix The namespace prefix. |
141
|
|
|
* @param string $relative_class The relative class name. |
142
|
|
|
* @return false|string Boolean false if no mapped file can be loaded, or the |
143
|
|
|
* name of the mapped file that was loaded. |
144
|
|
|
*/ |
145
|
|
|
protected function loadMappedFile($prefix, $relative_class) |
146
|
|
|
{ |
147
|
|
|
// are there any base directories for this namespace prefix? |
148
|
|
|
if (isset($this->prefixes[$prefix]) === false) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// look through base directories for this namespace prefix |
153
|
|
|
foreach ($this->prefixes[$prefix] as $base_dir) { |
154
|
|
|
|
155
|
|
|
// replace the namespace prefix with the base directory, |
156
|
|
|
// replace namespace separators with directory separators |
157
|
|
|
// in the relative class name, append with .php |
158
|
|
|
$file = $base_dir |
159
|
|
|
. str_replace('\\', DIRECTORY_SEPARATOR, $relative_class) |
160
|
|
|
. '.php'; |
161
|
|
|
$file = $base_dir |
162
|
|
|
. str_replace('\\', '/', $relative_class) |
163
|
|
|
. '.php'; |
164
|
|
|
|
165
|
|
|
// if the mapped file exists, require it |
166
|
|
|
if ($this->requireFile($file)) { |
167
|
|
|
// yes, we're done |
168
|
|
|
return $file; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// never found it |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* If a file exists, require it from the file system. |
178
|
|
|
* |
179
|
|
|
* @param string $file The file to require. |
180
|
|
|
* @return bool True if the file exists, false if not. |
181
|
|
|
*/ |
182
|
|
|
protected function requireFile($file) |
183
|
|
|
{ |
184
|
|
|
if (file_exists($file)) { |
185
|
|
|
require $file; |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|