1
|
|
|
<?php |
2
|
|
|
namespace Subreality\Dilmun\Nabu; |
3
|
|
|
|
4
|
|
|
use Subreality\Dilmun\LoggedClassTrait; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Nabu was the god of writing and scribes and was the keeper of the Tablets of |
8
|
|
|
* Destiny, in which the fate of humankind was recorded. |
9
|
|
|
* |
10
|
|
|
* @author derek |
11
|
|
|
*/ |
12
|
|
|
class Autoloader |
13
|
|
|
{ |
14
|
|
|
use LoggedClassTrait; |
15
|
|
|
|
16
|
|
|
protected $packages = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Registers the includeClass function as an autoloader. |
20
|
|
|
* |
21
|
|
|
* @see Autoloader::includeClass |
22
|
|
|
*/ |
23
|
2 |
|
public function register() |
24
|
|
|
{ |
25
|
2 |
|
spl_autoload_register(array($this,"includeClass")); |
26
|
2 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $package |
30
|
|
|
* @param $path |
31
|
|
|
*/ |
32
|
6 |
|
public function registerPackages($package, $path) |
33
|
|
|
{ |
34
|
6 |
|
$this->updateLog("info", "Preparing {$package} package for registration."); |
35
|
6 |
|
$this->updateLog("debug", "Parameters: package = {$package}, path = {$path}"); |
36
|
|
|
|
37
|
6 |
|
$package = trim($package, "\\"); |
38
|
6 |
|
$package = $package . "\\"; |
39
|
|
|
|
40
|
6 |
|
$path = rtrim($path, "/"); |
41
|
6 |
|
$path = $path . "/"; |
42
|
|
|
|
43
|
6 |
|
$this->updateLog("debug", "Registering index {$package} with {$path}"); |
44
|
6 |
|
$this->packages[$package][] = $path; |
45
|
|
|
|
46
|
6 |
|
$this->updateLog("info", "Registered index {$package} as {$path}"); |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Includes the file corresponding with the class name to be autoloaded. |
51
|
|
|
* |
52
|
|
|
* @param callable $class class name as defined by PHP autoloading |
53
|
|
|
* |
54
|
|
|
* @return string|false The path to the class on success; otherwise false |
55
|
|
|
*/ |
56
|
3 |
|
protected function includeClass($class) |
57
|
|
|
{ |
58
|
3 |
|
$this->updateLog("info", "Setting up {$class} class for inclusion."); |
59
|
|
|
|
60
|
3 |
|
$package_found = false; |
61
|
3 |
|
$package_loaded = false; |
62
|
|
|
|
63
|
3 |
|
foreach ($this->packages as $package => $sources) { |
64
|
2 |
|
if (strpos($class, $package) !== false) { |
65
|
2 |
|
$package_found = true; |
66
|
|
|
|
67
|
2 |
|
$package_length = strlen($package); |
68
|
|
|
|
69
|
2 |
|
$class_path = substr($class, $package_length); |
70
|
|
|
|
71
|
2 |
|
$this->updateLog("debug", "Found package {$package} and class path {$class_path} in class {$class}"); |
72
|
|
|
|
73
|
2 |
|
$package_loaded = $this->loadPackage($package, $class_path); |
74
|
2 |
|
} |
75
|
3 |
|
} |
76
|
|
|
|
77
|
3 |
|
if (!$package_found) { |
78
|
1 |
|
$this->updateLog("alert", "No registered packages found for {$class}!"); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
3 |
|
return $package_loaded; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Loads a package based on registered paths associated with the package. |
86
|
|
|
* |
87
|
|
|
* @param string $package_name The name of the package in Vendor\Package format |
88
|
|
|
* @param string $class_name The full, relative name of the class with the vendor and package names stripped |
89
|
|
|
* |
90
|
|
|
* @return false|string The path to the class on success; otherwise false |
91
|
|
|
*/ |
92
|
2 |
|
protected function loadPackage($package_name, $class_name) |
93
|
|
|
{ |
94
|
2 |
|
$this->updateLog("info", "Working on loading {$class_name} from {$package_name}"); |
95
|
|
|
|
96
|
2 |
|
$class_path = false; |
97
|
|
|
|
98
|
2 |
|
if (isset($this->packages[$package_name])) { |
99
|
2 |
|
$this->updateLog("debug", "Package index of {$package_name} is set."); |
100
|
|
|
|
101
|
2 |
|
foreach ($this->packages[$package_name] as $path) { |
102
|
2 |
|
$converted_class = str_replace('\\', '/', $class_name); |
103
|
2 |
|
$class_path = $path . $converted_class . ".php"; |
104
|
|
|
|
105
|
2 |
|
$this->updateLog("debug", "Class file path is {$class_path}"); |
106
|
|
|
|
107
|
2 |
|
if (file_exists($class_path)) { |
108
|
1 |
|
$this->updateLog("debug", "File {$class_path} exists; including file"); |
109
|
|
|
|
110
|
|
|
/** @noinspection PhpIncludeInspection */ |
111
|
1 |
|
include $class_path; |
112
|
|
|
|
113
|
1 |
|
$this->updateLog("info", "Completed loading {$class_path}"); |
114
|
1 |
|
} else { |
115
|
2 |
|
$this->updateLog("debug", "File {$class_path} does not exist"); |
116
|
|
|
|
117
|
2 |
|
$class_path = false; |
118
|
|
|
} |
119
|
2 |
|
} |
120
|
2 |
|
} |
121
|
|
|
|
122
|
2 |
|
return $class_path; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|