|
1
|
|
|
<?php |
|
2
|
|
|
namespace Subreality\Dilmun\Nabu; |
|
3
|
|
|
|
|
4
|
|
|
use Subreality\Dilmun\Nabu\LoggerHandler\File; |
|
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
|
|
|
protected $logger; |
|
15
|
|
|
|
|
16
|
|
|
protected $packages = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Automatically initializes logging using a File handler |
|
20
|
|
|
*/ |
|
21
|
3 |
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
3 |
|
$logger_handler = new File("autoloader.log", DILMUN_LOGS_DIR); |
|
24
|
3 |
|
$logger_handler->initialize(); |
|
25
|
|
|
|
|
26
|
3 |
|
$this->logger = new Logger($logger_handler); |
|
27
|
3 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Registers the includeClass function as an autoloader. |
|
31
|
|
|
* |
|
32
|
|
|
* @see Autoloader::includeClass |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function register() |
|
35
|
|
|
{ |
|
36
|
2 |
|
spl_autoload_register(array($this,"includeClass")); |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $package |
|
41
|
|
|
* @param $path |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function registerPackages($package, $path) |
|
44
|
|
|
{ |
|
45
|
5 |
|
$this->logger->info("Preparing {$package} package for registration."); |
|
46
|
5 |
|
$this->logger->debug("Parameters: package = {$package}, path = {$path}"); |
|
47
|
|
|
|
|
48
|
5 |
|
$package = trim($package, "\\"); |
|
49
|
5 |
|
$package = $package . "\\"; |
|
50
|
|
|
|
|
51
|
5 |
|
$path = rtrim($path, "/"); |
|
52
|
5 |
|
$path = $path . "/"; |
|
53
|
|
|
|
|
54
|
5 |
|
$this->logger->debug("Registering index {$package} with {$path}"); |
|
55
|
5 |
|
$this->packages[$package][] = $path; |
|
56
|
|
|
|
|
57
|
5 |
|
$this->logger->info("Registered index {$package} as {$path}"); |
|
58
|
5 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Includes the file corresponding with the class name to be autoloaded. |
|
62
|
|
|
* |
|
63
|
|
|
* @param callable $class class name as defined by PHP autoloading |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool|string The path to the class on success; otherwise false |
|
|
|
|
|
|
66
|
|
|
* otherwise. |
|
67
|
|
|
*/ |
|
68
|
3 |
|
protected function includeClass($class) |
|
69
|
|
|
{ |
|
70
|
3 |
|
$this->logger->info("Setting up {$class} class for inclusion."); |
|
71
|
3 |
|
$this->logger->debug("Parameter => class: {$class}"); |
|
72
|
|
|
|
|
73
|
|
|
//Find the vendor based on the first backslash |
|
74
|
3 |
|
$vendor_end = strpos($class, '\\'); |
|
75
|
3 |
|
$this->logger->debug("The vendor name in {$class} ends at position {$vendor_end}"); |
|
76
|
|
|
|
|
77
|
|
|
//Maybe we'll need this one day... |
|
78
|
|
|
//$vendor_name = substr($class, 0, $vendor_end); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
3 |
|
$package_found = false; |
|
|
|
|
|
|
81
|
3 |
|
$package_loaded = false; |
|
82
|
|
|
|
|
83
|
3 |
|
foreach ($this->packages as $package => $sources) { |
|
84
|
2 |
|
if (strpos($class, $package) !== false) { |
|
85
|
2 |
|
$package_found = true; |
|
86
|
2 |
|
$this->logger->debug("Found package {$package} in {$class}!"); |
|
87
|
|
|
|
|
88
|
2 |
|
$package_string_length = strlen($package); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
2 |
|
$class_path = substr($class, $package_string_length); |
|
91
|
2 |
|
$this->logger->debug("Pulled out class path {$class_path}"); |
|
92
|
|
|
|
|
93
|
2 |
|
$package_loaded = $this->loadPackage($package, $class_path); |
|
94
|
2 |
|
} |
|
95
|
3 |
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
if (!$package_found) { |
|
98
|
1 |
|
$this->logger->alert("No registered packages found for {$class}!"); |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
return $package_loaded; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Loads a package based on registered paths associated with the package. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $package_name The name of the package in Vendor\Package format |
|
108
|
|
|
* @param string $class_name The full, relative name of the class with the vendor and package names stripped |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool|string The path to the class on success; otherwise false |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
2 |
|
protected function loadPackage($package_name, $class_name) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
2 |
|
$this->logger->info("Working on loading class via file include."); |
|
115
|
2 |
|
$this->logger->debug("Parameters: package_name = {$package_name}, class_name = {$class_name}"); |
|
116
|
|
|
|
|
117
|
2 |
|
$path_loaded = false; |
|
118
|
|
|
|
|
119
|
2 |
|
if (isset($this->packages[$package_name])) { |
|
120
|
2 |
|
$this->logger->debug("Package index of {$package_name} is set."); |
|
121
|
|
|
|
|
122
|
2 |
|
foreach ($this->packages[$package_name] as $path) { |
|
123
|
2 |
|
$converted_class = str_replace('\\', '/', $class_name); |
|
124
|
2 |
|
$class_path = $path . $converted_class . ".php"; |
|
|
|
|
|
|
125
|
2 |
|
$this->logger->debug("Set up class path as {$class_path}"); |
|
126
|
|
|
|
|
127
|
2 |
|
if (file_exists($class_path)) { |
|
128
|
1 |
|
$this->logger->debug("File {$class_path} exists"); |
|
129
|
|
|
|
|
130
|
1 |
|
$this->logger->debug("Including " . $class_path); |
|
131
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
132
|
1 |
|
include $class_path; |
|
133
|
|
|
|
|
134
|
1 |
|
$path_loaded = $class_path; |
|
135
|
|
|
|
|
136
|
1 |
|
$this->logger->info("Completed loading {$path_loaded}"); |
|
137
|
1 |
|
} else { |
|
138
|
2 |
|
$this->logger->debug("File {$class_path} does not exist given {$package_name} package"); |
|
139
|
|
|
|
|
140
|
2 |
|
$path_loaded = false; |
|
141
|
|
|
} |
|
142
|
2 |
|
} |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
2 |
|
$this->logger->debug("loadPackage is returning {$path_loaded}"); |
|
146
|
2 |
|
return $path_loaded; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Allows overriding the default file logger created at construction |
|
151
|
|
|
* |
|
152
|
|
|
* @param Logger $logger |
|
153
|
|
|
*/ |
|
154
|
1 |
|
public function setLogger(Logger $logger) |
|
155
|
|
|
{ |
|
156
|
1 |
|
$this->logger = $logger; |
|
157
|
1 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Gets the Logger set for the Autoloader |
|
161
|
|
|
* |
|
162
|
|
|
* @return Logger |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public function getLogger() |
|
165
|
|
|
{ |
|
166
|
1 |
|
return $this->logger; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.