00001 <?php
00002 require_once dirname(__FILE__) . '/../simple/templateengine.simple.php';
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class TemplateEngineCore extends TemplateEngineSimple {
00035
00036
00037
00038
00039
00040
00041 protected function resolve_path($file) {
00042 $file_path = parent::resolve_path($file);
00043 $compile_path = $this->get_compile_name($file_path);
00044 if (!file_exists($compile_path)) {
00045 $this->compile($compile_path, $file_path);
00046 }
00047 else if (filemtime($compile_path) < filemtime($file_path)) {
00048 $this->compile($compile_path, $file_path);
00049 }
00050 return $compile_path;
00051 }
00052
00053 protected function get_compile_name($file) {
00054 $path = str_replace('.tpl.php', '', $file);
00055 foreach(Load::get_base_directories() as $dir) {
00056 $path = str_replace($dir, '', $path);
00057 }
00058 $path = str_replace('view/templates/', '', $path);
00059 $pos = strpos($path, '/');
00060 $path = substr($path, $pos + 1);
00061 return Config::get_value(Config::TEMP_DIR) . 'view/templates_c/' . str_replace('/', '-', $path) . '-' . md5($file) . '.tpl-c.php';
00062 }
00063
00064 protected function compile($compile_path, $source_path) {
00065 $c = file_get_contents($source_path);
00066
00067
00068 $c = $this->resolve_includes($c);
00069
00070
00071 $c = $this->resolve_quick_tags($c);
00072 file_put_contents($compile_path, $c);
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 protected function resolve_quick_tags($content) {
00084 $regex = '@(<\?=)(.*?)(;|\?>)@';
00085 $rep = '<?php print String::escape($2)$3';
00086 return String::preg_replace($regex, $rep, $content);
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 protected function resolve_includes($content) {
00096 $regex = '@(<\?php(.*?))gyro_include_template\((.*?)\)(.*?)(\?>)@';
00097 $matches = array();
00098 while (preg_match($regex, $content, $matches)) {
00099
00100
00101
00102
00103
00104
00105 $rep = '$1include($this->resolve_path($3))$4$5';
00106 $content = preg_replace($regex, $rep, $content, 1);
00107 }
00108
00109 return $content;
00110 }
00111 }