- changed internals to use Smarty::$_MBSTRING ($_CHARSET, $_DATE_FORMAT) for better unit testing

This commit is contained in:
rodneyrehm
2011-12-18 18:20:09 +00:00
parent 23a924cebe
commit d645ad2ea1
22 changed files with 69 additions and 55 deletions

View File

@@ -4,6 +4,7 @@
- added chaining to Smarty_Internal_Templatebase
- changed unloadFilter() to not return a boolean in favor of chaining and API conformity
- bugfix unregisterObject() raised notice when object to unregister did not exist
- changed internals to use Smarty::$_MBSTRING ($_CHARSET, $_DATE_FORMAT) for better unit testing
17.12.2011
- improvement of compiling speed by new handling of plain text blocks in the lexer/parser (issue 68)

View File

@@ -167,6 +167,11 @@ class Smarty extends Smarty_Internal_TemplateBase {
*/
public static $_muted_directories = array();
public static $_MBSTRING = SMARTY_MBSTRING;
public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
public static $_UTF8_MODIFIER = 'u';
/**#@+
* variables
*/
@@ -584,7 +589,7 @@ class Smarty extends Smarty_Internal_TemplateBase {
// selfpointer needed by some other class methods
$this->smarty = $this;
if (is_callable('mb_internal_encoding')) {
mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
mb_internal_encoding(Smarty::$_CHARSET);
}
$this->start_time = microtime(true);
// set default dirs
@@ -1432,6 +1437,11 @@ class Smarty extends Smarty_Internal_TemplateBase {
}
}
// let PREG treat strings as ISO-8859-1 if we're not dealing with UTF-8
if (Smarty::$_CHARSET !== 'UTF-8') {
Smarty::$_UTF8_MODIFIER = '';
}
/**
* Smarty exception class
* @package Smarty

View File

@@ -90,7 +90,7 @@ function smarty_block_textformat($params, $content, $template, &$repeat)
$_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
}
// wordwrap sentences
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
$_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
} else {

View File

@@ -24,23 +24,23 @@
*/
function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false)
{
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
if ($lc_rest) {
// uppercase (including hyphenated words)
$upper_string = mb_convert_case( $string, MB_CASE_TITLE, SMARTY_RESOURCE_CHAR_SET );
$upper_string = mb_convert_case( $string, MB_CASE_TITLE, Smarty::$_CHARSET );
} else {
// uppercase word breaks
$upper_string = preg_replace("!(^|[^\p{L}'])([\p{Ll}])!ueS", "stripslashes('\\1').mb_convert_case(stripslashes('\\2'),MB_CASE_UPPER, SMARTY_RESOURCE_CHAR_SET)", $string);
$upper_string = preg_replace("!(^|[^\p{L}'])([\p{Ll}])!ueS", "stripslashes('\\1').mb_convert_case(stripslashes('\\2'),MB_CASE_UPPER, '" . addslashes(Smarty::$_CHARSET) . "')", $string);
}
// check uc_digits case
if (!$uc_digits) {
if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!u", $string, $matches, PREG_OFFSET_CAPTURE)) {
foreach($matches[1] as $match) {
$upper_string = substr_replace($upper_string, mb_strtolower($match[0], SMARTY_RESOURCE_CHAR_SET), $match[1], strlen($match[0]));
$upper_string = substr_replace($upper_string, mb_strtolower($match[0], Smarty::$_CHARSET), $match[1], strlen($match[0]));
}
}
}
$upper_string = preg_replace("!((^|\s)['\"])(\w)!ue", "stripslashes('\\1').mb_convert_case(stripslashes('\\3'),MB_CASE_UPPER, SMARTY_RESOURCE_CHAR_SET)", $upper_string);
$upper_string = preg_replace("!((^|\s)['\"])(\w)!ue", "stripslashes('\\1').mb_convert_case(stripslashes('\\3'),MB_CASE_UPPER, '" . addslashes(Smarty::$_CHARSET) . "')", $upper_string);
return $upper_string;
}

View File

@@ -26,8 +26,11 @@
* @return string |void
* @uses smarty_make_timestamp()
*/
function smarty_modifier_date_format($string, $format = SMARTY_RESOURCE_DATE_FORMAT, $default_date = '',$formatter='auto')
function smarty_modifier_date_format($string, $format=null, $default_date='', $formatter='auto')
{
if ($format === null) {
$format = Smarty::$_DATE_FORMAT;
}
/**
* Include the {@link shared.make_timestamp.php} plugin
*/

View File

@@ -70,9 +70,9 @@ function smarty_modifier_debug_print_var ($var, $depth = 0, $length = 40)
case 'string' :
$results = strtr($var, $_replace);
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (mb_strlen($var, SMARTY_RESOURCE_CHAR_SET) > $length) {
$results = mb_substr($var, 0, $length - 3, SMARTY_RESOURCE_CHAR_SET) . '...';
if (Smarty::$_MBSTRING) {
if (mb_strlen($var, Smarty::$_CHARSET) > $length) {
$results = mb_substr($var, 0, $length - 3, Smarty::$_CHARSET) . '...';
}
} else {
if (isset($var[$length])) {
@@ -86,9 +86,9 @@ function smarty_modifier_debug_print_var ($var, $depth = 0, $length = 40)
case 'unknown type' :
default :
$results = strtr((string) $var, $_replace);
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (mb_strlen($results, SMARTY_RESOURCE_CHAR_SET) > $length) {
$results = mb_substr($results, 0, $length - 3, SMARTY_RESOURCE_CHAR_SET) . '...';
if (Smarty::$_MBSTRING) {
if (mb_strlen($results, Smarty::$_CHARSET) > $length) {
$results = mb_substr($results, 0, $length - 3, Smarty::$_CHARSET) . '...';
}
} else {
if (strlen($results) > $length) {

View File

@@ -24,7 +24,7 @@
function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true)
{
if (!$char_set) {
$char_set = SMARTY_RESOURCE_CHAR_SET;
$char_set = Smarty::$_CHARSET;
}
switch ($esc_type) {
@@ -32,7 +32,7 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
case 'htmlall':
if (SMARTY_MBSTRING /* ^phpunit */ && empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
// mb_convert_encoding ignores htmlspecialchars()
$string = htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
// htmlentities() won't convert everything, so use mb_convert_encoding
@@ -64,10 +64,10 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
case 'hexentity':
$return = '';
if (SMARTY_MBSTRING /* ^phpunit */ && empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
$return = '';
foreach (smarty_mb_to_unicode($string, SMARTY_RESOURCE_CHAR_SET) as $unicode) {
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
$return .= '&#x' . strtoupper(dechex($unicode)) . ';';
}
return $return;
@@ -81,10 +81,10 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
case 'decentity':
$return = '';
if (SMARTY_MBSTRING /* ^phpunit */ && empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
$return = '';
foreach (smarty_mb_to_unicode($string, SMARTY_RESOURCE_CHAR_SET) as $unicode) {
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
$return .= '&#' . $unicode . ';';
}
return $return;
@@ -101,7 +101,7 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
case 'mail':
if (SMARTY_MBSTRING /* ^phpunit */ && empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
}
@@ -111,9 +111,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
case 'nonstd':
// escape non-standard chars, such as ms document quotes
$return = '';
if (SMARTY_MBSTRING /* ^phpunit */ && empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
foreach (smarty_mb_to_unicode($string, SMARTY_RESOURCE_CHAR_SET) as $unicode) {
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
if ($unicode >= 126) {
$return .= '&#' . $unicode . ';';
} else {

View File

@@ -22,7 +22,7 @@
*/
function smarty_modifier_replace($string, $search, $replace)
{
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
return smarty_mb_str_replace($search, $replace, $string);
}

View File

@@ -28,16 +28,16 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_wo
if ($length == 0)
return '';
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (mb_strlen($string, SMARTY_RESOURCE_CHAR_SET) > $length) {
$length -= min($length, mb_strlen($etc, SMARTY_RESOURCE_CHAR_SET));
if (Smarty::$_MBSTRING) {
if (mb_strlen($string, Smarty::$_CHARSET) > $length) {
$length -= min($length, mb_strlen($etc, Smarty::$_CHARSET));
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1, SMARTY_RESOURCE_CHAR_SET));
$string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1, Smarty::$_CHARSET));
}
if (!$middle) {
return mb_substr($string, 0, $length, SMARTY_RESOURCE_CHAR_SET) . $etc;
return mb_substr($string, 0, $length, Smarty::$_CHARSET) . $etc;
}
return mb_substr($string, 0, $length / 2, SMARTY_RESOURCE_CHAR_SET) . $etc . mb_substr($string, - $length / 2, $length, SMARTY_RESOURCE_CHAR_SET);
return mb_substr($string, 0, $length / 2, Smarty::$_CHARSET) . $etc . mb_substr($string, - $length / 2, $length, Smarty::$_CHARSET);
}
return $string;
}

View File

@@ -23,8 +23,8 @@ function smarty_modifiercompiler_count_characters($params, $compiler)
if (!isset($params[1]) || $params[1] != 'true') {
return 'preg_match_all(\'/[^\s]/u\',' . $params[0] . ', $tmp)';
}
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
return 'mb_strlen(' . $params[0] . ', SMARTY_RESOURCE_CHAR_SET)';
if (Smarty::$_MBSTRING) {
return 'mb_strlen(' . $params[0] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')';
}
// no MBString fallback
return 'strlen(' . $params[0] . ')';

View File

@@ -20,7 +20,7 @@
*/
function smarty_modifiercompiler_count_words($params, $compiler)
{
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
// return 'preg_match_all(\'#[\w\pL]+#u\', ' . $params[0] . ', $tmp)';
// expression taken from http://de.php.net/manual/en/function.str-word-count.php#85592
return 'preg_match_all(\'/\p{L}[\p{L}\p{Mn}\p{Pd}\\\'\x{2019}]*/u\', ' . $params[0] . ', $tmp)';

View File

@@ -27,11 +27,11 @@ function smarty_modifiercompiler_escape($params, $compiler)
{
try {
$esc_type = smarty_literal_compiler_param($params, 1, 'html');
$char_set = smarty_literal_compiler_param($params, 2, SMARTY_RESOURCE_CHAR_SET);
$char_set = smarty_literal_compiler_param($params, 2, Smarty::$_CHARSET);
$double_encode = smarty_literal_compiler_param($params, 3, true);
if (!$char_set) {
$char_set = SMARTY_RESOURCE_CHAR_SET;
$char_set = Smarty::$_CHARSET;
}
switch ($esc_type) {
@@ -42,7 +42,7 @@ function smarty_modifiercompiler_escape($params, $compiler)
. var_export($double_encode, true) . ')';
case 'htmlall':
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
return 'mb_convert_encoding(htmlspecialchars('
. $params[0] .', ENT_QUOTES, '
. var_export($char_set, true) . ', '

View File

@@ -19,7 +19,7 @@
*/
function smarty_modifiercompiler_from_charset($params, $compiler)
{
if (!SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (!Smarty::$_MBSTRING) {
// FIXME: (rodneyrehm) shouldn't this throw an error?
return $params[0];
}
@@ -28,7 +28,7 @@ function smarty_modifiercompiler_from_charset($params, $compiler)
$params[1] = '"ISO-8859-1"';
}
return 'mb_convert_encoding(' . $params[0] . ', SMARTY_RESOURCE_CHAR_SET, ' . $params[1] . ')';
return 'mb_convert_encoding(' . $params[0] . ', "' . addslashes(Smarty::$_CHARSET) . '", ' . $params[1] . ')';
}
?>

View File

@@ -21,8 +21,8 @@
function smarty_modifiercompiler_lower($params, $compiler)
{
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
return 'mb_strtolower(' . $params[0] . ',SMARTY_RESOURCE_CHAR_SET)' ;
if (Smarty::$_MBSTRING) {
return 'mb_strtolower(' . $params[0] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')' ;
}
// no MBString fallback
return 'strtolower(' . $params[0] . ')';

View File

@@ -19,7 +19,7 @@
*/
function smarty_modifiercompiler_to_charset($params, $compiler)
{
if (!SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (!Smarty::$_MBSTRING) {
// FIXME: (rodneyrehm) shouldn't this throw an error?
return $params[0];
}
@@ -28,7 +28,7 @@ function smarty_modifiercompiler_to_charset($params, $compiler)
$params[1] = '"ISO-8859-1"';
}
return 'mb_convert_encoding(' . $params[0] . ', ' . $params[1] . ', SMARTY_RESOURCE_CHAR_SET)';
return 'mb_convert_encoding(' . $params[0] . ', ' . $params[1] . ', "' . addslashes(Smarty::$_CHARSET) . '")';
}
?>

View File

@@ -23,7 +23,7 @@ function smarty_modifiercompiler_unescape($params, $compiler)
$params[1] = 'html';
}
if (!isset($params[2])) {
$params[2] = "SMARTY_RESOURCE_CHAR_SET";
$params[2] = '\'' . addslashes(Smarty::$_CHARSET) . '\'';
} else {
$params[2] = "'" . $params[2] . "'";
}
@@ -32,7 +32,7 @@ function smarty_modifiercompiler_unescape($params, $compiler)
case 'entity':
return 'mb_convert_encoding(' . $params[0] . ', ' . $params[2] . ', \'HTML-ENTITIES\')';
case 'htmlall':
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
return 'mb_convert_encoding(' . $params[0] . ', ' . $params[2] . ', \'HTML-ENTITIES\')';
}
return 'html_entity_decode(' . $params[0] . ', ENT_QUOTES, ' . $params[2] . ')';

View File

@@ -20,8 +20,8 @@
*/
function smarty_modifiercompiler_upper($params, $compiler)
{
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
return 'mb_strtoupper(' . $params[0] . ',SMARTY_RESOURCE_CHAR_SET)' ;
if (Smarty::$_MBSTRING) {
return 'mb_strtoupper(' . $params[0] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')' ;
}
// no MBString fallback
return 'strtoupper(' . $params[0] . ')';

View File

@@ -30,7 +30,7 @@ function smarty_modifiercompiler_wordwrap($params, $compiler)
$params[3] = 'false';
}
$function = 'wordwrap';
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
if (Smarty::$_MBSTRING) {
if ($compiler->tag_nocache | $compiler->nocache) {
$compiler->template->required_plugins['nocache']['wordwrap']['modifier']['file'] = SMARTY_PLUGINS_DIR .'shared.mb_wordwrap.php';
$compiler->template->required_plugins['nocache']['wordwrap']['modifier']['function'] = 'smarty_mb_wordwrap';

View File

@@ -21,7 +21,7 @@ if (version_compare(PHP_VERSION, '5.2.3', '>=')) {
function smarty_function_escape_special_chars($string)
{
if (!is_array($string)) {
$string = htmlspecialchars($string, ENT_COMPAT, SMARTY_RESOURCE_CHAR_SET, false);
$string = htmlspecialchars($string, ENT_COMPAT, Smarty::$_CHARSET, false);
}
return $string;
}

View File

@@ -28,11 +28,11 @@ if(!function_exists('smarty_mb_wordwrap')) {
$_previous = false;
foreach ($tokens as $_token) {
$token_length = mb_strlen($_token, SMARTY_RESOURCE_CHAR_SET);
$token_length = mb_strlen($_token, Smarty::$_CHARSET);
$_tokens = array($_token);
if ($token_length > $width) {
// remove last space
$t = mb_substr($t, 0, -1, SMARTY_RESOURCE_CHAR_SET);
$t = mb_substr($t, 0, -1, Smarty::$_CHARSET);
$_previous = false;
$length = 0;
@@ -45,13 +45,13 @@ if(!function_exists('smarty_mb_wordwrap')) {
foreach ($_tokens as $token) {
$_space = !!preg_match('!^\s$!uS', $token);
$token_length = mb_strlen($token, SMARTY_RESOURCE_CHAR_SET);
$token_length = mb_strlen($token, Smarty::$_CHARSET);
$length += $token_length;
if ($length > $width) {
// remove space before inserted break
if ($_previous && $token_length < $width) {
$t = mb_substr($t, 0, -1, SMARTY_RESOURCE_CHAR_SET);
$t = mb_substr($t, 0, -1, Smarty::$_CHARSET);
}
// add the break before the token

View File

@@ -15,7 +15,7 @@
*/
function smarty_variablefilter_htmlspecialchars($source, $smarty)
{
return htmlspecialchars($source, ENT_QUOTES, SMARTY_RESOURCE_CHAR_SET);
return htmlspecialchars($source, ENT_QUOTES, Smarty::$_CHARSET);
}
?>

View File

@@ -83,7 +83,7 @@ class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_C
}
// autoescape html
if ($compiler->template->smarty->escape_html) {
$output = "htmlspecialchars({$output}, ENT_QUOTES, SMARTY_RESOURCE_CHAR_SET)";
$output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
}
// loop over registerd filters
if (!empty($compiler->template->smarty->registered_filters[Smarty::FILTER_VARIABLE])) {