-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresource.php
More file actions
243 lines (194 loc) · 6.88 KB
/
resource.php
File metadata and controls
243 lines (194 loc) · 6.88 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
* This software is the property of its authors.
* See the copyright.txt file for more details.
*
*/
function resource () {
$arguments = func_get_args();
$container = resourceContainer::instance();
if (count($arguments) == 0)
return $container;
if (count($arguments) == 1)
return $container[$arguments[0]];
return call_user_func_array($container[$arguments[0]], array_slice($arguments, 1));
}
class resourceContainer implements ArrayAccess {
static $_instance = null;
static function instance () {
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
protected $paths = array();
function registerPath ($path) {
$this->paths[] = $path;
}
// Store resource searches to prevent multiple searches of non existing resources.
protected $searches = [];
// Used by autoloader to infer resource source for cache purposes.
protected $newInitializators = [];
protected $initializators = array();
protected $resources = array();
function exist ($id) {
return $this->offsetExists($id);
}
function offsetExists ($id) {
$this->autoload($id);
return $this->isRegistered($id);
}
function offsetGet ($id) {
$this->autoload($id);
if ($this->isInitialized($id)) {
if (!self::isArray($id))
return $this->resources[$id];
$result = array();
foreach ($this->resources[$id] as $resource)
$result[] = $resource;
return $result;
}
if ($this->isRegistered($id)) {
$initialize = function ($id, $initializator) {
if (resourceContainer::isFunction($id))
return new resourceInvokable($initializator);
else if (is_callable($initializator))
return call_user_func($initializator);
else
return $initializator;
};
if (self::isArray($id)) {
$this->resources[$id] = array();
foreach ($this->initializators[$id] as $initializator)
$this->resources[$id][] = $initialize($id, $initializator);
} else {
$this->resources[$id] = $initialize($id, $this->initializators[$id]);
}
return $this->resources[$id];
}
assertTrue(false, "Resource *$id* not found.");
}
function offsetSet ($id, $value) {
$this->newInitializators[] = $id;
version_assert and assertTrue(!$this->isInitialized($id), "Resource *$id* already initialized.");
if (self::isArray($id)) {
if (!array_key_exists($id, $this->initializators))
$this->initializators[$id] = array();
$this->initializators[$id][] = $value;
} else {
$this->initializators[$id] = $value;
}
}
function offsetUnset ($id) {
assertTrue(false, "Unsetting resource *$id* is prohibited.");
}
function isInitialized ($id) {
return array_key_exists($id, $this->resources);
}
function isRegistered ($id) {
return array_key_exists($id, $this->initializators);
}
function isFullyRegistered ($id) {
if ($this->isInitialized($id))
return true;
// Array registration can be spread across multiple files so we have to assume that
// there might be some parts missing.
if ($this->isRegistered($id) && !self::isArray($id))
return true;
// In case there are no code changes (non-development environment) we assume that autoloader
// found and loaded the resource fully.
if (!version_development && $this->isRegistered($id))
return true;
return false;
}
/**
* Indicates whether the resource has already been searched for meaning there is no need to search for
* it or any missing part again. If true it implies that all resource parts are known and that resource
* is ready for initialization.
*/
function isSearched ($id) {
if (array_key_exists($id, $this->searches))
return true;
if ($this->isFullyRegistered($id))
return true;
// In case there are no code changes (non-development environment) we assume that autoloader
// searched through the entire code base. Even is the resource is not registered (because it does not exist)
// cache existence proves that resources has been searched for.
if (!version_development && apc_exists(self::cacheKey($id)))
return true;
return false;
}
function autoload ($id) {
if ($this->isFullyRegistered($id))
return;
if (apc_exists(self::cacheKey($id))) {
foreach (apc_fetch(self::cacheKey($id)) as $file)
if (is_file($file))
include_once $file;
if ($this->isFullyRegistered($id))
return;
}
// Make sure to search for a resource only once, even if it does not exist.
if ($this->isSearched($id))
return;
$this->searches[$id] = true;
// It is possible that resource is registered before autoload kicks in, in such cases
// we have to ensure that cache entry exists.
if (!apc_exists(self::cacheKey($id)))
apc_store(self::cacheKey($id), []);
foreach ($this->paths as $path)
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)) as $file) {
if (!in_array($file->getExtension(), array('php')))
continue;
$fileContent = file_get_contents($file);
if (preg_match('/(?si)resource\(/', $fileContent) == 0)
continue;
$this->newInitializators = [];
include_once $file;
$newInitializators = $this->newInitializators;
foreach ($newInitializators as $newInitializator) {
$initializatorFiles = array();
if (apc_exists(self::cacheKey($newInitializator)))
$initializatorFiles = apc_fetch(self::cacheKey($newInitializator));
$initializatorFiles[] = $file->getRealPath();
apc_store(self::cacheKey($newInitializator), $initializatorFiles);
}
// Array registration can be spread across multiple files so we have to scan
// everything in order to be sure it's fully registered.
if ($this->isRegistered($id) && !self::isArray($id))
return;
}
}
static function unmangledId ($id) {
$unmangledId = $id;
if (substr($unmangledId, -2) == '()')
$unmangledId = substr($unmangledId, 0, -2);
if (substr($unmangledId, -2) == '[]')
$unmangledId = substr($unmangledId, 0, -2);
return $unmangledId;
}
static function isFunction ($id) {
return substr($id, -2) == '()';
}
static function isArray ($id) {
return substr($id, -2) == '[]' || substr($id, -4) == '[]()';
}
static function cacheKey ($id) {
return 'phops_UqcV7Ql9MoDwpUlhwT08LsoM_resource_' . $id;
}
}
class resourceInvokable {
protected $invokable;
function __construct ($invokable) {
$this->invokable = $invokable;
}
function __invoke () {
return call_user_func_array($this->invokable, func_get_args());
}
function invoke () {
return call_user_func_array($this->invokable, func_get_args());
}
function toFunction () {
return $this->invokable;
}
}