123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- class LtTemplateView
- {
- public $layout;
- public $layoutDir;
- public $template;
- public $templateDir;
- public $compiledDir;
- public $autoCompile;
- public $component;
- private $tpl_include_files;
- public function __construct()
- {
-
- $this->autoCompile = true;
- $this->component = false;
- }
- public function render()
- {
- if (empty($this->compiledDir))
- {
- $this->compiledDir = dirname($this->templateDir) . "/viewTpl/";
- }
- if (!empty($this->layout))
- {
- include $this->template(true);
- }
- else if ($this->component)
- {
- return;
- }
- else
- {
- include $this->template();
- }
- }
-
- public function template($islayout = false)
- {
- $this->layoutDir = rtrim($this->layoutDir, '\\/') . '/';
- $this->compiledDir = rtrim($this->compiledDir, '\\/') . '/';
- $this->templateDir = rtrim($this->templateDir, '\\/') . '/';
- if ($islayout)
- {
- $tplfile = $this->layoutDir . $this->layout . '.php';
- $objfile = $this->compiledDir . 'layout/' . $this->layout . '@' . $this->template . '.php';
- }
- else
- {
- $tplfile = $this->templateDir . $this->template . '.php';
- $objfile = $this->compiledDir . $this->template . '.php';
- }
- if (is_file($objfile))
- {
- if ($this->autoCompile)
- {
- $iscompile = true;
- $tpl_include_files = include($objfile);
- $last_modified_time = array();
- foreach($tpl_include_files as $f)
- {
- $last_modified_time[] = filemtime($f);
- }
- if (filemtime($objfile) == max($last_modified_time))
- {
- $iscompile = false;
- }
- }
- else
- {
- $iscompile = false;
- }
- }
- else
- {
-
- $iscompile = true;
- }
- if ($iscompile)
- {
- $this->tpl_include_files[] = $objfile;
- $this->tpl_include_files[] = $tplfile;
- $dir = pathinfo($objfile, PATHINFO_DIRNAME);
- if (!is_dir($dir))
- {
- if (!mkdir($dir, 0777, true))
- {
- trigger_error("Can not create $dir");
- }
- }
- $str = file_get_contents($tplfile);
- if (!$str)
- {
- trigger_error('Template file Not found or have no access!', E_USER_ERROR);
- }
- $str = $this->parse($str);
- if ($this->autoCompile)
- {
- $prefix = "<?php\r\nif(isset(\$iscompile)&&true==\$iscompile)\r\nreturn " . var_export(array_unique($this->tpl_include_files), true) . ";?>";
- $prefix = preg_replace("/([\r\n])+/", "\r\n", $prefix);
- $postfix = "\r\n<!--Template compilation time : " . date('Y-m-d H:i:s') . "-->\r\n";
- }
- else
- {
- $prefix = '';
- $postfix = '';
- }
- $str = $prefix . $str . $postfix;
- if (!file_put_contents($objfile, $str))
- {
- if (file_put_contents($objfile . '.tmp', $str))
- {
- copy($objfile . '.tmp', $objfile);
- unlink($objfile . '.tmp');
- }
- }
- @chmod($objfile,0777);
- }
- return $objfile;
- }
-
- protected function parse($str)
- {
- $str = $this->removeComments($str);
- $str = $this->parseIncludeComponent($str);
-
- $str = str_replace("{CR}", "<?php echo \"\\r\";?>", $str);
- $str = str_replace("{LF}", "<?php echo \"\\n\";?>", $str);
-
- $str = preg_replace("/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str);
- $str = preg_replace("/\{else\}/", "<?php } else { ?>", $str);
- $str = preg_replace("/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str);
- $str = preg_replace("/\{\/if\}/", "<?php } ?>", $str);
-
- $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/e", "\$this->addquote('<?php if(isset(\\1) && is_array(\\1)) foreach(\\1 as \\2) { ?>')", $str);
- $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/e", "\$this->addquote('<?php if(isset(\\1) && is_array(\\1)) foreach(\\1 as \\2=>\\3) { ?>')", $str);
- $str = preg_replace("/\{\/loop\}/", "<?php } ?>", $str);
-
- $str = preg_replace("/\{url\(([^}]+)\)\}/", "<?php echo LtObjectUtil::singleton('LtUrl')->generate(\\1);?>", $str);
-
- $str = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\s*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str);
- $str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \$\\1;?>", $str);
-
-
-
- $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str);
- $str = preg_replace("/\{(\\$[a-zA-Z0-9_\.\[\]\'\"\$\x7f-\xff]+)\}/e", "\$this->addquote('<?php echo \\1;?>')", $str);
-
- $str = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff][+\-\>\$\'\"\,\[\]\(\)a-zA-Z0-9_\x7f-\xff]+)\}/es", "\$this->addquote('<?php echo \\1;?>')", $str);
-
- $str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str);
-
- $str = preg_replace("/\{([a-zA-Z0-9_]*::?\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str);
- $str = preg_replace("/\{([a-zA-Z0-9_]*::?\\\$[a-zA-Z0-9_\.\[\]\'\"\$\x7f-\xff]+)\}/e", "\$this->addquote('<?php echo \\1;?>')", $str);
-
- $str = preg_replace("/\?\>\s*\<\?php[\r\n\t ]*/", "", $str);
-
- $str = preg_replace("/([\r\n])+/", "\r\n", $str);
-
- $str = preg_replace("/^[\r\n]+/", "", $str);
-
- $str = trim($str);
- return $str;
- }
-
- protected function addquote($var)
- {
- preg_match_all("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", $var, $vars);
- foreach($vars[1] as $k => $v)
- {
- if (is_numeric($v))
- {
- $var = str_replace($vars[0][$k], "[$v]", $var);
- }
- else
- {
- $var = str_replace($vars[0][$k], "['$v']", $var);
- }
- }
- return str_replace("\\\"", "\"", $var);
- }
-
- protected function removeComments($str, $clear = false)
- {
- $str = str_replace(array('<?php exit?>', '<?php exit;?>'), array('', ''), $str);
-
- $str = preg_replace("/([\r\n]+)[\t ]+/s", "\\1", $str);
- $str = preg_replace("/[\t ]+([\r\n]+)/s", "\\1", $str);
-
- $str = preg_replace("/\<\!\-\-\s*\{(.+?)\}\s*\-\-\>/s", "{\\1}", $str);
- $str = preg_replace("/\<\!\-\-\s*\-\-\>/s", "", $str);
-
- $str = preg_replace("/\<\!\-\-\s*[^\<\{]*\s*\-\-\>/s", "", $str);
- if ($clear)
- {
- $str = $this->clear($str);
- }
- return $str;
- }
-
- protected function clear($str)
- {
- preg_match_all("|<script[^>]*>(.*)</script>|Usi", $str, $tvar);
- foreach($tvar[0] as $k => $v)
- {
-
- $v = preg_replace("/\/\/\s*[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/", "", $v);
-
- $v = preg_replace("/\/\*[^\/]*\*\//s", "", $v);
- $str = str_replace($tvar[0][$k], $v, $str);
- }
- preg_match_all("|<style[^>]*>(.*)</style>|Usi", $str, $tvar);
- foreach($tvar[0] as $k => $v)
- {
-
- $v = preg_replace("/\/\*[^\/]*\*\//s", "", $v);
- $str = str_replace($tvar[0][$k], $v, $str);
- }
- return $str;
- }
-
- protected function parseIncludeComponent($str)
- {
- $count_include_component = preg_match_all("/\{include\s+(.+)\}/", $str, $tvar);
- $count_include_component += preg_match_all("/\{component\s+([a-zA-Z0-9\.\-_]+)\s+([a-zA-Z0-9\.\-_]+)\}/", $str, $tvar);
- unset($tvar);
- while ($count_include_component > 0)
- {
- $str = $this->parseInclude($str);
- $str = $this->parseComponent($str);
- $count_include_component = preg_match_all("/\{include\s+(.+)\}/", $str, $tvar);
- $count_include_component += preg_match_all("/\{component\s+([a-zA-Z0-9\.\-_]+)\s+([a-zA-Z0-9\.\-_]+)\}/", $str, $tvar);
- unset($tvar);
- }
- $str = $this->removeComments($str);
- return $str;
- }
-
- private function parseInclude($str)
- {
- $countSubTpl = preg_match_all("/\{include\s+(.+)\}/", $str, $tvar);
- while ($countSubTpl > 0)
- {
- foreach($tvar[1] as $k => $subfile)
- {
- eval("\$subfile = $subfile;");
- if (is_file($subfile))
- {
- $findfile = $subfile;
- }
- else if (is_file($subfile . '.php'))
- {
- $findfile = $subfile . '.php';
- }
- else if (is_file($this->templateDir . $subfile))
- {
- $findfile = $this->templateDir . $subfile;
- }
- else if (is_file($this->templateDir . $subfile . '.php'))
- {
- $findfile = $this->templateDir . $subfile . '.php';
- }
- else
- {
- $findfile = '';
- }
-
- if (!empty($findfile))
- {
- $subTpl = file_get_contents($findfile);
- $this->tpl_include_files[] = $findfile;
- }
- else
- {
-
- $subTpl = 'SubTemplate not found:' . $subfile;
- }
- $str = str_replace($tvar[0][$k], $subTpl, $str);
- }
- $countSubTpl = preg_match_all("/\{include\s+(.+)\}/", $str, $tvar);
- }
- return $str;
- }
-
- private function parseComponent($str)
- {
- $countCom = preg_match_all("/\{component\s+([a-zA-Z0-9\.\-_]+)\s+([a-zA-Z0-9\.\-_]+)\}/", $str, $tvar);
- while ($countCom > 0)
- {
- $i = 0;
- while ($i < $countCom)
- {
- $comfile = $this->templateDir . "component/" . $tvar[1][$i] . '-' . $tvar[2][$i] . '.php';
- if (is_file($comfile))
- {
- $subTpl = file_get_contents($comfile);
- $this->tpl_include_files[] = $comfile;
- }
- else
- {
- $subTpl = 'SubTemplate not found:' . $comfile;
- }
- $module = $tvar[1][$i];
- $action = $tvar[2][$i];
- $subTpl = "<?php
- \$dispatcher = LtObjectUtil::singleton('LtDispatcher');
- \$dispatcher->dispatchComponent('$module', '$action', \$this->context);
- \$comdata = \$dispatcher->data;
- unset(\$dispatcher);
- ?>
- " . $subTpl;
- $str = str_replace($tvar[0][$i], $subTpl, $str);
- $i++;
- }
- $countCom = preg_match_all("/\{component\s+([a-zA-Z0-9\.\-_]+)\s+([a-zA-Z0-9\.\-_]+)\}/", $str, $tvar);
- }
- return $str;
- }
- }
|