-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
67 lines (58 loc) · 1.75 KB
/
bootstrap.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
<?php
/**
* @author Evgeniy Makhrov <emakhrov@gmail.com>
*/
class Bootstrap {
private
$ext,
$root,
$config,
$default_path;
public function __construct($root, $ext, $config, $default_path) {
$this->root = $root;
$this->ext = $ext;
$this->config= $config;
$this->default_path= $default_path;
}
public function buildPath($base, $parts, $filename) {
$pathname = $this->root . $base . ($parts ? '/' . implode('/', $parts) : '') . '/' . $filename . '.' . $this->ext;
return is_file($pathname) ? $pathname : false;
}
public function resolvePath($class) {
$parts = preg_split('#\\\\|_#', $class);
$filename = array_pop($parts);
if ($parts && isset($this->config[$parts[0]])) {
$ptr = $this->config;
foreach ($parts as $idx => $part) {
if (!isset($ptr[$part])) return false;
if (!is_string($ptr[$part])) $ptr = $ptr[$part];
else return $this->buildPath($ptr[$part], array_slice($parts, $idx + 1), $filename);
}
}
return $this->buildPath($this->default_path, $parts, $filename);
}
public function autoload($class) {
$path = $this->resolvePath($class);
if ($path) require_once $path;
}
public function register() {
spl_autoload_register([$this, 'autoload'], /* throw */true, /* prepend */true);
return $this;
}
public function getRoot() {
return $this->root;
}
}
return (new Bootstrap(
__DIR__,
'php',
[
'Gitphp' => '/classes',
'Ololo' => [
'Some' => '/classes/omg',
'Other' => '/classes/any',
],
],
''
))
->register();