-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_bundle.php
More file actions
executable file
·395 lines (329 loc) · 9.1 KB
/
_bundle.php
File metadata and controls
executable file
·395 lines (329 loc) · 9.1 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
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
<?php
namespace Bundles\LHTML;
use Bundles\Router\NotFoundException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Exception;
use stack;
use e;
class Bundle {
public static $url_vars = array();
public function __getBundle() {
return new Instance;
}
public function __callBundle() {
return new Instance;
}
public function __initBundle() {
/**
* Add basic hooks
*/
e::configure('lhtml')->activeAddKey('hook', ':e', new e_handle);
/**
* Add LHTML hooks
*/
e::configure('lhtml')->activeAddKey('hook', ':slug', function() { return e::$lhtml->_get_special_vars(':slug'); });
e::configure('lhtml')->activeAddKey('hook', ':id', function() { return e::$lhtml->_get_special_vars(':id'); });
e::configure('lhtml')->activeAddKey('hook', ':urlVars', function() { return e::$lhtml->_get_special_vars(':urlVars'); } );
}
/**
* Get all routes for the sitemap
* @author Nate Ferrero
*/
public function _on_portal_sitemap($path, $dir) {
$dir .= '/lhtml';
$all = array();
try {
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
if(pathinfo($name, PATHINFO_EXTENSION) !== 'lhtml')
continue;
/**
* Remove the start dir
*/
$name = substr($name, strlen($dir));
/**
* Remove .lhtml from the end and return
*/
$all[substr($name, 0, strlen($name) - 6)] = ucfirst(pathinfo($name, PATHINFO_FILENAME));
}
} catch(Exception $e) {}
return $all;
}
public function _on_portal_route($path, $dir) {
$this->route($path, array($dir));
}
//public function _on_router_route($path) {
// $this->route($path, array(e\site));
//}
public function _on_portal_exception($path, $dir, $exception) {
$this->exception($path, array($dir), $exception);
}
public function _on_router_exception($path, $exception) {
$this->exception($path, array(e\site), $exception);
}
public function exception($path, $dirs, $exception) {
$search = 'special-' . ($exception instanceof NotFoundException ? 'notfound' : 'exception');
$this->route(array($search), $dirs);
}
public function route($path, $dirs = null) {
// If dirs are not specified, use defaults
if(is_null($dirs))
$dirs = e::configure('lhtml')->locations;
// Make sure path contains valid controller name
if(!isset($path[0]) || $path[0] == '')
$path = array('index');
// Get the lhtml name
$name = strtolower(implode('/', $path));
e\Trace(__CLASS__, "Looking for $name.lhtml");
// Check all dirs for a matching lhtml
foreach($dirs as $dir) {
// Look in lhtml folder
if(basename($dir) !== 'lhtml')
$dir .= '/lhtml';
// Skip if missing
if(!is_dir($dir))
continue;
$matched = false; $vars = array(); $nodir = false; $badmatch = false;
$p = 1;
foreach($path as $key => $segment) {
if($matched == 'file') $vars[] = $segment;
if((!$matched || $matched == 'dir') && is_dir("$dir/$segment")) {
$dir .= "/$segment";
$matched = 'dir';
}
elseif(is_file("$dir/$segment.lhtml")) {
$file = "$dir/$segment.lhtml";
$matched = 'file';
}
elseif($matched != 'file' && $matched != 'dir') {
$badmatch = true;
break;
}
}
if($matched == 'dir' && !$badmatch && is_file("$dir/index.lhtml")) {
$file = "$dir/index.lhtml";
$matched = 'index';
}
# no match at all, just continue
if($matched == false) continue;
# set the url vars to use
self::$url_vars = $vars;
/**
* Parse the LHTML file
* @author Nate Ferrero
*/
$start = microtime(true);
$out = e::$lhtml->file($file)->parse(null, true)->build();
$end = microtime(true);
$time = ($end - $start) * 1000;
// Show debug time if set
if(isset($_GET['--lhtml-time'])) {
// $file $time
eval(d);
}
/**
* Output the header
* @author Nate Ferrero
*/
header('Content-Type: ' . Instance::$contentType . '; charset=utf-8');
/**
* HACK
* Since double quotes aren't parsed correctly (and &doesnt; work in some cases)
* @author Nate Ferrero
* @todo Find a better solution for this!
*/
$out = str_replace(array('-#-'), array('"'), $out);
echo $out;
// Complete the page load
e\Complete();
}
}
/**
* Clean up te LHTML cache
* @author Nate Ferrero
*/
public function _on_e_command_update($dir) {
$dir .= '/lhtml';
foreach(glob("$dir/*") as $file)
if(!unlink($file)) return "Error: Unable to clear LHTML cache";
return "LHTML cache cleared";
}
}
class Instance {
/**
* @todo Clean up, possibly move to url bundle
*/
public function _get_special_vars($matcher) {
switch($matcher) {
case ':id' :
if(isset(Bundle::$url_vars[0]) && is_numeric(Bundle::$url_vars[0])) return Bundle::$url_vars[0];
break;
case ':slug':
if(isset(Bundle::$url_vars[0])) return Bundle::$url_vars[0];
break;
case ':urlVars':
if(isset(Bundle::$url_vars[0])) return Bundle::$url_vars;
break;
}
return null;
}
private $file = null;
private $string = null;
private $description = '{unknown}';
public $stack;
public static $contentType = 'text/html';
public function file($file) {
$this->file = $file;
$this->string = null;
$this->description = $file;
if($this->stack)
unset($this->stack);
return $this;
}
public function string($string, $description = '{string}', $timestamp = null) {
$this->string = $string;
$this->file = null;
$this->description = $description;
if($this->stack)
unset($this->stack);
return $this;
}
public function evaluate($condition, $data) {
$scope = new Scope;
if(is_array($data)) {
foreach($data as $key => $value) {
$scope->$key = $value;
}
}
return $scope->evaluate($condition);
}
public function evaluateString($string, $data = array()) {
$stack = $this->string($string)->parse();
$scope = $stack->_data();
foreach($data as $key => $value) {
$scope->$key = $value;
}
$stack->_ready();
return $stack->build();
}
/**
* Override content type
* @author Nate Ferrero
*/
public function setContentType($type) {
self::$contentType = $type;
}
/**
* Parse the loaded file
* @author Nate Ferrero
*/
public function parse($rparent = null, $topLevel = false) {
if(is_null($this->file) && is_null($this->string))
throw new Exception("LHTML: No file or string specified to parse");
if(is_null($parent))
$parent = new Node;
if(!($parent instanceof Node))
throw new Exception("Parent is not a Node");
if(!is_null($this->file)) {
/**
* Check cache for existing stack
* @author Nate Ferrero
*/
$ctime = e::$cache->timestamp('lhtml', $this->file);
if(isset($_GET['--lhtml-no-cache']) || $ctime === false || filemtime($this->file) > $ctime) {
/**
* Actually parse the file
* @author Nate Ferrero
*/
$this->stack = Parser::parseFile($this->file, $this->description, $parent);
/**
* Store in cache
* @author Nate Ferrero
*/
e::$cache->store('lhtml', $this->file, $this->stack);
}
/**
* Get the stack from the cache
*/
if(empty($this->stack)) {
$uncache = e::$cache->get('lhtml', $this->file);
if(!($uncache instanceof Node))
throw new Exception("Cached LHTML stack is not a Node");
//$uncache->appendTo($parent);
$this->stack = $uncache;
}
unset($this->file);
}
else if(!is_null($this->string)) {
$hash = md5($this->string);
/**
* Load strings from cache. Increases speed greatly when parsing database stored LHTML.
* @author David Boskovic
*/
if(!isset($_GET['--lhtml-no-cache']) && e::$cache->check('lhtml','string-'.$hash)) {
$uncache = e::$cache->get('lhtml', 'string-'.$hash);
if(!($uncache instanceof Node))
throw new Exception("Cached LHTML stack is not a Node");
$this->stack = $uncache;
}
else {
$this->stack = Parser::parseString($this->string, $this->description, $parent);
/**
* Store in cache
* @author David Boskovic
*/
e::$cache->store('lhtml', 'string-'.$hash, $this->stack);
}
unset($this->string);
}
/**
* Handle stack ready
* @author Nate Ferrero
*/
$stack = $this->stack;
if($rparent instanceof Node) {
$stack->appendTo($rparent);
$stack = $rparent;
}
$this->stack = $stack;
/**
* This creates a loop that either returns or readies the new stack, as needed
* @author Nate Ferrero
*/
while($topLevel) {
try {
$this->stack->_ready();
e\trace("LHTML Top-Level Render", '', $this->stack->children);
return $this->stack;
}
catch(UseAlternateStack $use) {
e\trace_exception($use);
$this->stack = $use->stack;
}
}
return $this->stack;
}
}
/**
* Use alternate stack exception
* @author Nate Ferrero
*/
class UseAlternateStack extends Exception {
public $stack;
}
/**
* Allow access to the e stack in LHTML
*/
class e_handle {
public function __call($method, $args) {
$method = strtolower($method);
if(!empty($args)) {
$method = "e::$method";
return call_user_func_array($method, $args);
}
if(!isset(e::$$method))
throw new Exception("Bundle `$method` is not installed");
return e::$$method;
}
}