Skip to content

Commit 12192cd

Browse files
committed
Update Autoloader
1 parent 6e28573 commit 12192cd

1 file changed

Lines changed: 36 additions & 26 deletions

File tree

system/Autoloader/Autoloader.php

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
*/
7777
class Autoloader
7878
{
79-
8079
/**
8180
* Stores namespaces as key, and path as values.
8281
*
@@ -98,6 +97,8 @@ class Autoloader
9897
* the valid parts that we'll need.
9998
*
10099
* @param \Config\Autoload $config
100+
*
101+
* @return $this
101102
*/
102103
public function initialize(\Config\Autoload $config)
103104
{
@@ -110,25 +111,25 @@ public function initialize(\Config\Autoload $config)
110111

111112
if (isset($config->psr4))
112113
{
113-
$this->prefixes = $config->psr4;
114+
$this->addNamespace($config->psr4);
114115
}
115116

116117
if (isset($config->classmap))
117118
{
118119
$this->classmap = $config->classmap;
119120
}
120121

121-
$this->addNamespace(APP_NAMESPACE, APPPATH);
122+
//$this->addNamespace(APP_NAMESPACE, APPPATH);
122123

123124
unset($config);
125+
126+
return $this;
124127
}
125128

126129
//--------------------------------------------------------------------
127130

128131
/**
129132
* Register the loader with the SPL autoloader stack.
130-
*
131-
* @codeCoverageIgnore
132133
*/
133134
public function register()
134135
{
@@ -146,21 +147,21 @@ public function register()
146147
$config = is_array($this->classmap) ? $this->classmap : [];
147148

148149
spl_autoload_register(function ($class) use ($config) {
149-
if (! array_key_exists($class, $config))
150+
if (empty($config[$class]))
150151
{
151152
return false;
152153
}
153154

154155
include_once $config[$class];
155156
}, true, // Throw exception
156-
true // Prepend
157+
true // Prepend
157158
);
158159
}
159160

160161
//--------------------------------------------------------------------
161162

162163
/**
163-
* Registers a namespace with the autoloader.
164+
* Registers namespaces with the autoloader.
164165
*
165166
* @param array|string $namespace
166167
* @param string $path
@@ -169,16 +170,28 @@ public function register()
169170
*/
170171
public function addNamespace($namespace, string $path = null)
171172
{
172-
if (\is_array($namespace))
173+
if (is_array($namespace))
173174
{
174175
foreach ($namespace as $prefix => $path)
175176
{
176-
$this->prefixes[$prefix] = $path;
177+
$prefix = trim($prefix, '\\');
178+
179+
if (is_array($path))
180+
{
181+
foreach ($path as $dir)
182+
{
183+
$this->prefixes[$prefix][] = rtrim($dir, '/') . '/';
184+
}
185+
186+
continue;
187+
}
188+
189+
$this->prefixes[$prefix][] = rtrim($path, '/') . '/';
177190
}
178191
}
179192
else
180193
{
181-
$this->prefixes[$namespace] = $path;
194+
$this->prefixes[trim($namespace, '\\')][] = rtrim($path, '/') . '/';
182195
}
183196

184197
return $this;
@@ -187,9 +200,13 @@ public function addNamespace($namespace, string $path = null)
187200
//--------------------------------------------------------------------
188201

189202
/**
203+
* Get namespaces with prefixes as keys and paths as values.
204+
*
205+
* If a prefix param is set, returns only paths to the given prefix.
206+
*
190207
* @var string|null $prefix
191208
*
192-
* @return array|string
209+
* @return array
193210
*/
194211
public function getNamespace(string $prefix = null)
195212
{
@@ -198,7 +215,7 @@ public function getNamespace(string $prefix = null)
198215
return $this->prefixes;
199216
}
200217

201-
return $this->prefixes[$prefix];
218+
return $this->prefixes[trim($prefix, '\\')] ?? [];
202219
}
203220

204221
//--------------------------------------------------------------------
@@ -212,7 +229,7 @@ public function getNamespace(string $prefix = null)
212229
*/
213230
public function removeNamespace(string $namespace)
214231
{
215-
unset($this->prefixes[$namespace]);
232+
unset($this->prefixes[trim($namespace, '\\')]);
216233

217234
return $this;
218235
}
@@ -224,7 +241,7 @@ public function removeNamespace(string $namespace)
224241
*
225242
* @param string $class The fully qualified class name.
226243
*
227-
* @return mixed The mapped file on success, or boolean false
244+
* @return string|false The mapped file on success, or boolean false
228245
* on failure.
229246
*/
230247
public function loadClass(string $class)
@@ -251,7 +268,7 @@ public function loadClass(string $class)
251268
*
252269
* @param string $class The fully-qualified class name
253270
*
254-
* @return mixed The mapped file name on success, or boolean false on fail
271+
* @return string|false The mapped file name on success, or boolean false on fail
255272
*/
256273
protected function loadInNamespace(string $class)
257274
{
@@ -262,18 +279,14 @@ protected function loadInNamespace(string $class)
262279

263280
foreach ($this->prefixes as $namespace => $directories)
264281
{
265-
if (is_string($directories))
266-
{
267-
$directories = [$directories];
268-
}
269-
270282
foreach ($directories as $directory)
271283
{
272284
$directory = rtrim($directory, '/');
273285

274286
if (strpos($class, $namespace) === 0)
275287
{
276-
$filePath = $directory . str_replace('\\', '/', substr($class, strlen($namespace))) . '.php';
288+
$filePath = $directory . str_replace('\\', '/',
289+
substr($class, strlen($namespace))) . '.php';
277290
$filename = $this->requireFile($filePath);
278291

279292
if ($filename)
@@ -333,11 +346,9 @@ protected function loadLegacy(string $class)
333346
* A central way to require a file is loaded. Split out primarily
334347
* for testing purposes.
335348
*
336-
* @codeCoverageIgnore
337-
*
338349
* @param string $file
339350
*
340-
* @return boolean
351+
* @return string|false The filename on success, false if the file is not loaded
341352
*/
342353
protected function requireFile(string $file)
343354
{
@@ -382,6 +393,5 @@ public function sanitizeFilename(string $filename): string
382393

383394
return $filename;
384395
}
385-
386396
//--------------------------------------------------------------------
387397
}

0 commit comments

Comments
 (0)