-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_bundle.php
executable file
·406 lines (336 loc) · 9.32 KB
/
_bundle.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
namespace Bundles\Controller;
use Exception;
use stack;
use e;
class normalRoute extends Exception {}
/**
* Controller Bundle
* @author Nate Ferrero
*/
class Bundle {
private static $controllers = array();
public function _on_framework_loaded() {
/**
* Add LHTML Hook :controller(path/to/controller/file)
* @author Nate Ferrero
*/
e::configure('lhtml')->activeAddKey('hook', ':controller', function($controller) {
return e::controller($controller);
});
/**
* Add LHTML Hook :api
* @author Nate Ferrero
*/
e::configure('lhtml')->activeAddKey('hook', ':api', function() {
return e::portal('api')->controller->APIObject;
});
/**
* Add Portal Access Hook e::portal('api')->path->to->controller->method(arg1, arg2...);
* @author Nate Ferrero
*/
e::configure('portal')->activeAddKey('hook', 'controller', function($path, $class) {
return new ControllerAccessor($path . '/controllers', $class . '\\Controllers');
});
/**
* Configure Controller Bundle
* @author Kelly Becker
*/
e::configure('controller')->activeAdd('locations', e\site);
e::configure('autoload')->activeAddKey('special', 'Bundles\\Controller\\FormController', __DIR__ . '/library/form-controller.php');
e::configure('autoload')->activeAddKey('special', 'Bundles\\Controller\\ApiController', __DIR__ . '/library/api-controller.php');
e::configure('autoload')->activeAddKey('special', 'Bundles\\Controller\\ApiModel', __DIR__ . '/library/api-controller.php');
e::configure('autoload')->activeAddKey('special', 'Bundles\\Controller\\ApiList', __DIR__ . '/library/api-controller.php');
}
/**
* Allow direct access
*/
public function __callBundle($controller) {
$controller = '/portals/'.implode('/controllers/', explode('/', $controller, 2));
$class = str_replace('/', '\\', $controller);
e\VerifyClass($class);
return new $class;
}
/**
* Route through a portal
* @author Nate Ferrero
*/
public function _on_portal_route($path, $dir) {
/**
* Add portal directory
*/
e::configure('controller')->activeAdd('locations', $dir);
/**
* Route
*/
$this->route($path);
}
/**
* Route on main routing
* @author Nate Ferrero
*/
public function _on_router_route($path) {
$this->route($path);
}
/**
* Route or load a controller method
* @author Nate Ferrero
*/
public function route($path, $dirs = null, $tryRouter = true) {
// If dirs are not specified, use defaults
if(is_null($dirs))
$dirs = e::configure('controller')->locations;
// Get the controller name
$name = implode('/',$path);
/**
* Check all dirs for a matching controller
*/
if(is_array($dirs) || $dirs instanceof \Traversable) foreach($dirs as $dir) {
try {
/**
* Look in controllers folder
*/
if(basename($dir) !== 'controllers')
$dir .= '/controllers';
/**
* Skip if missing
*/
if(!is_dir($dir))
continue;
/**
* If router.php exists, use it!
* @author Nate Ferrero
* ********************
* If normalRoute gets thrown then
* route normally
* @author Kelly Becker
*/
if($tryRouter) {
try {
static $run = 0;
$file = "$dir/router.php";
if(is_file($file) && !$run) {
$run = 1;
$class = '\\Portals\\' . e::$portal->currentPortalName() . '\\Controllers\\Router';
require_once($file);
$router = new $class;
$router->route($path);
}
}
catch(normalRoute $n) {
$shift = (int) $n->getMessage();
if($shift) {
array_shift($path);
return $this->route($path, $dirs, false);
}
}
}
/**
* Find File
*/
$lname = $name;
$args = array();
$filea = explode('/', $lname);
$total = count($filea);
$i = 0;
while($i <= $total) {
/**
* Verify that name exists
*/
$last = count($filea) - 1;
if($last === -1 || $filea[$last] === '')
break;
/**
* File name
*/
$file = ($dir.'/'.($lname = implode('/', $filea)).'.php');
if(is_file($file))
break;
array_unshift($args, array_pop($filea));
$i++;
}
/**
* Skip if incorrect file
*/
if(!is_file($file))
continue;
$fname = basename($file);
/**
* Trace
*/
e\trace(__CLASS__, "Matched controller `$fname`");
/**
* Load controller if not already loaded
*/
if(!isset(self::$controllers[$file])) {
/**
* Define controller class
* @author Nate Ferrero
*/
$class = basename(dirname(dirname($dir))) . '\\' . basename(dirname($dir)) . '\\' . basename($dir) . '\\' . implode('\\', $filea);
/**
* Load File if not loaded already
*/
require_once($file);
/**
* Load controller
*/
e\VerifyClass($class);
self::$controllers[$file] = new $class;
/**
* Cache arguments
* @author Nate Ferrero
* @rationale Init Controller needs to get the same args as Init Controller Pattern
*/
$oargs = $args;
if(method_exists(self::$controllers[$file],'__initControllerPattern'))
$newArgs = call_user_func_array(array(self::$controllers[$file], '__initControllerPattern'), $oargs);
if(is_array($newArgs))
$args = $newArgs;
if(method_exists(self::$controllers[$file],'__initController'))
$newArgs = call_user_func_array(array(self::$controllers[$file], '__initController'), $oargs);
if(is_array($newArgs))
$args = $newArgs;
}
/**
* Get the method name
*/
$method = !empty($args) ? array_shift($args) : 'index';
/**
* Call the appropriate controller method with the remaining path elements as arguments
*/
if(method_exists(self::$controllers[$file], '__routeController')) {
$result = call_user_func_array(
array(self::$controllers[$file], '__routeController'),
array($method, $args)
);
}
else {
/**
* make sure that our controller method exists before attempting to call it
*/
if(!method_exists(self::$controllers[$file], $method) && !method_exists(self::$controllers[$file], '__call'))
throw new Exception("Controller `$lname` exists but the method `$method` is not defined in `$file`");
$result = call_user_func_array(
array(self::$controllers[$file], $method),
$args
);
}
/**
* If the controller has a shutdown methods, run them.
*/
if(method_exists(self::$controllers[$file],'__shutdownController'))
self::$controllers[$file]->__shutdownController();
if(method_exists(self::$controllers[$file],'__shutdownControllerPattern'))
self::$controllers[$file]->__shutdownControllerPattern();
/**
* Complete the page load
*/
e\complete($result);
}
catch(normalRoute $e) {
return;
}
}
}
}
/**
* Controller Accessor
* @author Nate Ferrero
*/
class ControllerAccessor {
private $segCount;
private $class;
private $path;
private $api;
private $_segCount;
private $_class;
private $_path;
private $_api;
/**
* Save the path and class
* @author Nate Ferrero
*/
public function __construct($path, $class) {
$this->api = false;
$this->path = $path;
$this->segCount = 0;
$this->class = $class;
}
/**
* Continue seeking controller paths
* @author Nate Ferrero
*/
public function __get($segment) {
$this->segCount = $this->segCount + 1;
if($segment == 'APIObject' && $this->segCount === 1) {
$this->api = true;
return $this;
}
$this->path .= '/' . $segment;
$this->class .= '\\' . $segment;
return $this;
}
/**
* Grab variables
* @author Nate Ferrero
*/
private function __snapshot() {
$this->_api = $this->api;
$this->_path = $this->path;
$this->_class = $this->class;
$this->_segCount = $this->segCount;
}
/**
* Restore data after a load
* @author Nate Ferrero
*/
private function __restore() {
$this->api = $this->_api;
$this->path = $this->_path;
$this->class = $this->_class;
$this->segCount = $this->_segCount;
}
/**
* Load the controller with a function call
* @author Nate Ferrero
*/
public function __call($method, $args) {
$this->__snapshot();
$this->$method;
if(!file_exists($this->path . '.php'))
throw new Exception("Controller `$this->path.php` does not exist");
e\VerifyClass($this->class);
$controller = new $this->class;
$this->__restore();
if(method_exists($controller,'__initControllerPattern'))
call_user_func_array(array($controller, '__initControllerPattern'), $args);
if(method_exists($controller,'__initController'))
call_user_func_array(array($controller, '__initController'), $args);
if(!$this->api) return $controller;
else return $controller->__phpAccess($args);
}
/**
* List all available controllers
* @author Nate Ferrero
*/
public function __list() {
$out = array();
foreach(glob($this->path . '/*.php') as $controller) {
$out[] = pathinfo($controller, PATHINFO_FILENAME);
}
return $out;
}
/**
* Return the an array of filename => class values
* @author Kelly Becker
*/
public function __listRecursive() {
$out = array();
foreach(e\glob_recursive($this->path . '/*.php') as $controller) {
$removePath = stripos($controller, str_replace('\\', '/', $this->class)) + 1;
$out[$controller] = str_replace('/', '\\', substr($controller, $removePath, -4));
}
return $out;
}
}