LightweightTemplate.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace KarmaFW\Templates;
  3. use \KarmaFW\App;
  4. class LightweightTemplate {
  5. // https://codeshack.io/lightweight-template-engine-php/
  6. static $blocks = array();
  7. static $cache_path = TPL_CACHE_DIR; // APP_DIR . '/var/cache/templates';
  8. static $tpl_path = TPL_DIR;
  9. static $cache_enabled = (ENV == 'prod') || true;
  10. static $tpl_last_updated = null;
  11. protected $data = [];
  12. public function __construct($tpl_path=null, $variables=[], $layout=null)
  13. {
  14. $this->data = $variables;
  15. }
  16. public function assign($k, $v=null)
  17. {
  18. if (is_array($k)) {
  19. $keys = $k;
  20. foreach ($keys as $k => $v) {
  21. $this->assign($k, $v);
  22. }
  23. } else {
  24. $this->data[$k] = $v;
  25. }
  26. }
  27. public function getVariables()
  28. {
  29. return $this->data;
  30. }
  31. public function getVar($var_name, $default_value=null)
  32. {
  33. return isset($this->data[$var_name]) ? $this->data[$var_name] : $default_value;
  34. }
  35. public function fetch($tpl=null, $extra_vars=[], $layout=null, $options=[])
  36. {
  37. ob_start();
  38. $this->display($tpl, $extra_vars, $layout, $options);
  39. $content = ob_get_contents();
  40. ob_end_clean();
  41. return $content;
  42. }
  43. public function display($tpl=null, $extra_vars=[], $layout=null, $options=[])
  44. {
  45. $tpl_data = $this->data + $extra_vars;
  46. self::view($tpl, $tpl_data);
  47. return true;
  48. }
  49. public static function view($file, $data = array()) {
  50. $cached_file = self::cache($file);
  51. extract($data, EXTR_SKIP);
  52. require $cached_file;
  53. }
  54. protected static function cache($file) {
  55. if (!file_exists(self::$cache_path)) {
  56. if (! @mkdir(self::$cache_path, 0744)) {
  57. throw new \Exception("Cannot create templates cache dir " . self::$cache_path, 1);
  58. }
  59. }
  60. $cached_file = self::$cache_path . '/' . str_replace(array('/', '.html'), array('_', ''), $file . '.php');
  61. $cached_file_exists = is_file($cached_file);
  62. if ($cached_file_exists) {
  63. $cached_file_updated = filemtime($cached_file);
  64. self::$tpl_last_updated = filemtime(self::$tpl_path . '/' . $file);
  65. } else {
  66. $cached_file_updated = null;
  67. }
  68. if (ENV == 'dev') {
  69. // on force le parcours de tous les fichiers inclus pour avoir la vraie valeur de self::$tpl_last_updated
  70. $code = self::includeFiles($file);
  71. }
  72. if (!self::$cache_enabled || ! $cached_file_exists || $cached_file_updated < self::$tpl_last_updated) {
  73. if (! isset($code)) {
  74. $code = self::includeFiles($file);
  75. }
  76. $code = self::compileCode($code);
  77. file_put_contents($cached_file, '<?php class_exists(\'' . __CLASS__ . '\') or exit; ?>' . PHP_EOL . $code);
  78. } else {
  79. //header('X-Template: cached'); // TODO: $response->addHeader(...)
  80. $debugbar = App::getData('debugbar');
  81. if ($debugbar) {
  82. if (isset($debugbar['templates'])) {
  83. $debugbar_message_idx = $debugbar['templates']->addMessage([
  84. 'tpl' => $cached_file,
  85. 'content_length' => filesize($cached_file),
  86. 'content_length_str' => formatSize(filesize($cached_file)),
  87. 'cached' => true,
  88. ]);
  89. }
  90. }
  91. }
  92. return $cached_file;
  93. }
  94. public static function clearCache() {
  95. foreach(glob(self::$cache_path . '/*') as $file) {
  96. unlink($file);
  97. }
  98. }
  99. protected static function compileCode($code) {
  100. $code = self::compileBlock($code);
  101. $code = self::compileYield($code);
  102. $code = self::compileModules($code);
  103. $code = self::compileEscapedEchos($code);
  104. $code = self::compileEchos($code);
  105. //$code = self::compilePHP($code);
  106. return $code;
  107. }
  108. protected static function includeFiles($file, $level=0, $caller_file=null, $parent_file=null) {
  109. if (! is_file(self::$tpl_path . '/' . $file)) {
  110. throw new \Exception("Template file not found " . $file, 500);
  111. }
  112. $code = file_get_contents(self::$tpl_path . '/' . $file);
  113. $code_init = $code;
  114. $layout = null;
  115. $ts_update_file = filectime(self::$tpl_path . '/' . $file);
  116. if (empty(self::$tpl_last_updated) || self::$tpl_last_updated < $ts_update_file) {
  117. self::$tpl_last_updated = $ts_update_file;
  118. }
  119. static $tpl_idx = null;
  120. if (is_null($tpl_idx) || (empty($caller_file) && empty($parent_file))) {
  121. $tpl_idx = 0;
  122. }
  123. $tpl_idx++;
  124. $debugbar = App::getData('debugbar');
  125. if ($debugbar) {
  126. if (isset($debugbar['templates'])) {
  127. $debugbar_message_idx = $debugbar['templates']->addMessage([
  128. 'tpl' => $file,
  129. ]);
  130. }
  131. }
  132. $ts_start = microtime(true);
  133. // Layout (1)
  134. preg_match_all('/{layout ?\'?(.*?)\'? ?}/i', $code, $layout_matches, PREG_SET_ORDER);
  135. if ($layout_matches) {
  136. $value = $layout_matches[0];
  137. $layout = $value[1];
  138. } else {
  139. $layout = null;
  140. }
  141. if (defined('ENV') && ENV == 'dev') {
  142. $tpl_infos = '';
  143. if ($caller_file) {
  144. $tpl_infos .= ' layout for ' . $caller_file . '';
  145. }
  146. if ($parent_file) {
  147. $tpl_infos .= ' child of ' . $parent_file . '';
  148. }
  149. if ($layout) {
  150. $tpl_infos .= ' with layout ' . $layout . '';
  151. }
  152. $begin = '<!-- [' . $level . '] BEGIN TEMPLATE #' . $tpl_idx . ' : ' . $file . ' (size: ' . formatSize(strlen($code)) . ' - ' . $tpl_infos . ') -->';
  153. $end = '<!-- [' . $level . '] END TEMPLATE #' . $tpl_idx . ' : ' . $file . ' (size: ' . formatSize(strlen($code)) . ' - ' . $tpl_infos . ') -->';
  154. $code = PHP_EOL . $begin . PHP_EOL . $code . PHP_EOL . $end . PHP_EOL;
  155. }
  156. // Layout (2)
  157. if ($layout_matches) {
  158. $value = $layout_matches[0];
  159. $layout = $value[1];
  160. $layout_code = self::includeFiles($layout, $level-1, $file);
  161. $code = str_replace($value[0], '', $code);
  162. $layout_code = str_replace('<' . '?=$child_content?' . '>', '{@content}', $layout_code);
  163. $layout_code = str_replace('{$child_content}', '{@content}', $layout_code);
  164. $layout_code = str_replace('{@content}', $code, $layout_code);
  165. $code = $layout_code;
  166. }
  167. // includes
  168. preg_match_all('/{include ?\'?(.*?)\'? ?}/i', $code, $matches, PREG_SET_ORDER);
  169. foreach ($matches as $value) {
  170. $included_code = self::includeFiles($value[1], $level+1, null, $file);
  171. $code = str_replace($value[0], $included_code, $code);
  172. }
  173. $ts_end = microtime(true);
  174. $duration = $ts_end - $ts_start;
  175. if (isset($debugbar_message_idx) && ! is_null($debugbar_message_idx)) {
  176. $debugbar['templates']->updateMessage($debugbar_message_idx, [
  177. 'tpl' => $file,
  178. 'layout' => $layout,
  179. 'source_length' => strlen($code_init),
  180. 'source_length_str' => formatSize(strlen($code_init)),
  181. 'content_length' => strlen($code),
  182. 'content_length_str' => formatSize(strlen($code)),
  183. 'duration' => round($duration, 6),
  184. 'duration_str' => formatDuration($duration),
  185. 'level' => $level,
  186. ]);
  187. }
  188. return $code;
  189. }
  190. protected static function compilePHP($code) {
  191. /* return preg_replace('~\{%\s*(.+?)\s*\%}~is', '<?php $1 ?>', $code); */
  192. return $code;
  193. }
  194. protected static function compileModules($code) {
  195. // url => {url clients_list}
  196. $code = preg_replace('/{routeUrl /', '{url ', $code); // for compatibility with old templates
  197. preg_match_all('~{url (.*?)}~is', $code, $matches, PREG_SET_ORDER);
  198. foreach ($matches as $value) {
  199. //pre($matches); exit;
  200. $code = str_replace($value[0], getRouteUrl($value[1]), $code);
  201. }
  202. // foreach => {foreach $list as $item}<div>...</div>{/foreach}
  203. preg_match_all('~{foreach (.*?) ?}(.*?){/foreach}~is', $code, $matches, PREG_SET_ORDER);
  204. foreach ($matches as $value) {
  205. $replaced = PHP_EOL . '<' . '?php foreach (' . $value[1] . ') : ?' . '>' . PHP_EOL . $value[2] . PHP_EOL . '<' . '?php endforeach; ?' . '>';
  206. $code = str_replace($value[0], $replaced, $code);
  207. }
  208. // if => {if $item}<div>...</div>{/if}
  209. preg_match_all('~{if (.*?) ?}(.*?){/if}~is', $code, $matches, PREG_SET_ORDER);
  210. foreach ($matches as $value) {
  211. $replaced = '<' . '?php elseif ( $1 ) : ?' . '>';
  212. $value[2] = preg_replace('/{elseif (.*?) ?}/', $replaced, $value[2]);
  213. $replaced = PHP_EOL . '<' . '?php if (' . $value[1] . ') : ?' . '>' . PHP_EOL . $value[2] . PHP_EOL . '<' . '?php endif; ?' . '>';
  214. $code = str_replace($value[0], $replaced, $code);
  215. }
  216. return $code;
  217. }
  218. protected static function compileEchos($code, $strict=true) {
  219. // compile PHP variables (method 1) => {$my_var}
  220. if ($strict) {
  221. $code = preg_replace('~\{\$(.+?)}~is', '<?php echo \$$1 ?>', $code);
  222. } else {
  223. $code = preg_replace('~\{\$(.+?)}~is', '<?php echo isset(\$$1) ? (\$$1) : ""; ?>', $code);
  224. }
  225. // compile PHP variables (method 2) => {{ $my_var }}
  226. return preg_replace('~\{{\s*(.+?)\s*\}}~is', '<?php echo $1 ?>', $code);
  227. }
  228. protected static function compileEscapedEchos($code) {
  229. // compile PHP escaped variables => {{{ $my_var }}}
  230. return preg_replace('~\{{{\s*(.+?)\s*\}}}~is', '<?php echo htmlentities($1, ENT_QUOTES, \'UTF-8\') ?>', $code);
  231. }
  232. protected static function compileBlock($code) {
  233. preg_match_all('~{block ?(.*?) ?}(.*?){/block}~is', $code, $matches, PREG_SET_ORDER);
  234. foreach ($matches as $value) {
  235. if (!array_key_exists($value[1], self::$blocks)) self::$blocks[$value[1]] = '';
  236. if (strpos($value[2], '@parent') === false) {
  237. self::$blocks[$value[1]] = $value[2];
  238. } else {
  239. self::$blocks[$value[1]] = str_replace('@parent', self::$blocks[$value[1]], $value[2]);
  240. }
  241. $code = str_replace($value[0], '', $code);
  242. }
  243. return $code;
  244. }
  245. protected static function compileYield($code) {
  246. // compile yields => {yield my_block}
  247. foreach(self::$blocks as $block => $value) {
  248. $code = preg_replace('/{yield ' . $block . ' ?}/', $value, $code);
  249. }
  250. $code = preg_replace('/{yield ?(.*?) ?}/i', '', $code);
  251. return $code;
  252. }
  253. }