-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRender.php
399 lines (317 loc) · 11 KB
/
Render.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
<?php
/**
* The Render class is used to render a view (template).
* Render also handles template tags and the templating engine.
*
* To extend Render capabilities, just make a new Service Provider
* copy over the view(), templateEngine(), and getTemplateTags() methods.
* Then add that new Service Provider in the config file.
*
* @package Mira Core
* @author Mira Framework
**/
namespace Mira\Render;
class Render
{
public static $section_variable;
public static $template_variables;
/**
* Check if multi-tenancy => true in the project config file
*
* @return bool
**/
public function multiTenancy()
{
// Multi-tenancy
$subdomain = self::getSubdomain();
$host = explode('.', $_SERVER['HTTP_HOST']);
$multi_tenancy = \Mira\Project\Project::config('multi-tenancy');
if (count($host) >= 3 && $subdomain != 'www') {
return true;
}
return false;
}
/**
* Check if the template has its own config file then include that
* else include a project wide config file.
*
* @param $template - an app name
* @return config file
**/
public static function getConfig($template)
{
$app_name = explode('.', $template);
$name = $app_name[0];
if (file_exists($_SERVER['DOCUMENT_ROOT']."/application/app/$name/config.php")) {
return $config = require $_SERVER['DOCUMENT_ROOT']."/application/app/$name/config.php";
} else {
return $config = require $_SERVER['DOCUMENT_ROOT'].'/config/config.php';
}
}
/**
* Get the subdomain from the url
*
* @return subdomain - first section of url
* @see multiTenancy()
* @test
**/
public static function getSubdomain()
{
// Multi-tenancy
$url = $_SERVER['HTTP_HOST'];
$host = explode('.', $url);
return $subdomain = $host[0];
}
/**
* undocumented function
*
* @param $pattern - a regex expression
* @param $replace - a replacement for the output
* @param $output - take input from file_get_contents() @see view()
* @return $output - a preg_replace template tag
* @see getTemplateTags()
* @test
**/
public static function register($pattern, $replace, $output)
{
return $output = preg_replace($pattern, $replace, $output);
}
/**
* Render a view
*
* @param $template - app.template string
* @param $variables (array) - an array of variables to pass to view
* @return null
**/
public static function view($template, $variables = [])
{
extract($variables);
self::$template_variables = $variables;
$config = self::getConfig($template);
self::multiTenancy();
ob_clean();
// Template Engine Logic
$template = explode(".", $template);
if (count($template) > 1) {
self::templateEngine($template, $variables);
} else {
return false;
}
$template = ob_get_contents();
ob_clean();
self::getHeader($config, $variables);
$head = ob_get_contents();
ob_clean();
self::getFooter($config, $variables);
$footer = ob_get_contents();
ob_end_clean();
echo $head;
echo $template;
echo $footer;
return true;
}
/**
* undocumented function
*
* @param $url - /normal/url/
* @return header
**/
public static function redirect($url)
{
Header("Location: $url");
return true;
}
/**
* undocumented function
*
* @param $app - app name
* @param $app_template - app template
* @param $variables (array) - array of variables to pass to template
* @return template file
* @see templateEngine()
* @test
**/
public static function getTemplate($app, $app_template, $variables = [])
{
extract($variables);
if (file_exists($_SERVER['DOCUMENT_ROOT']."/application/app/$app/templates/$app_template.engine.php")) {
include $_SERVER['DOCUMENT_ROOT']."/application/app/$app/templates/$app_template.engine.php";
return true;
} else {
if (file_exists($_SERVER['DOCUMENT_ROOT']."/application/app/$app/templates/$app_template.php")) {
include $_SERVER['DOCUMENT_ROOT']."/application/app/$app/templates/$app_template.php";
return true;
}
}
return false;
}
/**
* The engine to power the template syntax. Replaces template syntax
* with valid PHP code
*
* 1. Gets the file_get_contents of the template
* 2. Then runs getTemplateTags() which preg_replaces the output
* 3. Then returns the eval of the replaced template syntax
*
* @param $template - takes an exploded() template
* @param $variables (array) - array of variables to pass to view
* @return php eval code for Comet template syntax engine
* @see getTemplateTags()
**/
public static function templateEngine($template, $variables)
{
extract($variables);
if (self::multiTenancy()) {
$app = self::getSubdomain();
} else {
$app = $template[0];
}
$app_template = $template[1];
if (file_exists($_SERVER['DOCUMENT_ROOT']."/application/app/$app/templates/$app_template.engine.php")) {
$output = file_get_contents($_SERVER['DOCUMENT_ROOT']."/application/app/$app/templates/$app_template.engine.php");
$output = self::getTemplateTags($output);
echo eval(' ?>'.$output. ' ');
} else {
self::getTemplate($app, $app_template, $variables);
}
}
/**
* Function registers template tags for the Comet template engine.
*
* This function takes $output as a paramter which is from a
* file_get_contents() function. This function processes the
* $output variable and returns the last $output which will
* preg_replace() all the template engine syntax.
*
* @param $output - a file_get_contents() of the template
* @return output file
* @see view()
**/
public static function getTemplateTags($output)
{
// register template tags
$output = static::compileMustache($output);
$output = static::compileIfStatements($output);
$output = static::compileComments($output);
$output = static::compileUnless($output);
$output = static::compileUse($output);
$output = static::compileExcerpt($output);
$output = static::compileDate($output);
$output = static::compileTitle($output);
$output = static::compileDeclare($output);
$output = static::compileYield($output);
$output = static::compileSection($output);
return $output = self::register(self::matcher('extends'), '$1<?php Mira\\Render\Render::templateExtends($2) ?>', $output);
}
public static function compileMustache($output)
{
$output = self::register("/{{/", '<?=', $output);
return $output = self::register("/}}/", '?>', $output);
}
public static function compileIfStatements($output)
{
$output = self::register(self::matcher("(if|elseif|foreach|for|while)"), '$1<?php $2$3: ?>', $output);
$output = self::register("/(\s*)@(else)(\s*)/", '$1<?php $2:$i++; ?>$3', $output);
return $output = self::register('/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/', '$1<?php $2; ?>$3', $output);
}
public static function compileComments($output)
{
$output = self::register("/(\s*)@(comment)/", '$1<?php if (0): ?>', $output);
return $output = self::register("/(\s*)@(endcomment)/", "<?php endif; ?>", $output);
}
public static function compileUnless($output)
{
$output = self::register('/(\s*)@unless(\s*\(.*\))/', "$1<?php if ( ! ($2)): ?>", $output);
return $output = self::register('/(\s*)@(endunless)(\s*)/', '<?php endif; ?>', $output);
}
public static function compileUse($output)
{
return $output = self::register("/(\s*)@(use)(\s.*)/", "<?php use $3; ?>", $output);
}
public static function compileExcerpt($output)
{
return $output = self::register("/(\s*)@(excerpt)(\s.*)/", "<?= comet($3)->excerpt() ?>", $output);
}
public static function compileTitle($output)
{
return $output = self::register("/(\s*)@(title)(\s.*)/", "<?= comet($3)->title() ?>", $output);
}
public static function compileDate($output)
{
return $output = self::register("/(\s*)@(date)(\s.*)/", "<?= comet($3)->date() ?>", $output);
}
public static function compileDeclare($output)
{
return $output = self::register("/(\s*)@(declare)(\s.*)/", "<?php $3; ?>", $output);
}
public static function compileYield($output)
{
return $output = self::register("/(\s*)@yield(\s*\(.*\))/", "$1<?php static::yield($2); ?>", $output);
}
public static function compileSection($output)
{
$output = self::register('/(\s*)@section(\s*\(.*\))/', "$1<?php static::section($2); ob_start(); ?>", $output);
return $output = self::register('/(\s*)@(endsection)(\s*)/', '<?php $output = ob_get_contents(); ob_end_clean(); static::globals($output); ?>', $output);
}
/**
* Returns a pattern that matches expressions such as @tag('')
*
* @param $tag - the tag name to match such as extends,
* @return regex pattern
* @see getTemplateTags()
* @test
**/
public static function matcher($tag)
{
return '/(\s*)@'.$tag.'(\s*\(.*\))/';
}
/**
* undocumented function
*
* @return void
* @test
**/
public function getHeader($config, $variables)
{
$header = explode('.', $config['header']);
return static::templateEngine($header, $variables);
}
/**
* undocumented function
*
* @return void
* @test
**/
public function getFooter($config, $variables)
{
$footer = explode('.', $config['footer']);
return static::templateEngine($footer, $variables);
}
/**
* function for @extends() template syntax
*
* @param $template - app.template
* @return the template
* @see getTemplateTags()
* @test
**/
public static function templateExtends($template)
{
$template = explode('.', $template);
return static::templateEngine($template, self::$template_variables);
}
public function globals($content)
{
$GLOBALS['mira'][self::$section_variable] = trim($content);
}
public static function section($variable)
{
self::$section_variable = $variable;
}
public static function yield($name)
{
if (isset($GLOBALS['mira'][$name])) {
echo $GLOBALS['mira'][$name];
}
}
}