forked from Level-2/Axel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaxel.php
51 lines (45 loc) · 1.58 KB
/
axel.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
<?php
namespace Axel;
class Axel {
private $paths = [];
private $cache;
private $saveCache = false;
private $modules = [];
private $cacheIndex;
public function __construct(\ArrayAccess $cache = null, $cacheIndex = 'axelpaths') {
$this->cache = $cache;
$this->cacheIndex = $cacheIndex;
spl_autoload_register([$this, 'load']);
$this->paths = ($this->cache && $this->cache[$this->cacheIndex] !== null) ? $this->cache[$this->cacheIndex] : ['autoload\module' => __DIR__ . '/module.php', 'axel\module\psr0' => __DIR__ .'/module/PSR0.php'];
$this->addModule(new Module\PSR0(ltrim(str_replace(getcwd(), '', __DIR__), DIRECTORY_SEPARATOR) . '/module', 'Axel\\Module'));
}
public function load($className) {
$className = trim($className, '\\');
$classNameLc = strtolower($className);
if (isset($this->paths[$classNameLc])) {
if (file_exists($this->paths[$classNameLc])) require_once $this->paths[$classNameLc];
else {
$this->saveCache = true;
unset($this->paths[$classNameLc]);
//Something changed since the last run, clear the path for the file and try to load it again.
$this->load($className);
}
}
else {
foreach ($this->modules as $module) {
if ($file = $module->locate($className)) {
$this->paths[$classNameLc] = $file;
$this->saveCache = true;
require_once $this->paths[$classNameLc];
break;
}
}
}
}
public function addModule(\Autoload\Module $module) {
$this->modules[] = $module;
}
public function __destruct() {
if ($this->cache !== null && $this->saveCache) $this->cache[$this->cacheIndex] = $this->paths;
}
}