forked from laperla/codeigniter-Blade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlade.php
778 lines (640 loc) · 17.9 KB
/
Blade.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* This class is a port of Laravel's blade templating system.
* See usage docs in Laravel framework site.
*
* @author Miguel Ayllón
* @package Blade
* @category Libraries
* @version 1.0.2
* @url https://github.com/laperla/codeigniter-Blade
*
*/
class Blade
{
/**
* All of the compiler methods used by Blade.
*
* @var array
*/
protected $_compilers = array(
'extensions',
'comments',
'escaped_echos',
'js_expressions',
'echos',
'echos',
'forelse',
'empty',
'endforelse',
'structure_openings',
'structure_closings',
'switch',
'case',
'default_case',
'break',
'else',
'unless',
'endunless',
'includes',
'layouts',
'section_start',
'section_end',
'yields',
'yield_sections'
);
/**
* Stack of current sections being buffered
*
* @var array
*/
protected $_last_section = array();
/**
* Array of sections content
*
* @var array
*/
protected $_sections = array();
/**
* An array of user defined compilers.
*
* @var array
*/
protected $_extensions = array();
/**
* Global data array for templates
*
* @var array
*/
protected $_data = array();
/**
* Template file extension
*
* @var string
*/
public $blade_ext = '.blade.php';
/**
* Cache expire time
*
* @var int
*/
public $cache_time = 3600;
public function __set($name, $value)
{
$this->_data[$name] = $value;
}
public function __unset($name)
{
unset($this->_data[$name]);
}
public function __get($name)
{
if (key_exists($name, $this->_data))
{
return $this->_data[$name];
}
$_CI = & get_instance();
return $_CI->$name;
}
public function __construct()
{
$this->load->driver('cache');
}
/**
* Sets global data for template
*
* @param string $name
* @param mixed $value
* @return Blade
*/
public function set($name, $value)
{
$this->_data[$name] = $value;
return $this;
}
/**
* Appends or concats a value to a template global data if type is array
* or string respectively
*
* @param string $name
* @param mixed $value
* @return Blade
*/
public function append($name, $value)
{
if (is_array($this->_data[$name]))
{
$this->_data[$name][] = $value;
}
else
{
$this->_data[$name] .= $value;
}
return $this;
}
/**
* Sets multiple global data in array format
*
* @param array $data
* @return Blade
*/
public function set_data($data)
{
$this->_data = array_merge($this->_data, $data);
return $this;
}
/**
* Adds a custom compiler function
*
* @param mixed $compiler
* @return Blade
*/
public function extend($compiler)
{
$this->_extensions[] = $compiler;
return $this;
}
/**
* Outputs template content. You can also pass an array of global data.
* If $return is TRUE then returns the template as a string.
*
* @param string $template
* @param array $data
* @param bool $return
* @return string
*/
public function render($template, $data = NULL, $return = FALSE)
{
if (isset($data))
{
$this->set_data($data);
}
// Compile and run template
$compiled = $this->_compile($template);
$content = $this->_run($compiled, $this->_data);
if ( ! $return)
{
$this->output->append_output($content);
}
return $content;
}
/**
* Output default 404 page.
*
* @return string
*/
public function err_404()
{
return exit($this->render('404'));
}
/**
* Find the full path to a view file. Shows an error if file not found.
*
* @param string $view
* @return string
*/
protected function _find_view($view)
{
// Default location
$full_path = APPPATH . 'views/' . $view . $this->blade_ext;
// Modular Separation / Modular Extensions has been detected
if (method_exists($this->router, 'fetch_module'))
{
$module = $this->router->fetch_module();
list($path, $_view) = Modules::find($view . $this->blade_ext, $module, 'views/');
if ($path)
{
$full_path = $path . $_view;
}
}
// File not found
if ( ! is_file($full_path))
{
show_error('[Blade] Unable to find view: ' . $view);
}
return $full_path;
}
/**
* Compiles a template and stores it in the cache.
*
* @param string $template
* @return string
*/
protected function _compile($template)
{
// Prepare template info
$view_path = $this->_find_view($template);
$cache_id = 'blade-' . md5($view_path);
// Test if a compiled version exists in the cache
if ($compiled = $this->cache->file->get($cache_id))
{
// In production, avoid to test if the template was updated
if (ENVIRONMENT == 'production')
{
return $compiled;
}
// Return cache version if the template was not updated
$meta = $this->cache->file->get_metadata($cache_id);
if ($meta['mtime'] > filemtime($view_path))
{
return $compiled;
}
}
// Template content
$template = file_get_contents($view_path);
// Compilers
foreach ($this->_compilers as $compiler)
{
$method = "_compile_{$compiler}";
$template = $this->$method($template);
}
// Store compiled version in the cache
$this->cache->file->save($cache_id, $template, $this->cache_time);
// Return compiled template
return $template;
}
/**
* Runs a compiled template with its variables
*
* @param string $template
* @param array $data
* @return string
*/
protected function _run($template, $data = NULL)
{
if (is_array($data))
{
extract($data);
}
ob_start();
eval(' ?>' . $template . '<?php ');
$content = ob_get_clean();
return $content;
}
/**
* Get a template content for use inside the current template. Inherits
* all global data, combined with local data passed as second argument.
*
* @param string $template
* @param array $data
* @return string
*/
protected function _include($template, $data = NULL)
{
$template = str_replace('.','/',$template);
// Merge local data with global data
$data = isset($data) ? array_merge($this->_data, $data) : $this->_data;
// Compile and run template
$compiled = $this->_compile($template);
return $this->_run($compiled, $data);
}
/**
* Gets a section content
*
* @param string $section
* @return string
*/
protected function _yield($section)
{
return isset($this->_sections[$section]) ? $this->_sections[$section] : '';
}
/**
* Starts buffering section content
*
* @param string $section
*/
protected function _section_start($section)
{
array_push($this->_last_section, $section);
ob_start();
}
/**
* Stops buffering section content. Returns the current section name.
*
* @return string
*/
protected function _section_end()
{
$last = array_pop($this->_last_section);
$this->_section_extend($last, ob_get_clean());
return $last;
}
/**
* Stores section content, replacing '@parent' with the previous section
* content if any.
*
* @param string $section
* @param string $content
*/
protected function _section_extend($section, $content)
{
if (isset($this->_sections[$section]))
{
$this->_sections[$section] = str_replace('@parent', $content, $this->_sections[$section]);
}
else
{
$this->_sections[$section] = $content;
}
}
// -------------------------------------------------------
//
// COMPILERS
//
// -------------------------------------------------------
/**
* Get the regular expression for a generic Blade function.
*
* @param string $function
* @return string
*/
public function matcher($function)
{
return '/(\s*)@' . $function . '(\s*\(.*\))/';
}
/**
* Rewrites Blade comments into PHP comments.
*
* @param string $value
* @return string
*/
protected function _compile_comments($value)
{
$value = preg_replace('/\{\{--(.+?)(--\}\})?\n/', "<?php // $1 ?>", $value);
return preg_replace('/\{\{--((.|\s)*?)--\}\}/', "<?php /* $1 */ ?>\n", $value);
}
/**
* Rewrites Blade escaped echo statements into PHP escaped echo statements.
*
* @param string $value
* @return string
*/
protected function _compile_escaped_echos($value)
{
return preg_replace('/(?<!\{|@)\{\{(.+?)\}\}(?!\})/', '<?php echo addslashes($1); ?>', $value);
}
/**
* Rewrites Blade JavaScript expressions into regular JavaScript expressions.
*
* @param string $value
* @return string
*/
protected function _compile_js_expressions($value)
{
return preg_replace('/(?<!\{)@\{\{\s*(.+?)\s*\}\}(?!\})/', '{{ $1 }}', $value);
}
/**
* Rewrites Blade echo statements into PHP echo statements.
*
* @param string $value
* @return string
*/
protected function _compile_echos($value)
{
return preg_replace('/\{\!\!(.+?)\!\!\}/', "<?php echo $1 ?>", $value);
}
/**
* Rewrites Blade "for else" statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function _compile_forelse($value)
{
preg_match_all('/(\s*)@forelse(\s*\(.*\))(\s*)/', $value, $matches);
foreach ($matches[0] as $forelse)
{
preg_match('/\$[^\s]*/', $forelse, $variable);
// Once we have extracted the variable being looped against, we can add
// an if statement to the start of the loop that checks if the count
// of the variable being looped against is greater than zero.
$if = "<?php if (count({$variable[0]}) > 0): ?>";
$search = '/(\s*)@forelse(\s*\(.*\))/';
$replace = '$1' . $if . '<?php foreach$2: ?>';
$blade = preg_replace($search, $replace, $forelse);
// Finally, once we have the check prepended to the loop we'll replace
// all instances of this forelse syntax in the view content of the
// view being compiled to Blade syntax with real PHP syntax.
$value = str_replace($forelse, $blade, $value);
}
return $value;
}
/**
* Rewrites Blade "empty" statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function _compile_empty($value)
{
return str_replace('@empty', '<?php endforeach; ?><?php else: ?>', $value);
}
/**
* Rewrites Blade "forelse" endings into valid PHP.
*
* @param string $value
* @return string
*/
protected function _compile_endforelse($value)
{
return str_replace('@endforelse', '<?php endif; ?>', $value);
}
/**
* Rewrites Blade structure openings into PHP structure openings.
*
* @param string $value
* @return string
*/
protected function _compile_structure_openings($value)
{
$pattern = '/(\s*)@(if|elseif|foreach|for|while)(\s*\(.*\))/';
return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
}
/**
* Rewrites Blade structure closings into PHP structure closings.
*
* @param string $value
* @return string
*/
protected function _compile_structure_closings($value)
{
$pattern = '/(\s*)@(endif|endforeach|endfor|endwhile|endswitch)(\s*)/';
return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
}
/**
* Rewrites Blade switch statements into PHP switch statements.
*
* @param string $value
* @return string
*/
protected function _compile_switch($value)
{
$pattern = '/(\s*)@(switch)(\s*\(.*\))(\s*)@(case)(\s*)\((.*)\)/';
return preg_replace($pattern, '$1<?php $2$3:$4 $5 $6$7: ?>', $value);
}
/**
* Rewrites Blade case statements into PHP case statements.
*
* @param string $value
* @return string
*/
protected function _compile_case($value)
{
$pattern = '/(\s*)@(case)(\s*)\((.*)\)/';
return preg_replace($pattern, '$1<?php $2 $3$4: ?>', $value);
}
/**
* Rewrites Blade default statements into PHP default statements.
*
* @param string $value
* @return string
*/
protected function _compile_default_case($value)
{
$pattern = '/(\s*)@(default)(\s*)/';
return preg_replace($pattern, '$1<?php $2: ?>$3', $value);
}
/**
* Rewrites Blade break statements into PHP break statements.
*
* @param string $value
* @return string
*/
protected function _compile_break($value)
{
$pattern = '/(\s*)@(break)(\s*)/';
return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
}
/**
* Rewrites Blade else statements into PHP else statements.
*
* @param string $value
* @return string
*/
protected function _compile_else($value)
{
return preg_replace('/(\s*)@(else)(\s*)/', '$1<?php $2: ?>$3', $value);
}
/**
* Rewrites Blade "unless" statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function _compile_unless($value)
{
$pattern = '/(\s*)@unless(\s*\(.*\))/';
return preg_replace($pattern, '$1<?php if( ! ($2)): ?>', $value);
}
/**
* Rewrites Blade "unless" endings into valid PHP.
*
* @param string $value
* @return string
*/
protected function _compile_endunless($value)
{
return str_replace('@endunless', '<?php endif; ?>', $value);
}
/**
* Execute user defined compilers.
*
* @param string $value
* @return string
*/
protected function _compile_extensions($value)
{
foreach ($this->_extensions as $compiler)
{
$value = call_user_func($compiler, $value);
}
return $value;
}
/**
* Rewrites Blade @include statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function _compile_includes($value)
{
$pattern = static::matcher('include');
return preg_replace($pattern, '$1<?php echo $this->_include$2; ?>', $value);
}
/**
* Rewrites Blade "@layout" and "@extends" expressions into valid PHP.
*
* @param string $value
* @return string
*/
protected function _compile_layouts($value)
{
$pattern = $this->matcher('layout');
$pattern2 = $this->matcher('extends');
// Find "@layout" expressions
if ( ! preg_match_all($pattern, $value, $matches, PREG_SET_ORDER) && ! preg_match_all($pattern2, $value, $matches, PREG_SET_ORDER))
{
return $value;
}
// Delete "@layout" or "@extends" expressions
if (preg_match_all($pattern, $value, $matches, PREG_SET_ORDER)) {
$value = preg_replace($pattern, '', $value);
}
elseif (preg_match_all($pattern2, $value, $matches, PREG_SET_ORDER)) {
$value = preg_replace($pattern2, '', $value);
}
// Include layouts at the end of template
foreach ($matches as $set)
{
$value .= "\n" . $set[1] . '<?php echo $this->_include' . $set[2] . "; ?>\n";
}
return $value;
}
/**
* Rewrites Blade @yield statements into Section statements.
*
* The Blade @yield statement is a shortcut to the Section::yield method.
*
* @param string $value
* @return string
*/
protected function _compile_yields($value)
{
$pattern = $this->matcher('yield');
return preg_replace($pattern, '$1<?php echo $this->_yield$2; ?>', $value);
}
/**
* Rewrites Blade @section statements into Section statements.
*
* @param string $value
* @return string
*/
protected function _compile_section_start($value)
{
$pattern = $this->matcher('section');
return preg_replace($pattern, '$1<?php $this->_section_start$2; ?>', $value);
}
/**
* Rewrites Blade @endsection statements into Section statements.
*
* @param string $value
* @return string
*/
protected function _compile_section_end($value)
{
$replace = '<?php $this->_section_end(); ?>';
return str_replace('@endsection', $replace, $value);
}
/**
* Rewrites Blade yield section statements into valid PHP.
*
* @return string
*/
protected function _compile_yield_sections($value)
{
$replace = '<?php echo $this->_yield($this->_section_end()); ?>';
return str_replace('@yield_section', $replace, $value);
}
}
// END Blade class
/* End of file Blade.php */