PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Template/class.VTE.php

https://gitlab.com/vanthanhhoh/devlovebook
PHP | 596 lines | 508 code | 49 blank | 39 comment | 99 complexity | f8575e1ee4225946628bee3f61e6be7b MD5 | raw file
  1. <?php
  2. //$ArrayElement = '(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)';
  3. class VTE
  4. {
  5. private $Tag = array('left' => '{', 'right' => '}');
  6. protected $SingleVar = '[a-zA-Z0-9\_]';
  7. protected $VarElement = '\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*(.*?)';
  8. private $CACHE_EXPIRE_TIME = 3600; // default cache expire time = hour
  9. private $InputTPLString = '';
  10. private $TPLFileName = '';
  11. private $HtmlString = '';
  12. private $CompiledPHPCode = '';
  13. private $TPLFilePath = '';
  14. private $CompiledFilePath = '';
  15. private $CachedFilePath = '';
  16. private $ReCompile = false;
  17. private $IsCache = false;
  18. private $VAR = array();
  19. static $TPLFileDir = '/';
  20. static $CompiledDir = '/';
  21. static $CacheDir = '/';
  22. static $MergedDir = '/';
  23. public $_TPLFileDir = '/';
  24. public $_CompiledDir = '/';
  25. public $_CacheDir = '/';
  26. public $_MergedDir = '/';
  27. public $TPLFileExt = '.tpl';
  28. static function TPLFileDir($TPLFileDir) {
  29. self::$TPLFileDir = $TPLFileDir;
  30. }
  31. public function __construct() {
  32. $this->_TPLFileDir = self::$TPLFileDir;
  33. $this->_CompiledDir = self::$CompiledDir;
  34. $this->_CacheDir = self::$CacheDir;
  35. $this->_MergedDir = self::$MergedDir;
  36. }
  37. public function SetDir($Name, $Value) {
  38. $Name = '_' . $Name;
  39. $this->$Name = $Value;
  40. return $this;
  41. }
  42. public function String($HtmlString = '', $ReCompile = false, $IsCache = false) {
  43. $this->ReCompile = $ReCompile;
  44. $this->IsCache = $IsCache;
  45. $this->HtmlString = $HtmlString;
  46. return $this;
  47. }
  48. public function Compiled($CompiledPHPCode, $ReCompile = false, $IsCache) {
  49. $this->ReCompile = $ReCompile;
  50. $this->IsCache = $IsCache;
  51. $this->CompiledPHPCode = $CompiledPHPCode;
  52. return $this;
  53. }
  54. public function File($TPLFile = '', $ReCompile = false, $IsCache = false) {
  55. $this->ReCompile = $ReCompile;
  56. $this->IsCache = $IsCache;
  57. if(file_exists($TPLFile)) {
  58. $this->SetDir('TPLFileDir', dirname($TPLFile) . DIRECTORY_SEPARATOR);
  59. $this->TPLFileName = rtrim(basename($TPLFile), '.tpl');
  60. }
  61. else $this->TPLFileName = $TPLFile;
  62. return $this;
  63. }
  64. public function Assign($Name, $Value = NULL) {
  65. is_array($Name) ? $this->VAR += $Name : $this->VAR[$Name] = $Value;
  66. return $this;
  67. }
  68. public function Output($Return = true, $ReturnCompiled = false) {
  69. if($this->TPLFileName != '') $this->InputTPLString = $this->ReadTPLFile();
  70. $TPLFileName = rtrim(basename($this->TPLFilePath), $this->TPLFileExt);
  71. $EncodedFileName = $TPLFileName . '_' . md5($this->TPLFilePath);
  72. $this->CompiledFilePath = $this->_CompiledDir . $EncodedFileName . '.php';
  73. $this->CachedFilePath = $this->_CacheDir . $EncodedFileName . '.html';
  74. if( $this->ReCompile ||
  75. !file_exists($this->CompiledFilePath) ||
  76. (file_exists($this->CompiledFilePath) && filemtime($this->CompiledFilePath) < filemtime($this->TPLFilePath))
  77. ) $ReCompile = true;
  78. else $ReCompile = false;
  79. if($ReCompile) $this->CompileTPL();
  80. if($ReturnCompiled) {
  81. $CompiledContent = file_get_contents($this->CompiledFilePath);
  82. return array('var' => $this->VAR, 'content' => $CompiledContent);
  83. }
  84. elseif($this->IsCache) {
  85. $CacheLifeTime = time() - filemtime($this->CachedFilePath);
  86. if(file_exists($this->CachedFilePath) && $CacheLifeTime < $this->CACHE_EXPIRE_TIME )
  87. file_get_contents( $this->CachedFilePath );
  88. else
  89. file_put_contents($this->CachedFilePath, $OutputContent );
  90. }
  91. elseif(!$Return && !$this->IsCache) {
  92. extract($this->VAR);
  93. include $this->CompiledFilePath;
  94. }
  95. else {
  96. ob_start();
  97. extract($this->VAR);
  98. include $this->CompiledFilePath;
  99. $OutputContent = ob_get_clean();
  100. // return or print the template
  101. if($Return) return $OutputContent; else echo $OutputContent;
  102. }
  103. }
  104. private function ReadTPLFile() {
  105. $this->TPLFilePath = $this->_TPLFileDir . $this->TPLFileName . $this->TPLFileExt;
  106. if(file_exists($this->TPLFilePath)) {
  107. return file_get_contents($this->TPLFilePath);
  108. }
  109. else {
  110. throw new VTE_Exception ('TPL file not found ' . $this->TPLFilePath);
  111. trigger_error('TPL file not found ' . $this->TPLFilePath);
  112. }
  113. }
  114. private function DetectCodeTag() {
  115. $SingleVar = $this->SingleVar;
  116. $VarElement = $this->VarElement;
  117. $CodesCheck =
  118. array(
  119. 'if' => 'if(:condition){0,1}\s*\(.*\)',
  120. 'elseif' => 'elseif(:condition){0,1}\s*\(.*\)',
  121. 'else' => 'else',
  122. 'end_if' => '\/if',
  123. 'for' => 'for\s+\$' . $SingleVar . '+\s+in\s+\$' . $VarElement . '*\s*(?: as\s+\$' . $SingleVar . '+)?',
  124. 'end_for' => '\/for',
  125. 'while' => 'while(:condition){0,1}\s*\(.*\)',
  126. 'end_while' => '\/while',
  127. 'skip' => 'skip',
  128. 'end_skip' => '\/skip',
  129. 'comment' => '\/\*\*',
  130. 'end_comment' => '\*\*\/',
  131. 'function' => 'function',
  132. 'end_function' => '\/function',
  133. 'do' => 'do',
  134. 'end_do' => '\/do',
  135. 'block' => '\{block(:block_name){0,1}\s*\:\:\s*.*\}',
  136. 'lang' => '\{lang\s*\:\:\s*(:pharse){0,1}\s*[a-zA-Z0-9\_\-\s]*(?:\:.*){0,1}\}'
  137. );
  138. $_c = array();
  139. foreach($CodesCheck as $key =>$code)
  140. $_c[$key] = '(\\' . $this->Tag['left'] . $code . '\\' . $this->Tag['right'] . ')';
  141. $_c['area'] = '(\[\@.*\@\])';
  142. $CodesCheck = $_c; unset($_c);
  143. $CodesCheck = '/' . join('|', $CodesCheck) . '/';
  144. $return = preg_split ($CodesCheck, $this->InputTPLString, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  145. //n($return);
  146. //die();
  147. return $return;
  148. }
  149. private function VariableTrueForm($Var) {
  150. $_temp = preg_split( "/\.|\[|\-\>/", $Var );
  151. $VarName = $_temp[0];
  152. //variable path
  153. $VariablePath = substr( $Var, strlen( $VarName ) );
  154. //parentesis transform [ e ] in [" e in "]
  155. if(!empty($VariablePath)) {
  156. if(preg_match_all('/' . '(.*)\[\$(.*)\](.*)' . '/', $VariablePath, $matches)) {
  157. if(isset($matches[2][0]) && !empty($matches[2][0])) {
  158. $VariablePath = '[$' . $this->VariableTrueForm($matches[2][0]) . ']';
  159. }
  160. }
  161. else {
  162. if(!empty($VariablePath)) {
  163. $VariablePath = str_replace( '[', '["', $VariablePath );
  164. $VariablePath = str_replace( ']', '"]', $VariablePath );
  165. }
  166. }
  167. }
  168. //transform .$variable in ["$variable"] and .variable in ["variable"]
  169. if(!empty($VariablePath)) {
  170. $VariablePath = preg_replace('/\.(\w+)/', "['\\1']", $VariablePath );
  171. $VariablePath = preg_replace('/\.(\${1}\w+)/', '[\\1]', $VariablePath );
  172. }
  173. return $VarName . $VariablePath;
  174. }
  175. private function CompileFunction($Code, $echo = false) {
  176. //if(preg_match_all('/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $Code, $matches)) {
  177. //if(preg_match_all('/' . '\{(\#{0,1}\${0,1}[a-zA-Z0-9\_]*?)(\|\${0,1}\w.*?)\}' . '/', $Code, $matches)) {
  178. if(preg_match_all('/' . '\{(\#{0,1}\${0,1}\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*?)(\|\${0,1}\w.*?)\}' . '/', $Code, $matches)) {
  179. $Parsed = array();
  180. //n($matches);
  181. $TotalTags = count($matches[0]);
  182. for($tagIndex = 0; $tagIndex < $TotalTags; $tagIndex++) {
  183. $Tag = array();
  184. $Tag['key'] = $matches[0][$tagIndex];
  185. $Tag['VariableName'] = $matches[1][$tagIndex];
  186. $Tag['ExtraFunction'] = $matches[2][$tagIndex];
  187. $Tag['FunctionName'] = '';
  188. $Tag['FunctionParams'] = '';
  189. if(preg_match_all('/\{(\$' . $this->VarElement . ')((?:\+\+|\-\-|\+=|\-=|\*=|=|\.=)([^=].*))\}/', $Tag['key'], $m)){
  190. $VarKey = $m[1][0];
  191. $IsInitializeVar = true;
  192. $InitializeVar = $m[1][0];
  193. $FunctionVar = trim($m[3][0]);
  194. $VarOnly = trim($m[4][0]);
  195. $subLength = strlen($FunctionVar) - strlen($VarOnly);
  196. $Math = trim(substr($FunctionVar, 0, $subLength));
  197. $VarOnly = substr($VarOnly, 0, -strlen($Tag['ExtraFunction']));
  198. if($Tag['ExtraFunction'] && $Tag['ExtraFunction'][0] == '|') {
  199. $Tag['ExtraFunction'] = ltrim($Tag['ExtraFunction'], '|');
  200. //Check Static Class method
  201. if(strpos($Tag['ExtraFunction'], '::') != NULL)
  202. $Tag['ExtraFunction'] = str_replace('::', '@@StaticClassMethodSignal@@', $Tag['ExtraFunction']);
  203. if(strpos($Tag['ExtraFunction'], ':') != NULL) {
  204. $_f = explode(':', $Tag['ExtraFunction']);
  205. $Tag['FunctionName'] = $_f[0];
  206. $Tag['FunctionParams'] = $_f[1];
  207. }
  208. else $Tag['FunctionName'] = $Tag['ExtraFunction'];
  209. $Tag['FunctionName'] = str_replace('@@StaticClassMethodSignal@@', '::', $Tag['FunctionName']);
  210. $FunctionName = trim($Tag['FunctionName']);
  211. $InitializeVar = $this->VariableTrueForm(trim($InitializeVar));
  212. $VarOnly = $this->VariableTrueForm(trim($VarOnly));
  213. $ExtraParams = $Tag['FunctionParams'];
  214. $PhpCompiled = !empty($ExtraParams) ? "$InitializeVar $Math $FunctionName($VarOnly,$ExtraParams)" : "$InitializeVar $Math $FunctionName($VarOnly)";
  215. $PhpCompiled = '<?php ' . $PhpCompiled . ' ?>';
  216. $Code = str_replace($Tag['key'], $PhpCompiled, $Code);
  217. }
  218. }
  219. else {
  220. if($Tag['ExtraFunction'] && $Tag['ExtraFunction'][0] == '|') {
  221. $ExtraFunctions = array_filter(explode('|', $Tag['ExtraFunction']));
  222. $PhpCompiled = '';
  223. $i = 0;
  224. foreach($ExtraFunctions as $ExtraFunction) {
  225. if($i == 0) $VariableName = $this->VariableTrueForm(trim($Tag['VariableName']));
  226. else $VariableName = $PhpCompiled;
  227. $Tag['ExtraFunction'] = $ExtraFunction;
  228. //Check Static Class method
  229. if(strpos($Tag['ExtraFunction'], '::') != NULL)
  230. $Tag['ExtraFunction'] = str_replace('::', '@@StaticClassMethodSignal@@', $Tag['ExtraFunction']);
  231. if(strpos($Tag['ExtraFunction'], ':') != NULL) {
  232. $_f = explode(':', $Tag['ExtraFunction']);
  233. $Tag['FunctionName'] = $_f[0];
  234. $Tag['FunctionParams'] = $_f[1];
  235. }
  236. else $Tag['FunctionName'] = $Tag['ExtraFunction'];
  237. $Tag['FunctionName'] = str_replace('@@StaticClassMethodSignal@@', '::', $Tag['FunctionName']);
  238. $FunctionName = trim($Tag['FunctionName']);
  239. $ExtraParams = $Tag['FunctionParams'];
  240. $PhpCompiled = (!empty($ExtraParams) ? "$FunctionName($VariableName,$ExtraParams)" : "$FunctionName($VariableName)");
  241. $i++;
  242. }
  243. $PhpCompiled = ($echo ? 'echo ' : NULL) . $PhpCompiled;
  244. $PhpCompiled = '<?php ' . $PhpCompiled . ' ?>';
  245. $Code = str_replace($Tag['key'], $PhpCompiled, $Code);
  246. }
  247. }
  248. }
  249. }
  250. return $Code;
  251. }
  252. private function CompileVariable($Code, $echo = true) {
  253. if(preg_match_all('/\{\$(' . $this->VarElement . ')\}/', $Code, $matches)) {
  254. $n = count($matches[0]);
  255. $Parsed = array();
  256. for($i = 0; $i < $n; $i++ )
  257. $Parsed[$matches[0][$i]] = array('Var' => $matches[1][$i],'ExtraVar'=>$matches[2][$i]);
  258. foreach($Parsed as $Key => $Value) {
  259. $IsInitializeVar = false;
  260. $VarKey = '';
  261. if(preg_match_all('/\{(\$' . $this->VarElement . ')((?:\+\+|\-\-|\+=|\-=|\*=|=)([^=].*))\}/', $Key, $m)) {
  262. $VarKey = trim($m[1][0]);
  263. $IsInitializeVar = true;
  264. }
  265. if($IsInitializeVar) {
  266. $PhpCompiled = '<?php ' . $this->VariableTrueForm($VarKey) . $Value['ExtraVar'] . ' ?>';
  267. }
  268. elseif($echo) {
  269. $PhpCompiled = '<?php ' . ($echo ? 'echo ' : NULL) . $this->VariableTrueForm('$' . $Value['Var']) . ' ?>';
  270. }
  271. else
  272. $PhpCompiled = $this->VariableTrueForm('$' . $Value['Var']);
  273. $Code = str_replace($Key, $PhpCompiled, $Code);
  274. }
  275. }
  276. return $Code;
  277. }
  278. private function CompileConstant($Code, $echo = true) {
  279. if(preg_match_all('/\{\#(' . $this->VarElement . ')\}/', $Code, $matches)) {
  280. $n = count($matches[0]);
  281. $Parsed = array();
  282. for($i = 0; $i < $n; $i++ )
  283. $Parsed[$matches[0][$i]] = array('Var' => $matches[1][$i],'ExtraVar'=>$matches[2][$i]);
  284. foreach($Parsed as $Key => $Value) {
  285. $IsInitializeVar = false;
  286. $VarKey = '';
  287. if($echo) {
  288. $PhpCompiled = '<?php ' . ($echo ? 'echo ' : NULL) . $Value['Var'] . ' ?>';
  289. }
  290. else
  291. $PhpCompiled = $Value['Var'];
  292. $Code = str_replace($Key, $PhpCompiled, $Code);
  293. }
  294. }
  295. return $Code;
  296. }
  297. private function CompileCondition($Code, $echo = false) {
  298. if(preg_match_all('/\$(' . $this->VarElement . ')/', $Code, $matches)) {
  299. $n = count($matches[0]);
  300. $Parsed = array();
  301. for($i = 0; $i < $n; $i++ )
  302. $Parsed[$matches[0][$i]] = array('Var' => $matches[1][$i],'ExtraVar'=>$matches[2][$i]);
  303. foreach($Parsed as $Key => $Value) {
  304. if($echo)
  305. $PhpCompiled = '<?php ' . ($echo ? 'echo ' : NULL) . $this->VariableTrueForm('$' . $Value['Var']) . ' ?>';
  306. else
  307. $PhpCompiled = $this->VariableTrueForm('$' . $Value['Var']);
  308. $Code = str_replace($Key, $PhpCompiled, $Code);
  309. }
  310. }
  311. return $Code;
  312. }
  313. private function CompileTPL() {
  314. $TPLCodes = $this->DetectCodeTag();
  315. $CompiledCode = array();
  316. $IfOpen = $ForOpen = $WhileOpen = $CommentOpen = $SkipOpen = 0;
  317. $CheckCommand['if'] = '/\\' . $this->Tag['left'] . 'if\s*\(\s*(.*)\s*\)\s*\\' . $this->Tag['right'] . '/';
  318. $CheckCommand['elseif'] = '/\\' . $this->Tag['left'] . 'elseif(?: condition){0,1}\s*\(\s*(.*)\s*\)\s*\\' . $this->Tag['right'] . '/';
  319. $CheckCommand['for'] = '/\\' . $this->Tag['left'] . 'for\s+\$(?P<ArrayElement>' . $this->VarElement . ')\s+in\s+\$(?P<Array>' . $this->VarElement . ')\s*(?: as\s+\$(?P<ArrayKey>' . $this->VarElement . '))?\\' . $this->Tag['right'] . '/';
  320. $CheckCommand['while'] = '/\\' . $this->Tag['left'] . 'while\s*\(\s*(.*)\s*\)\s*\\' . $this->Tag['right'] . '/';
  321. $CheckCommand['block'] = '/\\' . $this->Tag['left'] . '\{block\:\:([^0-9][a-zA-Z0-9\_]+)\s*\:{0,1}([a-zA-Z0-9\,\s]*)\}\\' . $this->Tag['right'] . '/';
  322. $CheckCommand['area'] = '/\[\@([^0-9][a-zA-Z0-9\_]*)\@\]/';
  323. $CheckCommand['lang'] = '/\\' . $this->Tag['left'] . '\{lang\s*\:\:\s*([a-zA-Z0-9\_\-\s]+)\s*\:{0,1}(.*)\}\\' . $this->Tag['right'] . '/';
  324. while($Html = array_shift($TPLCodes)) {
  325. // Remove all comment tag
  326. if($Html == '{**/}') {
  327. if($CommentOpen < 1) continue;
  328. else $CommentOpen--;
  329. }
  330. elseif($Html == '{/**}') {
  331. $CommentOpen++;
  332. }
  333. elseif($CommentOpen > 0 ) continue;
  334. // Skip all betwen Skip tag
  335. elseif($Html == '{skip}') {
  336. $SkipOpen++;
  337. if($SkipOpen == 1) continue;
  338. else $CompiledCode[] = $Html;
  339. }
  340. elseif($Html == '{/skip}') {
  341. if($SkipOpen == 1) {
  342. $SkipOpen = 0;
  343. continue;
  344. }
  345. else {
  346. $SkipOpen--;
  347. $CompiledCode[] = $Html;
  348. }
  349. }
  350. elseif($SkipOpen > 0) $CompiledCode[] = $Html;
  351. // Compile If command
  352. elseif(preg_match($CheckCommand['if'], $Html, $Code)) {
  353. $IfOpen++;
  354. $IfCondition = $Code[1];
  355. $parsed_condition = $this->CompileCondition($IfCondition, false);
  356. $CompiledCode[] = "<?php if($parsed_condition) { ?>";
  357. }
  358. //elseif
  359. elseif( preg_match($CheckCommand['elseif'], $Html, $Code ) ){
  360. $tag = $Code[0];
  361. $condition = $Code[1];
  362. $parsed_condition = $this->CompileCondition($condition, false);
  363. $CompiledCode[] = "<?php } elseif($parsed_condition) { ?>";
  364. }
  365. //else
  366. elseif( strpos( $Html, '{else}' ) !== FALSE )
  367. $CompiledCode[] = '<?php }else{ ?>';
  368. //close if tag
  369. elseif( strpos( $Html, '{/if}' ) !== FALSE ) {
  370. $IfOpen--;
  371. $CompiledCode[] = '<?php } ?>';
  372. }
  373. // Compile For command
  374. elseif(preg_match($CheckCommand['for'], $Html, $Code)) {
  375. $ForOpen++;
  376. $Array = $this->VariableTrueForm('$' . $Code['Array']);
  377. $ArrayElement = $this->VariableTrueForm('$' . $Code['ArrayElement']);
  378. if(isset($Code['ArrayKey'])) {
  379. $ArrayKey = $this->VariableTrueForm('$' . $Code['ArrayKey']);
  380. $CompiledCode[] = "<?php foreach($Array as $ArrayKey => $ArrayElement) { ?>";
  381. }
  382. else
  383. $CompiledCode[] = "<?php foreach($Array as $ArrayElement) { ?>";
  384. }
  385. //close For tag
  386. elseif( strpos( $Html, '{/for}' ) !== FALSE ) {
  387. $ForOpen--;
  388. $CompiledCode[] = '<?php } ?>';
  389. }
  390. // Compile While command
  391. elseif(preg_match($CheckCommand['while'], $Html, $Code)) {
  392. $WhileOpen++;
  393. $While = $Code[1];
  394. $parsed_while = $this->CompileCondition($While, false);
  395. $CompiledCode[] = "<?php while($parsed_while) { ?>";
  396. }
  397. //close For tag
  398. elseif( strpos( $Html, '{/while}' ) !== FALSE ) {
  399. $WhileOpen--;
  400. $CompiledCode[] = '<?php } ?>';
  401. }
  402. // Compile function
  403. elseif($Html == '{function}') {
  404. $CompiledCode[] = '<?php ';
  405. }
  406. elseif($Html == '{/function}') {
  407. $CompiledCode[] = ' ?>';
  408. }
  409. elseif($Html == '{do}') {
  410. $CompiledCode[] = '<?php ';
  411. }
  412. elseif($Html == '{/do}') {
  413. $CompiledCode[] = ' ?>';
  414. }
  415. // Compile block command
  416. elseif(preg_match($CheckCommand['block'], $Html, $Code)) {
  417. $CompiledCode[] = "<?php echo Theme::useBlock('" . $Code[1] . ":" . $Code[2] . "') ?>";
  418. }
  419. // Compile area command
  420. elseif(preg_match($CheckCommand['area'], $Html, $Code)) {
  421. $CompiledCode[] = "<?php echo Theme::getArea('" . $Code[1] . "') ?>";
  422. }
  423. // Compile lang command
  424. elseif(preg_match($CheckCommand['lang'], $Html, $Code)) {
  425. //n($Code);
  426. $Code[2] = trim($Code[2], ',');
  427. $Code[2] = $this->CompileCondition($Code[2], false);
  428. if($Code[2] != '')
  429. $CompiledCode[] = "<?php echo Lang::get_string('" . $Code[1] . "', " . $Code[2] . ") ?>";
  430. else
  431. $CompiledCode[] = "<?php echo Lang::get_string('" . $Code[1] . "') ?>";
  432. }
  433. else {
  434. $Html = $this->CompileFunction($Html, true);
  435. $Html = $this->CompileConstant($Html, true);
  436. $CompiledCode[] = $this->CompileVariable($Html, true);
  437. }
  438. //else $CompiledCode[] = '<font color="red">' . $Html . '</font>';
  439. }
  440. //die();
  441. file_put_contents($this->CompiledFilePath, $CompiledCode);
  442. }
  443. static function MergeCompiledFile($CompiledData,$MergedFileName) {
  444. $MergedContent = array();
  445. foreach($CompiledData as $D) {
  446. $_Var = var_export($D['var'], true);
  447. $_Content = $D['content'];
  448. $MergedContent[] = '<?php $_Var = ' . $_Var . '; extract($_Var); ?>' . $_Content;
  449. }
  450. file_put_contents(VTE::$MergedDir . $MergedFileName, implode(PHP_EOL, $MergedContent));
  451. }
  452. static function DeleteMergedFiles($Path = '') {
  453. if($Path == '') $Path = VTE::$MergedDir;
  454. $ListFiles = glob($Path . '*');
  455. foreach($ListFiles as $File)
  456. if(is_file($File)) unlink($File);
  457. /*
  458. foreach (new DirectoryIterator($Path) as $fileInfo) {
  459. if(!$fileInfo->isDot()) {
  460. unlink($fileInfo->getPathname());
  461. //unlink($Path . $fileInfo->getPathname());
  462. }
  463. }
  464. */
  465. }
  466. static function DeleteCompiledFiles($Path = '') {
  467. if($Path == '') $Path = VTE::$CompiledDir;
  468. $ListFiles = glob($Path . '*');
  469. foreach($ListFiles as $File)
  470. if(is_file($File)) unlink($File);
  471. }
  472. static function DeleteCachedFiles($Path = '') {
  473. if($Path == '') $Path = VTE::$CacheDir;
  474. $ListFiles = glob($Path . '*');
  475. foreach($ListFiles as $File)
  476. if(is_file($File)) unlink($File);
  477. }
  478. }
  479. class MergedTPL
  480. {
  481. private $MergedFileList = array();
  482. private $MergedData = array();
  483. private $TotalFile = 0;
  484. private $CurrentFile = 0;
  485. static $TPLFileDir = '/';
  486. static $CacheDir = '/';
  487. static $MergedDir = '/';
  488. private $TPLFileExt = '.tpl';
  489. private function Init() {
  490. MergedTPL::$TPLFileDir = VTE::$TPLFileDir;
  491. MergedTPL::$CacheDir = VTE::$CacheDir;
  492. MergedTPL::$MergedDir = VTE::$MergedDir;
  493. }
  494. public function Merge($TPLFileList) {
  495. $this->Init();
  496. $this->TotalFile = count($TPLFileList);
  497. $this->MergedFileList = $TPLFileList;
  498. return $this;
  499. }
  500. public function SavePath($Path = '') {
  501. if($Path != '') MergedTPL::$MergedDir = $Path;
  502. return $this;
  503. }
  504. public function AddFile($TPLFileName, $Vars = array(), $TPLFileDir = '') {
  505. $this->CurrentFile++;
  506. $TPLFileDir = ($TPLFileDir == '') ? MergedTPL::$TPLFileDir : $TPLFileDir;
  507. //$TPLFileName = rtrim($TPLFileName, '.tpl');
  508. $this->MergedData[$this->CurrentFile] = array('file' => $TPLFileName, 'dir' => $TPLFileDir, 'var' . $this->CurrentFile => $Vars);
  509. return $this;
  510. }
  511. public function OutputMerged($Return = false, $ReCompiled = false) {
  512. $MergedVars = array();
  513. $FileName = implode('_', $this->MergedFileList);
  514. $MergedFilePath = MergedTPL::$MergedDir . $FileName . '_' . md5($FileName) . '.php';
  515. $DetectTPLChanged = 0;
  516. $MergedFileTime = filemtime($MergedFilePath);
  517. foreach($this->MergedData as $i => $Data) {
  518. $MergedVars['var' . $i] = $Data['var' . $i];
  519. $_TPLFile = $Data['dir'] . $Data['file'] . $this->TPLFileExt;
  520. if(filemtime($_TPLFile) > $MergedFileTime)
  521. $DetectTPLChanged++;
  522. }
  523. if(file_exists($MergedFilePath) && !$ReCompiled && $DetectTPLChanged == 0) {
  524. }
  525. else {
  526. $MergedContent = '';
  527. foreach($this->MergedData as $i => $Data) {
  528. $MergedVars['var' . $i] = $Data['var' . $i];
  529. $MergedContent .= '<?php extract($MergedVars[\'var' . $i . '\']); ?>';
  530. $_CompiledFile = TPL::File($Data['file'], true)
  531. ->SetDir('TPLFileDir', $Data['dir'])
  532. //->Assign('var' . $i, $Data['var' . $i])
  533. ->Output(false, true);
  534. $MergedContent .= $_CompiledFile['content'];
  535. }
  536. file_put_contents($MergedFilePath, '<?php if(!class_exists(\'MergedTPL\')) die(\'Invalid action!\') ?>' . $MergedContent);
  537. }
  538. if($Return) {
  539. ob_start();
  540. include $MergedFilePath;
  541. $OutputContent = ob_get_clean();
  542. return array('var' => $MergedVars, 'content' => $OutputContent);
  543. }
  544. else include $MergedFilePath;
  545. }
  546. }
  547. ?>