From: Andrey Kutejko Date: Tue, 30 Jul 2013 20:08:56 +0000 (+0300) Subject: make all modifiers autoloadable X-Git-Tag: 0.5~100 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=31d6564b34a4229f58d0f02fb8fe56b5f2a0fb7f;p=ipf.git make all modifiers autoloadable --- diff --git a/ipf/template.php b/ipf/template.php index fd9b27b..0c40fee 100644 --- a/ipf/template.php +++ b/ipf/template.php @@ -36,24 +36,3 @@ abstract class IPF_Template } } -function IPF_Template_dateFormat($date, $format='%b %e, %Y') -{ - if (substr(PHP_OS,0,3) == 'WIN') { - $_win_from = array ('%e', '%T', '%D'); - $_win_to = array ('%#d', '%H:%M:%S', '%m/%d/%y'); - $format = str_replace($_win_from, $_win_to, $format); - } - $date = date('Y-m-d H:i:s', strtotime($date.' GMT')); - return strftime($format, strtotime($date)); -} - -function IPF_Template_timeFormat($time, $format='Y-m-d H:i:s') -{ - return date($format, $time); -} - -function IPF_Template_floatFormat($number, $decimals=2, $dec_point='.', $thousands_sep=',') -{ - return number_format($number, $decimals, $dec_point, $thousands_sep); -} - diff --git a/ipf/template/compiler.php b/ipf/template/compiler.php index c258fa9..195408e 100644 --- a/ipf/template/compiler.php +++ b/ipf/template/compiler.php @@ -32,7 +32,7 @@ class IPF_Template_Compiler 'upper' => 'strtoupper', 'lower' => 'strtolower', 'escxml' => 'htmlspecialchars', - 'escape' => 'IPF_Utils::escape', + 'escape' => 'IPF_Template_Modifier::escape', 'strip_tags' => 'strip_tags', 'escurl' => 'rawurlencode', 'capitalize' => 'ucwords', @@ -43,16 +43,15 @@ class IPF_Template_Compiler 'trim' => 'trim', 'unsafe' => 'IPF_Template_SafeString::markSafe', 'safe' => 'IPF_Template_SafeString::markSafe', - 'date' => 'IPF_Template_dateFormat', - 'time' => 'IPF_Template_timeFormat', - 'floatformat' => 'IPF_Template_floatFormat', - 'limit_words' => 'IPF_Utils::limitWords', + 'date' => 'IPF_Template_Modifier::dateFormat', + 'time' => 'IPF_Template_Modifier::timeFormat', + 'floatformat' => 'IPF_Template_Modifier::floatFormat', + 'limit_words' => 'IPF_Template_Modifier::limitWords', + 'limit_chars' => 'IPF_Template_Modifier::limitCharacters', ); protected $_literals; - public $_usedModifiers = array(); - protected $_blockStack = array(); protected $_transStack = array(); @@ -95,13 +94,6 @@ class IPF_Template_Compiler public function getCompiledTemplate() { $result = $this->compile(); - if (count($this->_usedModifiers)) { - $code = array(); - foreach ($this->_usedModifiers as $modifier) { - $code[] = 'IPF::loadFunction(\''.$modifier.'\'); '; - } - $result = ''.$result; - } $result = str_replace(array('?>', ''), '', $result); $result = str_replace("?>\n", "?>\n\n", $result); return $result; @@ -156,7 +148,6 @@ class IPF_Template_Compiler $compiler = clone($this); $compiler->templateContent = substr($this->templateContent, $blocks[$i]['start'], $blocks[$i]['finish'] - $blocks[$i]['start']); $_tmp = $compiler->compile(); - $this->updateModifierStack($compiler); if (!isset($this->_extendBlocks[$blockName])) { $this->_extendBlocks[$blockName] = $_tmp; } else { @@ -233,10 +224,6 @@ class IPF_Template_Compiler } else { $res = $modifier.'('.$res.')'; } - - if (!in_array($modifier, $this->_usedModifiers)) { - $this->_usedModifiers[] = $modifier; - } } return $res; } @@ -389,7 +376,6 @@ class IPF_Template_Compiler $includedTemplateContent = $this->environment->loadTemplateFile($argfct); $_comp = new IPF_Template_Compiler($includedTemplateContent, $this->environment); $res = $_comp->compile(); - $this->updateModifierStack($_comp); break; default: $_start = true; @@ -473,15 +459,6 @@ class IPF_Template_Compiler } return $result; } - - protected function updateModifierStack($compiler) - { - foreach ($compiler->_usedModifiers as $_um) { - if (!in_array($_um, $this->_usedModifiers)) { - $this->_usedModifiers[] = $_um; - } - } - } } IPF_Template_Compiler::init(); diff --git a/ipf/template/modifier.php b/ipf/template/modifier.php new file mode 100644 index 0000000..0c837da --- /dev/null +++ b/ipf/template/modifier.php @@ -0,0 +1,82 @@ += $n) { + $out = trim($out); + return (strlen($out) == strlen($str)) ? $out : $out.$end_char; + } + } + } +} + diff --git a/ipf/utils.php b/ipf/utils.php index e642ff6..b239d9a 100644 --- a/ipf/utils.php +++ b/ipf/utils.php @@ -262,11 +262,6 @@ class IPF_Utils return $html; } - public static function escape($string) - { - return htmlspecialchars((string)$string, ENT_COMPAT, 'UTF-8'); - } - static function moneyFormat($val) { return number_format((float)$val,2); @@ -278,56 +273,5 @@ class IPF_Utils return strtolower(preg_replace('/[^A-Z^a-z^0-9^\/\_]+/', '-', $slug)); return $slug; } - - /** - * Word Limiter - * - * Limits a string to X number of words. - * - * @param string - * @param integer - * @param string the end character. Usually an ellipsis - * @return string - */ - public static function limitWords($str, $limit=100, $end_char='…') - { - if (trim($str) == '') - return $str; - preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); - if (strlen($str) == strlen($matches[0])) - $end_char = ''; - return rtrim($matches[0]).$end_char; - } - - /** - * Character Limiter - * - * Limits the string based on the character count. Preserves complete words - * so the character count may not be exactly as specified. - * - * @param string - * @param integer - * @param string the end character. Usually an ellipsis - * @return string - */ - function limitCharacters($str, $n=500, $end_char='…') - { - if (strlen($str) < $n) - return $str; - - $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); - - if (strlen($str) <= $n) - return $str; - - $out = ""; - foreach (explode(' ', trim($str)) as $val) { - $out .= $val.' '; - if (strlen($out) >= $n) { - $out = trim($out); - return (strlen($out) == strlen($str)) ? $out : $out.$end_char; - } - } - } }