indent lol

This commit is contained in:
Flummi 2022-12-20 12:28:33 +01:00
parent 01819af376
commit 9f030dbb28

View File

@ -1,77 +1,76 @@
<?php <?php
class tpl { class tpl {
static $blocks = array(); static $blocks = array();
static $cache_path = '/home/users/flumm/tmp/'; static $cache_path = '/home/users/flumm/tmp/';
static function view($file, $data = array()) { static function view($file, $data = array()) {
$cached_file = self::cache($file); $cached_file = self::cache($file);
extract($data, EXTR_SKIP); extract($data, EXTR_SKIP);
require $cached_file; require $cached_file;
unlink($cached_file); unlink($cached_file);
} }
static function cache($file) { static function cache($file) {
if(!file_exists(self::$cache_path)) if(!file_exists(self::$cache_path))
mkdir(self::$cache_path, 0744); mkdir(self::$cache_path, 0744);
$cached_file = self::$cache_path . str_replace(array('/', '.html'), array('_', ''), $file . '.php'); $cached_file = self::$cache_path . str_replace(array('/', '.html'), array('_', ''), $file . '.php');
if(!file_exists($cached_file) || filemtime($cached_file) < filemtime($file)) { if(!file_exists($cached_file) || filemtime($cached_file) < filemtime($file)) {
$code = self::includeFiles($file); $code = self::includeFiles($file);
$code = self::compileCode($code); $code = self::compileCode($code);
file_put_contents($cached_file, '<?php class_exists(\'' . __CLASS__ . '\') or exit; ?>' . PHP_EOL . $code); file_put_contents($cached_file, '<?php class_exists(\'' . __CLASS__ . '\') or exit; ?>' . PHP_EOL . $code);
} }
return $cached_file; return $cached_file;
} }
static function compileCode($code) { static function compileCode($code) {
$code = self::compileBlock($code); $code = self::compileBlock($code);
$code = self::compileYield($code); $code = self::compileYield($code);
$code = self::compileEscapedEchos($code); $code = self::compileEscapedEchos($code);
$code = self::compileEchos($code); $code = self::compileEchos($code);
$code = self::compilePHP($code); $code = self::compilePHP($code);
return $code; return $code;
} }
static function includeFiles($file) { static function includeFiles($file) {
$code = file_get_contents($file); $code = file_get_contents($file);
preg_match_all('/{% ?(extends|include) ?\'?(.*?)\'? ?%}/i', $code, $matches, PREG_SET_ORDER); preg_match_all('/{% ?(extends|include) ?\'?(.*?)\'? ?%}/i', $code, $matches, PREG_SET_ORDER);
foreach($matches as $value) foreach($matches as $value)
$code = str_replace($value[0], self::includeFiles($value[2]), $code); $code = str_replace($value[0], self::includeFiles($value[2]), $code);
$code = preg_replace('/{% ?(extends|include) ?\'?(.*?)\'? ?%}/i', '', $code); $code = preg_replace('/{% ?(extends|include) ?\'?(.*?)\'? ?%}/i', '', $code);
return $code; return $code;
} }
static function compilePHP($code) { static function compilePHP($code) {
return preg_replace('~\{%\s*(.+?)\s*\%}~is', '<?php $1 ?>', $code); return preg_replace('~\{%\s*(.+?)\s*\%}~is', '<?php $1 ?>', $code);
} }
static function compileEchos($code) { static function compileEchos($code) {
return preg_replace('~\{{\s*(.+?)\s*\}}~is', '<?php echo $1 ?>', $code); return preg_replace('~\{{\s*(.+?)\s*\}}~is', '<?php echo $1 ?>', $code);
} }
static function compileEscapedEchos($code) { static function compileEscapedEchos($code) {
return preg_replace('~\{{{\s*(.+?)\s*\}}}~is', '<?php echo htmlentities($1, ENT_QUOTES, \'UTF-8\') ?>', $code); return preg_replace('~\{{{\s*(.+?)\s*\}}}~is', '<?php echo htmlentities($1, ENT_QUOTES, \'UTF-8\') ?>', $code);
} }
static function compileBlock($code) { static function compileBlock($code) {
preg_match_all('/{% ?block ?(.*?) ?%}(.*?){% ?endblock ?%}/is', $code, $matches, PREG_SET_ORDER); preg_match_all('/{% ?block ?(.*?) ?%}(.*?){% ?endblock ?%}/is', $code, $matches, PREG_SET_ORDER);
foreach($matches as $value) { foreach($matches as $value) {
if(!array_key_exists($value[1], self::$blocks)) if(!array_key_exists($value[1], self::$blocks))
self::$blocks[$value[1]] = ''; self::$blocks[$value[1]] = '';
if(strpos($value[2], '@parent') === false) if(strpos($value[2], '@parent') === false)
self::$blocks[$value[1]] = $value[2]; self::$blocks[$value[1]] = $value[2];
else else
self::$blocks[$value[1]] = str_replace('@parent', self::$blocks[$value[1]], $value[2]); self::$blocks[$value[1]] = str_replace('@parent', self::$blocks[$value[1]], $value[2]);
$code = str_replace($value[0], '', $code); $code = str_replace($value[0], '', $code);
} }
return $code; return $code;
} }
static function compileYield($code) { static function compileYield($code) {
foreach(self::$blocks as $block => $value) { foreach(self::$blocks as $block => $value)
$code = preg_replace('/{% ?yield ?' . $block . ' ?%}/', $value, $code); $code = preg_replace('/{% ?yield ?' . $block . ' ?%}/', $value, $code);
} $code = preg_replace('/{% ?yield ?(.*?) ?%}/i', '', $code);
$code = preg_replace('/{% ?yield ?(.*?) ?%}/i', '', $code); return $code;
return $code; }
}
} }
?> ?>