-
Notifications
You must be signed in to change notification settings - Fork 5
/
axel.php
59 lines (50 loc) · 1.61 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
52
53
54
55
56
57
58
59
<?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;
$this->paths = $this->cache[$this->cacheIndex] ?? ['axel\module' => __DIR__ . '/module.php', 'axel\module\psr4' => __DIR__ .'/module/PSR4.php'];
$this->register();
$this->modules[] = new Module\PSR4(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];
return $file;
}
}
}
}
public function addModule(\Axel\Module $module) {
$axel = clone $this;
$axel->modules[] = $module;
return $axel;
}
public function register() {
spl_autoload_register([$this, 'load']);
}
public function __destruct() {
if ($this->cache !== null && $this->saveCache) $this->cache[$this->cacheIndex] = $this->paths;
}
}