diff --git a/TODO.txt b/TODO.txt new file mode 100644 index 00000000..af24694a --- /dev/null +++ b/TODO.txt @@ -0,0 +1,2 @@ +- [ ] find ->ext-> calls, rewrite and remove first function call param +- [ ] review usages of ->_getSmartyObj and ->smarty: maybe change this so we can hide more \ No newline at end of file diff --git a/src/Compile/PrivateSpecialVariable.php b/src/Compile/PrivateSpecialVariable.php index 9b7657f1..fd450247 100644 --- a/src/Compile/PrivateSpecialVariable.php +++ b/src/Compile/PrivateSpecialVariable.php @@ -102,9 +102,9 @@ class PrivateSpecialVariable extends Base { // no break case 'config': if (isset($_index[2])) { - return "(is_array(\$tmp = \$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])) ? \$tmp[$_index[2]] : null)"; + return "(is_array(\$tmp = \$_smarty_tpl->getConfigVariable($_index[1])) ? \$tmp[$_index[2]] : null)"; } else { - return "\$_smarty_tpl->smarty->ext->configload->_getConfigVariable(\$_smarty_tpl, $_index[1])"; + return "\$_smarty_tpl->getConfigVariable($_index[1])"; } // no break case 'ldelim': diff --git a/src/Compiler/Template.php b/src/Compiler/Template.php index a3558cc1..2babfd9c 100644 --- a/src/Compiler/Template.php +++ b/src/Compiler/Template.php @@ -533,14 +533,12 @@ class Template { // not a variable variable $var = trim($variable, '\''); $this->tag_nocache = $this->tag_nocache | - $this->template->ext->getTemplateVars->_getVariable( - $this->template, + $this->template->_getVariable( $var, null, true, false )->nocache; - // todo $this->template->compiled->properties['variables'][$var] = $this->tag_nocache | $this->nocache; } return '$_smarty_tpl->tpl_vars[' . $variable . ']->value'; } @@ -554,7 +552,7 @@ class Template { */ public function compileConfigVariable($variable) { // return '$_smarty_tpl->config_vars[' . $variable . ']'; - return '$_smarty_tpl->smarty->ext->configLoad->_getConfigVariable($_smarty_tpl, ' . $variable . ')'; + return '$_smarty_tpl->getConfigVariable(' . $variable . ')'; } /** diff --git a/src/Data.php b/src/Data.php index bd784744..6f85ea59 100644 --- a/src/Data.php +++ b/src/Data.php @@ -13,27 +13,17 @@ namespace Smarty; /** * Base class with template and variable methods - * - * @package Smarty - * @subpackage Template - * - * @property int $scope - * @property Smarty $smarty - * The following methods will be dynamically loaded by the extension handler when they are called. - * They are located in a corresponding Smarty_Internal_Method_xxxx class - * - * @method mixed _getConfigVariable(string $varName, bool $errorEnable = true) - * @method mixed getConfigVariable(string $varName, bool $errorEnable = true) - * @method mixed getConfigVars(string $varName = null, bool $searchParents = true) - * @method mixed getGlobal(string $varName = null) - * @method mixed getStreamVariable(string $variable) - * @method Data clearAssign(mixed $tpl_var) - * @method Data clearAllAssign() - * @method Data clearConfig(string $varName = null) - * @method Data configLoad(string $config_file, mixed $sections = null, string $scope = 'local') */ abstract class Data { + + /** + * Global smarty instance + * + * @var Smarty + */ + public $smarty = null; + /** * This object type (Smarty = 1, template = 2, data = 4) * @@ -129,15 +119,47 @@ abstract class Data * @param bool $nocache if true any output of this variable will * be not cached * - * @return Data|\Smarty_Internal_Template|\Smarty - *@link https://www.smarty.net/docs/en/api.append.tpl + * @return Data + * @link https://www.smarty.net/docs/en/api.append.tpl * * @api Smarty::append() */ - public function append($tpl_var, $value = null, $merge = false, $nocache = false) - { - return $this->ext->append->append($this, $tpl_var, $value, $merge, $nocache); - } + public function append($tpl_var, $value = null, $merge = false, $nocache = false) + { + if (is_array($tpl_var)) { + // $tpl_var is an array, ignore $value + foreach ($tpl_var as $_key => $_val) { + if ($_key !== '') { + $this->append($this, $_key, $_val, $merge, $nocache); + } + } + } else { + if ($tpl_var !== '' && isset($value)) { + if (!isset($this->tpl_vars[ $tpl_var ])) { + $tpl_var_inst = $this->_getVariable($tpl_var, null, true, false); + if ($tpl_var_inst instanceof Smarty_Undefined_Variable) { + $this->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache); + } else { + $this->tpl_vars[ $tpl_var ] = clone $tpl_var_inst; + } + } + if (!(is_array($this->tpl_vars[ $tpl_var ]->value) + || $this->tpl_vars[ $tpl_var ]->value instanceof ArrayAccess) + ) { + settype($this->tpl_vars[ $tpl_var ]->value, 'array'); + } + if ($merge && is_array($value)) { + foreach ($value as $_mkey => $_mval) { + $this->tpl_vars[ $tpl_var ]->value[ $_mkey ] = $_mval; + } + } else { + $this->tpl_vars[ $tpl_var ]->value[] = $value; + } + } + $this->_updateScope($tpl_var); + } + return $this; + } /** * assigns a global Smarty variable @@ -146,11 +168,19 @@ abstract class Data * @param mixed $value the value to assign * @param boolean $nocache if true any output of this variable will be not cached * - * @return Data|\Smarty_Internal_Template|\Smarty + * @return Data */ public function assignGlobal($varName, $value = null, $nocache = false) { - return $this->ext->assignGlobal->assignGlobal($this, $varName, $value, $nocache); + if ($varName !== '') { + Smarty::$global_tpl_vars[ $varName ] = new \Smarty\Variable($value, $nocache); + $ptr = $this; + while ($ptr->_isTplObj()) { + $ptr->tpl_vars[ $varName ] = clone Smarty::$global_tpl_vars[ $varName ]; + $ptr = $ptr->parent; + } + } + return $this; } /** @@ -160,12 +190,28 @@ abstract class Data * @param mixed &$value the referenced value to append * @param boolean $merge flag if array elements shall be merged * - * @return Data|\Smarty_Internal_Template|\Smarty + * @return Data */ - public function appendByRef($tpl_var, &$value, $merge = false) - { - return $this->ext->appendByRef->appendByRef($this, $tpl_var, $value, $merge); - } + public function appendByRef($tpl_var, &$value, $merge = false) + { + if ($tpl_var !== '' && isset($value)) { + if (!isset($this->tpl_vars[ $tpl_var ])) { + $this->tpl_vars[ $tpl_var ] = new \Smarty\Variable(); + } + if (!is_array($this->tpl_vars[ $tpl_var ]->value)) { + settype($this->tpl_vars[ $tpl_var ]->value, 'array'); + } + if ($merge && is_array($value)) { + foreach ($value as $_key => $_val) { + $this->tpl_vars[ $tpl_var ]->value[ $_key ] = &$value[ $_key ]; + } + } else { + $this->tpl_vars[ $tpl_var ]->value[] = &$value; + } + $this->_updateScope($tpl_var); + } + return $this; + } /** * assigns values to template variables by reference @@ -174,18 +220,26 @@ abstract class Data * @param $value * @param boolean $nocache if true any output of this variable will be not cached * - * @return Data|\Smarty_Internal_Template|\Smarty + * @return Data */ - public function assignByRef($tpl_var, &$value, $nocache = false) - { - return $this->ext->assignByRef->assignByRef($this, $tpl_var, $value, $nocache); - } + public function assignByRef($tpl_var, &$value, $nocache) + { + if ($tpl_var !== '') { + $this->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache); + $this->tpl_vars[ $tpl_var ]->value = &$value; + $this->_updateScope($tpl_var); + } + return $this; + } + + protected function _updateScope($varName, $tagScope = 0) { + // implemented in Smarty_Internal_Template only + } /** * Returns a single or all template variables * * @param string $varName variable name or null - * @param Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object * @param bool $searchParents include parent templates? * * @return mixed variable value or or array of variables @@ -195,9 +249,84 @@ abstract class Data */ public function getTemplateVars($varName = null, Data $_ptr = null, $searchParents = true) { - return $this->ext->getTemplateVars->getTemplateVars($this, $varName, $_ptr, $searchParents); + if (isset($varName)) { + $_var = $this->_getVariable($varName, $_ptr, $searchParents, false); + if (is_object($_var)) { + return $_var->value; + } else { + return null; + } + } else { + $_result = array(); + if ($_ptr === null) { + $_ptr = $this; + } + while ($_ptr !== null) { + foreach ($_ptr->tpl_vars as $key => $var) { + if (!array_key_exists($key, $_result)) { + $_result[ $key ] = $var->value; + } + } + // not found, try at parent + if ($searchParents && isset($_ptr->parent)) { + $_ptr = $_ptr->parent; + } else { + $_ptr = null; + } + } + if ($searchParents && isset(Smarty::$global_tpl_vars)) { + foreach (Smarty::$global_tpl_vars as $key => $var) { + if (!array_key_exists($key, $_result)) { + $_result[ $key ] = $var->value; + } + } + } + return $_result; + } } + /** + * gets the object of a Smarty variable + * + * @param string $varName the name of the Smarty variable + * @param Data|null $_ptr optional pointer to data object + * @param bool $searchParents search also in parent data + * @param bool $errorEnable + * + * @return Variable + */ + public function _getVariable( + $varName, + Data $_ptr = null, + $searchParents = true, + $errorEnable = true + ) { + if ($_ptr === null) { + $_ptr = $this; + } + while ($_ptr !== null) { + if (isset($_ptr->tpl_vars[ $varName ])) { + // found it, return it + return $_ptr->tpl_vars[ $varName ]; + } + // not found, try at parent + if ($searchParents && isset($_ptr->parent)) { + $_ptr = $_ptr->parent; + } else { + $_ptr = null; + } + } + if (isset(Smarty::$global_tpl_vars[ $varName ])) { + // found it, return it + return Smarty::$global_tpl_vars[ $varName ]; + } + if ($errorEnable && $this->_getSmartyObj()->error_unassigned) { + // force a notice + $x = $$varName; + } + return new Smarty_Undefined_Variable; + } + /** * Follow the parent chain an merge template and config variables * @@ -262,4 +391,229 @@ abstract class Data { return $this->ext->_callExternalMethod($this, $name, $args); } + + /** + * clear the given assigned template variable(s). + * + * @param string|array $tpl_var the template variable(s) to clear + * + * @return Data + * @link https://www.smarty.net/docs/en/api.clear.assign.tpl + * + * @api Smarty::clearAssign() + */ + public function clearAssign($tpl_var) + { + if (is_array($tpl_var)) { + foreach ($tpl_var as $curr_var) { + unset($this->tpl_vars[ $curr_var ]); + } + } else { + unset($this->tpl_vars[ $tpl_var ]); + } + return $this; + } + + /** + * clear all the assigned template variables. + * + * @return Data + * @link https://www.smarty.net/docs/en/api.clear.all.assign.tpl + * + * @api Smarty::clearAllAssign() + */ + public function clearAllAssign() + { + $this->tpl_vars = array(); + return $this; + } + + /** + * clear a single or all config variables + * + * @param string|null $name variable name or null + * + * @return Data + * @link https://www.smarty.net/docs/en/api.clear.config.tpl + * + * @api Smarty::clearConfig() + */ + public function clearConfig($name = null) + { + if (isset($name)) { + unset($this->config_vars[ $name ]); + } else { + $this->config_vars = array(); + } + return $this; + } + + /** + * load a config file, optionally load just selected sections + * + * @param string $config_file filename + * @param mixed $sections array of section names, single + * section or null + * + * @return Data + * @throws \Exception + *@api Smarty::configLoad() + * @link https://www.smarty.net/docs/en/api.config.load.tpl + * + */ + public function configLoad($config_file, $sections = null) + { + $this->_loadConfigFile($this, $config_file, $sections, null); + return $this; + } + + /** + * load a config file, optionally load just selected sections + * + * @param string $config_file filename + * @param mixed $sections array of section names, single + * section or null + * @param int $scope scope into which config variables + * shall be loaded + * + * @throws \Exception + *@link https://www.smarty.net/docs/en/api.config.load.tpl + * + * @api Smarty::configLoad() + */ + public function _loadConfigFile($config_file, $sections = null, $scope = 0) + { + /* @var \Smarty $smarty */ + $smarty = $this->_getSmartyObj(); + /* @var \Smarty_Internal_Template $confObj */ + $confObj = new Smarty_Internal_Template($config_file, $smarty, $this, null, null, null, null, true); + $confObj->caching = Smarty::CACHING_OFF; + $confObj->source->config_sections = $sections; + $confObj->source->scope = $scope; + $confObj->compiled = Smarty_Template_Compiled::load($confObj); + $confObj->compiled->render($confObj); + if ($this->_isTplObj()) { + $this->compiled->file_dependency[ $confObj->source->uid ] = + array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type); + } + } + + /** + * gets a config variable value + * + * @param string $varName the name of the config variable + * @param bool $errorEnable + * + * @return null|string the value of the config variable + */ + public function getConfigVariable($varName = null, $errorEnable = true) + { + $_ptr = $this; + while ($_ptr !== null) { + if (isset($_ptr->config_vars[ $varName ])) { + // found it, return it + return $_ptr->config_vars[ $varName ]; + } + // not found, try at parent + $_ptr = $_ptr->parent; + } + if ($this->smarty->error_unassigned && $errorEnable) { + // force a notice + $x = $$varName; + } + return null; + } + + /** + * Returns a single or all config variables + * + * @api Smarty::getConfigVars() + * @link https://www.smarty.net/docs/en/api.get.config.vars.tpl + * + * @param string $varname variable name or null + * @param bool $search_parents include parent templates? + * + * @return mixed variable value or or array of variables + */ + public function getConfigVars($varname = null, $search_parents = true) + { + $_ptr = $this; + $var_array = array(); + while ($_ptr !== null) { + if (isset($varname)) { + if (isset($_ptr->config_vars[ $varname ])) { + return $_ptr->config_vars[ $varname ]; + } + } else { + $var_array = array_merge($_ptr->config_vars, $var_array); + } + // not found, try at parent + if ($search_parents) { + $_ptr = $_ptr->parent; + } else { + $_ptr = null; + } + } + if (isset($varname)) { + return ''; + } else { + return $var_array; + } + } + + /** + * Returns a single or all global variables + * + * @api Smarty::getGlobal() + * + * @param string $varName variable name or null + * + * @return string|array variable value or or array of variables + */ + public function getGlobal($varName = null) + { + if (isset($varName)) { + if (isset(Smarty::$global_tpl_vars[ $varName ])) { + return Smarty::$global_tpl_vars[ $varName ]->value; + } else { + return ''; + } + } else { + $_result = array(); + foreach (Smarty::$global_tpl_vars as $key => $var) { + $_result[ $key ] = $var->value; + } + return $_result; + } + } + + /** + * gets a stream variable + * + * @api Smarty::getStreamVariable() + * + * @param string $variable the stream of the variable + * + * @return mixed + * @throws \SmartyException + */ + public function getStreamVariable($variable) + { + $_result = ''; + $fp = fopen($variable, 'r+'); + if ($fp) { + while (!feof($fp) && ($current_line = fgets($fp)) !== false) { + $_result .= $current_line; + } + fclose($fp); + return $_result; + } + $smarty = $this->smarty ?? $this; + if ($smarty->error_unassigned) { + throw new SmartyException('Undefined stream variable "' . $variable . '"'); + } else { + return null; + } + } + } diff --git a/src/Method/smarty_internal_method_adddefaultmodifiers.php b/src/Method/smarty_internal_method_adddefaultmodifiers.php deleted file mode 100644 index c3feb3d8..00000000 --- a/src/Method/smarty_internal_method_adddefaultmodifiers.php +++ /dev/null @@ -1,42 +0,0 @@ -_getSmartyObj(); - if (is_array($modifiers)) { - $smarty->default_modifiers = array_merge($smarty->default_modifiers, $modifiers); - } else { - $smarty->default_modifiers[] = $modifiers; - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_append.php b/src/Method/smarty_internal_method_append.php deleted file mode 100644 index f3d9f546..00000000 --- a/src/Method/smarty_internal_method_append.php +++ /dev/null @@ -1,74 +0,0 @@ - $_val) { - if ($_key !== '') { - $this->append($data, $_key, $_val, $merge, $nocache); - } - } - } else { - if ($tpl_var !== '' && isset($value)) { - if (!isset($data->tpl_vars[ $tpl_var ])) { - $tpl_var_inst = $data->ext->getTemplateVars->_getVariable($data, $tpl_var, null, true, false); - if ($tpl_var_inst instanceof Smarty_Undefined_Variable) { - $data->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache); - } else { - $data->tpl_vars[ $tpl_var ] = clone $tpl_var_inst; - } - } - if (!(is_array($data->tpl_vars[ $tpl_var ]->value) - || $data->tpl_vars[ $tpl_var ]->value instanceof ArrayAccess) - ) { - settype($data->tpl_vars[ $tpl_var ]->value, 'array'); - } - if ($merge && is_array($value)) { - foreach ($value as $_mkey => $_mval) { - $data->tpl_vars[ $tpl_var ]->value[ $_mkey ] = $_mval; - } - } else { - $data->tpl_vars[ $tpl_var ]->value[] = $value; - } - } - if ($data->_isTplObj() && $data->scope) { - $data->ext->_updateScope->_updateScope($data, $tpl_var); - } - } - return $data; - } -} diff --git a/src/Method/smarty_internal_method_appendbyref.php b/src/Method/smarty_internal_method_appendbyref.php deleted file mode 100644 index a7407e45..00000000 --- a/src/Method/smarty_internal_method_appendbyref.php +++ /dev/null @@ -1,49 +0,0 @@ -tpl_vars[ $tpl_var ])) { - $data->tpl_vars[ $tpl_var ] = new \Smarty\Variable(); - } - if (!is_array($data->tpl_vars[ $tpl_var ]->value)) { - settype($data->tpl_vars[ $tpl_var ]->value, 'array'); - } - if ($merge && is_array($value)) { - foreach ($value as $_key => $_val) { - $data->tpl_vars[ $tpl_var ]->value[ $_key ] = &$value[ $_key ]; - } - } else { - $data->tpl_vars[ $tpl_var ]->value[] = &$value; - } - if ($data->_isTplObj() && $data->scope) { - $data->ext->_updateScope->_updateScope($data, $tpl_var); - } - } - return $data; - } -} diff --git a/src/Method/smarty_internal_method_assignbyref.php b/src/Method/smarty_internal_method_assignbyref.php deleted file mode 100644 index b9a75332..00000000 --- a/src/Method/smarty_internal_method_assignbyref.php +++ /dev/null @@ -1,36 +0,0 @@ -tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache); - $data->tpl_vars[ $tpl_var ]->value = &$value; - if ($data->_isTplObj() && $data->scope) { - $data->ext->_updateScope->_updateScope($data, $tpl_var); - } - } - return $data; - } -} diff --git a/src/Method/smarty_internal_method_assignglobal.php b/src/Method/smarty_internal_method_assignglobal.php deleted file mode 100644 index 3366bb27..00000000 --- a/src/Method/smarty_internal_method_assignglobal.php +++ /dev/null @@ -1,44 +0,0 @@ -_isTplObj()) { - $ptr->tpl_vars[ $varName ] = clone \Smarty\Smarty::$global_tpl_vars[ $varName ]; - $ptr = $ptr->parent; - } - } - return $data; - } -} diff --git a/src/Method/smarty_internal_method_clearallassign.php b/src/Method/smarty_internal_method_clearallassign.php deleted file mode 100644 index efeea368..00000000 --- a/src/Method/smarty_internal_method_clearallassign.php +++ /dev/null @@ -1,36 +0,0 @@ -tpl_vars = array(); - return $data; - } -} diff --git a/src/Method/smarty_internal_method_clearallcache.php b/src/Method/smarty_internal_method_clearallcache.php deleted file mode 100644 index b33bb310..00000000 --- a/src/Method/smarty_internal_method_clearallcache.php +++ /dev/null @@ -1,41 +0,0 @@ -_clearTemplateCache(); - // load cache resource and call clearAll - $_cache_resource = \Smarty\Cacheresource\Base::load($smarty, $type); - return $_cache_resource->clearAll($smarty, $exp_time); - } -} diff --git a/src/Method/smarty_internal_method_clearassign.php b/src/Method/smarty_internal_method_clearassign.php deleted file mode 100644 index b0a6b73d..00000000 --- a/src/Method/smarty_internal_method_clearassign.php +++ /dev/null @@ -1,43 +0,0 @@ -tpl_vars[ $curr_var ]); - } - } else { - unset($data->tpl_vars[ $tpl_var ]); - } - return $data; - } -} diff --git a/src/Method/smarty_internal_method_clearcache.php b/src/Method/smarty_internal_method_clearcache.php deleted file mode 100644 index d6ed15c2..00000000 --- a/src/Method/smarty_internal_method_clearcache.php +++ /dev/null @@ -1,50 +0,0 @@ -_clearTemplateCache(); - // load cache resource and call clear - $_cache_resource = \Smarty\Cacheresource\Base::load($smarty, $type); - return $_cache_resource->clear($smarty, $template_name, $cache_id, $compile_id, $exp_time); - } -} diff --git a/src/Method/smarty_internal_method_clearcompiledtemplate.php b/src/Method/smarty_internal_method_clearcompiledtemplate.php deleted file mode 100644 index c6cf03b7..00000000 --- a/src/Method/smarty_internal_method_clearcompiledtemplate.php +++ /dev/null @@ -1,131 +0,0 @@ -_clearTemplateCache(); - $_compile_dir = $smarty->getCompileDir(); - if ($_compile_dir === '/') { //We should never want to delete this! - return 0; - } - $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null; - $_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^'; - if (isset($resource_name)) { - $_save_stat = $smarty->caching; - $smarty->caching = \Smarty\Smarty::CACHING_OFF; - /* @var Smarty_Internal_Template $tpl */ - $tpl = $smarty->createTemplate($resource_name); - $smarty->caching = $_save_stat; - if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) { - $_resource_part_1 = basename(str_replace('^', DIRECTORY_SEPARATOR, $tpl->compiled->filepath)); - $_resource_part_1_length = strlen($_resource_part_1); - } else { - return 0; - } - $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1); - $_resource_part_2_length = strlen($_resource_part_2); - } - $_dir = $_compile_dir; - if ($smarty->use_sub_dirs && isset($_compile_id)) { - $_dir .= $_compile_id . $_dir_sep; - } - if (isset($_compile_id)) { - $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep; - $_compile_id_part_length = strlen($_compile_id_part); - } - $_count = 0; - try { - $_compileDirs = new RecursiveDirectoryIterator($_dir); - // NOTE: UnexpectedValueException thrown for PHP >= 5.3 - } catch (Exception $e) { - return 0; - } - $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); - foreach ($_compile as $_file) { - if (substr(basename($_file->getPathname()), 0, 1) === '.') { - continue; - } - $_filepath = (string)$_file; - if ($_file->isDir()) { - if (!$_compile->isDot()) { - // delete folder if empty - @rmdir($_file->getPathname()); - } - } else { - // delete only php files - if (substr($_filepath, -4) !== '.php') { - continue; - } - $unlink = false; - if ((!isset($_compile_id) || - (isset($_filepath[ $_compile_id_part_length ]) && - $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) - && (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ]) - && substr_compare( - $_filepath, - $_resource_part_1, - -$_resource_part_1_length, - $_resource_part_1_length - ) === 0) || (isset($_filepath[ $_resource_part_2_length ]) - && substr_compare( - $_filepath, - $_resource_part_2, - -$_resource_part_2_length, - $_resource_part_2_length - ) === 0)) - ) { - if (isset($exp_time)) { - if (is_file($_filepath) && time() - filemtime($_filepath) >= $exp_time) { - $unlink = true; - } - } else { - $unlink = true; - } - } - if ($unlink && is_file($_filepath) && @unlink($_filepath)) { - $_count++; - if (function_exists('opcache_invalidate') - && (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api')) < 1) - ) { - opcache_invalidate($_filepath, true); - } elseif (function_exists('apc_delete_file')) { - apc_delete_file($_filepath); - } - } - } - } - return $_count; - } -} diff --git a/src/Method/smarty_internal_method_clearconfig.php b/src/Method/smarty_internal_method_clearconfig.php deleted file mode 100644 index b28fa379..00000000 --- a/src/Method/smarty_internal_method_clearconfig.php +++ /dev/null @@ -1,41 +0,0 @@ -config_vars[ $name ]); - } else { - $data->config_vars = array(); - } - return $data; - } -} diff --git a/src/Method/smarty_internal_method_compileallconfig.php b/src/Method/smarty_internal_method_compileallconfig.php deleted file mode 100644 index 3934ca04..00000000 --- a/src/Method/smarty_internal_method_compileallconfig.php +++ /dev/null @@ -1,36 +0,0 @@ -compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors, true); - } -} diff --git a/src/Method/smarty_internal_method_compilealltemplates.php b/src/Method/smarty_internal_method_compilealltemplates.php deleted file mode 100644 index 92daef68..00000000 --- a/src/Method/smarty_internal_method_compilealltemplates.php +++ /dev/null @@ -1,130 +0,0 @@ -compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors); - } - - /** - * Compile all template or config files - * - * @param \Smarty $smarty - * @param string $extension template file name extension - * @param bool $force_compile force all to recompile - * @param int $time_limit set maximum execution time - * @param int $max_errors set maximum allowed errors - * @param bool $isConfig flag true if called for config files - * - * @return int number of template files compiled - */ - protected function compileAll( - Smarty $smarty, - $extension, - $force_compile, - $time_limit, - $max_errors, - $isConfig = false - ) { - // switch off time limit - if (function_exists('set_time_limit')) { - @set_time_limit($time_limit); - } - $_count = 0; - $_error_count = 0; - $sourceDir = $isConfig ? $smarty->getConfigDir() : $smarty->getTemplateDir(); - // loop over array of source directories - foreach ($sourceDir as $_dir) { - $_dir_1 = new RecursiveDirectoryIterator( - $_dir, - defined('FilesystemIterator::FOLLOW_SYMLINKS') ? - FilesystemIterator::FOLLOW_SYMLINKS : 0 - ); - $_dir_2 = new RecursiveIteratorIterator($_dir_1); - foreach ($_dir_2 as $_fileinfo) { - $_file = $_fileinfo->getFilename(); - if (substr(basename($_fileinfo->getPathname()), 0, 1) === '.' || strpos($_file, '.svn') !== false) { - continue; - } - if (substr_compare($_file, $extension, -strlen($extension)) !== 0) { - continue; - } - if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) { - $_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file; - } - echo "\n
", $_dir, '---', $_file; - flush(); - $_start_time = microtime(true); - $_smarty = clone $smarty; - // - $_smarty->_cache = array(); - $_smarty->ext = new Smarty_Internal_Extension_Handler(); - $_smarty->ext->objType = $_smarty->_objType; - $_smarty->force_compile = $force_compile; - try { - /* @var Smarty_Internal_Template $_tpl */ - $_tpl = new $smarty->template_class($_file, $_smarty); - $_tpl->caching = \Smarty\Smarty::CACHING_OFF; - $_tpl->source = - $isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl); - if ($_tpl->mustCompile()) { - $_tpl->compileTemplateSource(); - $_count++; - echo ' compiled in ', microtime(true) - $_start_time, ' seconds'; - flush(); - } else { - echo ' is up to date'; - flush(); - } - } catch (Exception $e) { - echo "\n
------>Error: ", $e->getMessage(), "

\n"; - $_error_count++; - } - // free memory - unset($_tpl); - $_smarty->_clearTemplateCache(); - if ($max_errors !== null && $_error_count === $max_errors) { - echo "\n

too many errors\n"; - exit(1); - } - } - } - echo "\n
"; - return $_count; - } -} diff --git a/src/Method/smarty_internal_method_configload.php b/src/Method/smarty_internal_method_configload.php deleted file mode 100644 index 201e8301..00000000 --- a/src/Method/smarty_internal_method_configload.php +++ /dev/null @@ -1,182 +0,0 @@ -_loadConfigFile($data, $config_file, $sections, null); - return $data; - } - - /** - * load a config file, optionally load just selected sections - * - * @api Smarty::configLoad() - * @link https://www.smarty.net/docs/en/api.config.load.tpl - * - * @param \Smarty|\Smarty\Data|\Smarty_Internal_Template $data - * @param string $config_file filename - * @param mixed $sections array of section names, single - * section or null - * @param int $scope scope into which config variables - * shall be loaded - * - * @throws \Exception - */ - public function _loadConfigFile(\Smarty\Data $data, $config_file, $sections = null, $scope = 0) - { - /* @var \Smarty $smarty */ - $smarty = $data->_getSmartyObj(); - /* @var \Smarty_Internal_Template $confObj */ - $confObj = new Smarty_Internal_Template($config_file, $smarty, $data, null, null, null, null, true); - $confObj->caching = \Smarty\Smarty::CACHING_OFF; - $confObj->source->config_sections = $sections; - $confObj->source->scope = $scope; - $confObj->compiled = Smarty_Template_Compiled::load($confObj); - $confObj->compiled->render($confObj); - if ($data->_isTplObj()) { - $data->compiled->file_dependency[ $confObj->source->uid ] = - array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type); - } - } - - /** - * load config variables into template object - * - * @param \Smarty_Internal_Template $tpl - * @param array $new_config_vars - */ - public function _loadConfigVars(Smarty_Internal_Template $tpl, $new_config_vars) - { - $this->_assignConfigVars($tpl->parent->config_vars, $tpl, $new_config_vars); - $tagScope = $tpl->source->scope; - if ($tagScope >= 0) { - if ($tagScope === \Smarty\Smarty::SCOPE_LOCAL) { - $this->_updateVarStack($tpl, $new_config_vars); - $tagScope = 0; - if (!$tpl->scope) { - return; - } - } - if ($tpl->parent->_isTplObj() && ($tagScope || $tpl->parent->scope)) { - $mergedScope = $tagScope | $tpl->scope; - if ($mergedScope) { - // update scopes - /* @var \Smarty_Internal_Template|\Smarty|\Smarty\Data $ptr */ - foreach ($tpl->smarty->ext->_updateScope->_getAffectedScopes($tpl->parent, $mergedScope) as $ptr) { - $this->_assignConfigVars($ptr->config_vars, $tpl, $new_config_vars); - if ($tagScope && $ptr->_isTplObj() && isset($tpl->_var_stack)) { - $this->_updateVarStack($tpl, $new_config_vars); - } - } - } - } - } - } - - /** - * Assign all config variables in given scope - * - * @param array $config_vars config variables in scope - * @param \Smarty_Internal_Template $tpl - * @param array $new_config_vars loaded config variables - */ - public function _assignConfigVars(&$config_vars, Smarty_Internal_Template $tpl, $new_config_vars) - { - // copy global config vars - foreach ($new_config_vars[ 'vars' ] as $variable => $value) { - if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) { - $config_vars[ $variable ] = $value; - } else { - $config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value); - } - } - // scan sections - $sections = $tpl->source->config_sections; - if (!empty($sections)) { - foreach ((array)$sections as $tpl_section) { - if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) { - foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) { - if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) { - $config_vars[ $variable ] = $value; - } else { - $config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value); - } - } - } - } - } - } - - /** - * Update config variables in template local variable stack - * - * @param \Smarty_Internal_Template $tpl - * @param array $config_vars - */ - public function _updateVarStack(Smarty_Internal_Template $tpl, $config_vars) - { - $i = 0; - while (isset($tpl->_var_stack[ $i ])) { - $this->_assignConfigVars($tpl->_var_stack[ $i ][ 'config' ], $tpl, $config_vars); - $i++; - } - } - - /** - * gets a config variable value - * - * @param \Smarty|\Smarty\Data|\Smarty_Internal_Template $data - * @param string $varName the name of the config variable - * @param bool $errorEnable - * - * @return null|string the value of the config variable - */ - public function _getConfigVariable(\Smarty\Data $data, $varName, $errorEnable = true) - { - $_ptr = $data; - while ($_ptr !== null) { - if (isset($_ptr->config_vars[ $varName ])) { - // found it, return it - return $_ptr->config_vars[ $varName ]; - } - // not found, try at parent - $_ptr = $_ptr->parent; - } - if ($data->smarty->error_unassigned && $errorEnable) { - // force a notice - $x = $$varName; - } - return null; - } -} diff --git a/src/Method/smarty_internal_method_createdata.php b/src/Method/smarty_internal_method_createdata.php deleted file mode 100644 index 8b264810..00000000 --- a/src/Method/smarty_internal_method_createdata.php +++ /dev/null @@ -1,44 +0,0 @@ -_getSmartyObj(); - $dataObj = new Smarty_Data($parent, $smarty, $name); - if ($smarty->debugging) { - \Smarty\Debug::register_data($dataObj); - } - return $dataObj; - } -} diff --git a/src/Method/smarty_internal_method_getconfigvariable.php b/src/Method/smarty_internal_method_getconfigvariable.php deleted file mode 100644 index b08dbee8..00000000 --- a/src/Method/smarty_internal_method_getconfigvariable.php +++ /dev/null @@ -1,34 +0,0 @@ -ext->configLoad->_getConfigVariable($data, $varName, $errorEnable); - } -} diff --git a/src/Method/smarty_internal_method_getconfigvars.php b/src/Method/smarty_internal_method_getconfigvars.php deleted file mode 100644 index 666410d7..00000000 --- a/src/Method/smarty_internal_method_getconfigvars.php +++ /dev/null @@ -1,58 +0,0 @@ -config_vars[ $varname ])) { - return $_ptr->config_vars[ $varname ]; - } - } else { - $var_array = array_merge($_ptr->config_vars, $var_array); - } - // not found, try at parent - if ($search_parents) { - $_ptr = $_ptr->parent; - } else { - $_ptr = null; - } - } - if (isset($varname)) { - return ''; - } else { - return $var_array; - } - } -} diff --git a/src/Method/smarty_internal_method_getdebugtemplate.php b/src/Method/smarty_internal_method_getdebugtemplate.php deleted file mode 100644 index 77d908c1..00000000 --- a/src/Method/smarty_internal_method_getdebugtemplate.php +++ /dev/null @@ -1,35 +0,0 @@ -_getSmartyObj(); - return $smarty->debug_tpl; - } -} diff --git a/src/Method/smarty_internal_method_getdefaultmodifiers.php b/src/Method/smarty_internal_method_getdefaultmodifiers.php deleted file mode 100644 index 57da85c4..00000000 --- a/src/Method/smarty_internal_method_getdefaultmodifiers.php +++ /dev/null @@ -1,35 +0,0 @@ -_getSmartyObj(); - return $smarty->default_modifiers; - } -} diff --git a/src/Method/smarty_internal_method_getglobal.php b/src/Method/smarty_internal_method_getglobal.php deleted file mode 100644 index df95db81..00000000 --- a/src/Method/smarty_internal_method_getglobal.php +++ /dev/null @@ -1,47 +0,0 @@ -value; - } else { - return ''; - } - } else { - $_result = array(); - foreach (\Smarty\Smarty::$global_tpl_vars as $key => $var) { - $_result[ $key ] = $var->value; - } - return $_result; - } - } -} diff --git a/src/Method/smarty_internal_method_getregisteredobject.php b/src/Method/smarty_internal_method_getregisteredobject.php deleted file mode 100644 index 0b3a071d..00000000 --- a/src/Method/smarty_internal_method_getregisteredobject.php +++ /dev/null @@ -1,44 +0,0 @@ -_getSmartyObj(); - if (!isset($smarty->registered_objects[ $object_name ])) { - throw new SmartyException("'$object_name' is not a registered object"); - } - if (!is_object($smarty->registered_objects[ $object_name ][ 0 ])) { - throw new SmartyException("registered '$object_name' is not an object"); - } - return $smarty->registered_objects[ $object_name ][ 0 ]; - } -} diff --git a/src/Method/smarty_internal_method_getstreamvariable.php b/src/Method/smarty_internal_method_getstreamvariable.php deleted file mode 100644 index 552803ae..00000000 --- a/src/Method/smarty_internal_method_getstreamvariable.php +++ /dev/null @@ -1,50 +0,0 @@ -smarty) ? $data->smarty : $data; - if ($smarty->error_unassigned) { - throw new SmartyException('Undefined stream variable "' . $variable . '"'); - } else { - return null; - } - } -} diff --git a/src/Method/smarty_internal_method_gettemplatevars.php b/src/Method/smarty_internal_method_gettemplatevars.php deleted file mode 100644 index 560bcdad..00000000 --- a/src/Method/smarty_internal_method_gettemplatevars.php +++ /dev/null @@ -1,119 +0,0 @@ -_getVariable($data, $varName, $_ptr, $searchParents, false); - if (is_object($_var)) { - return $_var->value; - } else { - return null; - } - } else { - $_result = array(); - if ($_ptr === null) { - $_ptr = $data; - } - while ($_ptr !== null) { - foreach ($_ptr->tpl_vars as $key => $var) { - if (!array_key_exists($key, $_result)) { - $_result[ $key ] = $var->value; - } - } - // not found, try at parent - if ($searchParents && isset($_ptr->parent)) { - $_ptr = $_ptr->parent; - } else { - $_ptr = null; - } - } - if ($searchParents && isset(\Smarty\Smarty::$global_tpl_vars)) { - foreach (\Smarty\Smarty::$global_tpl_vars as $key => $var) { - if (!array_key_exists($key, $_result)) { - $_result[ $key ] = $var->value; - } - } - } - return $_result; - } - } - - /** - * gets the object of a Smarty variable - * - * @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $data - * @param string $varName the name of the Smarty variable - * @param \Smarty\Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object - * @param bool $searchParents search also in parent data - * @param bool $errorEnable - * - * @return \Smarty\Variable - */ - public function _getVariable( - \Smarty\Data $data, - $varName, - \Smarty\Data $_ptr = null, - $searchParents = true, - $errorEnable = true - ) { - if ($_ptr === null) { - $_ptr = $data; - } - while ($_ptr !== null) { - if (isset($_ptr->tpl_vars[ $varName ])) { - // found it, return it - return $_ptr->tpl_vars[ $varName ]; - } - // not found, try at parent - if ($searchParents && isset($_ptr->parent)) { - $_ptr = $_ptr->parent; - } else { - $_ptr = null; - } - } - if (isset(\Smarty\Smarty::$global_tpl_vars[ $varName ])) { - // found it, return it - return \Smarty\Smarty::$global_tpl_vars[ $varName ]; - } - if ($errorEnable && $data->_getSmartyObj()->error_unassigned) { - // force a notice - $x = $$varName; - } - return new Smarty_Undefined_Variable; - } -} diff --git a/src/Method/smarty_internal_method_literals.php b/src/Method/smarty_internal_method_literals.php deleted file mode 100644 index bfa3f58e..00000000 --- a/src/Method/smarty_internal_method_literals.php +++ /dev/null @@ -1,100 +0,0 @@ -_getSmartyObj(); - return (array)$smarty->literals; - } - - /** - * Add literals - * - * @api Smarty::addLiterals() - * - * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj - * @param array|string $literals literal or list of literals - * to addto add - * - * @return \Smarty|\Smarty_Internal_Template - * @throws \SmartyException - */ - public function addLiterals(Smarty_Internal_TemplateBase $obj, $literals = null) - { - if (isset($literals)) { - $this->set($obj->_getSmartyObj(), (array)$literals); - } - return $obj; - } - - /** - * Set literals - * - * @api Smarty::setLiterals() - * - * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj - * @param array|string $literals literal or list of literals - * to setto set - * - * @return \Smarty|\Smarty_Internal_Template - * @throws \SmartyException - */ - public function setLiterals(Smarty_Internal_TemplateBase $obj, $literals = null) - { - $smarty = $obj->_getSmartyObj(); - $smarty->literals = array(); - if (!empty($literals)) { - $this->set($smarty, (array)$literals); - } - return $obj; - } - - /** - * common setter for literals for easier handling of duplicates the - * Smarty::$literals array gets filled with identical key values - * - * @param \Smarty $smarty - * @param array $literals - * - * @throws \SmartyException - */ - private function set(Smarty $smarty, $literals) - { - $literals = array_combine($literals, $literals); - $error = isset($literals[ $smarty->left_delimiter ]) ? array($smarty->left_delimiter) : array(); - $error = isset($literals[ $smarty->right_delimiter ]) ? $error[] = $smarty->right_delimiter : $error; - if (!empty($error)) { - throw new SmartyException( - 'User defined literal(s) "' . $error . - '" may not be identical with left or right delimiter' - ); - } - $smarty->literals = array_merge((array)$smarty->literals, (array)$literals); - } -} diff --git a/src/Method/smarty_internal_method_loadfilter.php b/src/Method/smarty_internal_method_loadfilter.php deleted file mode 100644 index 2efac815..00000000 --- a/src/Method/smarty_internal_method_loadfilter.php +++ /dev/null @@ -1,75 +0,0 @@ - true, 'post' => true, 'output' => true, 'variable' => true); - - /** - * load a filter of specified type and name - * - * @api Smarty::loadFilter() - * - * @link https://www.smarty.net/docs/en/api.load.filter.tpl - * - * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj - * @param string $type filter type - * @param string $name filter name - * - * @return bool - * @throws SmartyException if filter could not be loaded - */ - public function loadFilter(Smarty_Internal_TemplateBase $obj, $type, $name) - { - $smarty = $obj->_getSmartyObj(); - $this->_checkFilterType($type); - $_plugin = "smarty_{$type}filter_{$name}"; - $_filter_name = $_plugin; - if (is_callable($_plugin)) { - $smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin; - return true; - } - if (class_exists($_plugin, false)) { - $_plugin = array($_plugin, 'execute'); - } - if (is_callable($_plugin)) { - $smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin; - return true; - } - throw new SmartyException("{$type}filter '{$name}' not found or callable"); - } - - /** - * Check if filter type is valid - * - * @param string $type - * - * @throws \SmartyException - */ - public function _checkFilterType($type) - { - if (!isset($this->filterTypes[ $type ])) { - throw new SmartyException("Illegal filter type '{$type}'"); - } - } -} diff --git a/src/Method/smarty_internal_method_mustcompile.php b/src/Method/smarty_internal_method_mustcompile.php deleted file mode 100644 index 381346c8..00000000 --- a/src/Method/smarty_internal_method_mustcompile.php +++ /dev/null @@ -1,50 +0,0 @@ -source->exists) { - if ($_template->_isSubTpl()) { - $parent_resource = " in '{$_template->parent->template_resource}'"; - } else { - $parent_resource = ''; - } - throw new SmartyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}"); - } - if ($_template->mustCompile === null) { - $_template->mustCompile = (!$_template->source->handler->uncompiled && - ($_template->smarty->force_compile || $_template->source->handler->recompiled || - !$_template->compiled->exists || ($_template->compile_check && - $_template->compiled->getTimeStamp() < - $_template->source->getTimeStamp()))); - } - return $_template->mustCompile; - } -} diff --git a/src/Method/smarty_internal_method_registerfilter.php b/src/Method/smarty_internal_method_registerfilter.php deleted file mode 100644 index 9719eb2b..00000000 --- a/src/Method/smarty_internal_method_registerfilter.php +++ /dev/null @@ -1,87 +0,0 @@ - true, 'post' => true, 'output' => true, 'variable' => true); - - /** - * Registers a filter function - * - * @api Smarty::registerFilter() - * - * @link https://www.smarty.net/docs/en/api.register.filter.tpl - * - * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj - * @param string $type filter type - * @param callback $callback - * @param string|null $name optional filter name - * - * @return \Smarty|\Smarty_Internal_Template - * @throws \SmartyException - */ - public function registerFilter(Smarty_Internal_TemplateBase $obj, $type, $callback, $name = null) - { - $smarty = $obj->_getSmartyObj(); - $this->_checkFilterType($type); - $name = isset($name) ? $name : $this->_getFilterName($callback); - if (!is_callable($callback)) { - throw new SmartyException("{$type}filter '{$name}' not callable"); - } - $smarty->registered_filters[ $type ][ $name ] = $callback; - return $obj; - } - - /** - * Return internal filter name - * - * @param callback $function_name - * - * @return string internal filter name - */ - public function _getFilterName($function_name) - { - if (is_array($function_name)) { - $_class_name = (is_object($function_name[ 0 ]) ? get_class($function_name[ 0 ]) : $function_name[ 0 ]); - return $_class_name . '_' . $function_name[ 1 ]; - } elseif (is_string($function_name)) { - return $function_name; - } else { - return 'closure'; - } - } - - /** - * Check if filter type is valid - * - * @param string $type - * - * @throws \SmartyException - */ - public function _checkFilterType($type) - { - if (!isset($this->filterTypes[ $type ])) { - throw new SmartyException("Illegal filter type '{$type}'"); - } - } -} diff --git a/src/Method/smarty_internal_method_unloadfilter.php b/src/Method/smarty_internal_method_unloadfilter.php deleted file mode 100644 index e41e8dff..00000000 --- a/src/Method/smarty_internal_method_unloadfilter.php +++ /dev/null @@ -1,43 +0,0 @@ -_getSmartyObj(); - $this->_checkFilterType($type); - if (isset($smarty->registered_filters[ $type ])) { - $_filter_name = "smarty_{$type}filter_{$name}"; - if (isset($smarty->registered_filters[ $type ][ $_filter_name ])) { - unset($smarty->registered_filters[ $type ][ $_filter_name ]); - if (empty($smarty->registered_filters[ $type ])) { - unset($smarty->registered_filters[ $type ]); - } - } - } - return $obj; - } -} diff --git a/src/Method/smarty_internal_method_unregisterfilter.php b/src/Method/smarty_internal_method_unregisterfilter.php deleted file mode 100644 index ebc9337d..00000000 --- a/src/Method/smarty_internal_method_unregisterfilter.php +++ /dev/null @@ -1,43 +0,0 @@ -_getSmartyObj(); - $this->_checkFilterType($type); - if (isset($smarty->registered_filters[ $type ])) { - $name = is_string($callback) ? $callback : $this->_getFilterName($callback); - if (isset($smarty->registered_filters[ $type ][ $name ])) { - unset($smarty->registered_filters[ $type ][ $name ]); - if (empty($smarty->registered_filters[ $type ])) { - unset($smarty->registered_filters[ $type ]); - } - } - } - return $obj; - } -} diff --git a/src/Smarty.php b/src/Smarty.php index 7a33c61c..8d5e08fc 100644 --- a/src/Smarty.php +++ b/src/Smarty.php @@ -34,17 +34,6 @@ namespace Smarty; /** * This is the main Smarty class - * - * @package Smarty - * - * The following methods will be dynamically loaded by the extension handler when they are called. - * They are located in a corresponding Smarty_Internal_Method_xxxx class - * - * @method int clearAllCache(int $exp_time = null, string $type = null) - * @method int clearCache(string $template_name, string $cache_id = null, string $compile_id = null, int $exp_time = null, string $type = null) - * @method int compileAllTemplates(string $extension = '.tpl', bool $force_compile = false, int $time_limit = 0, $max_errors = null) - * @method int compileAllConfig(string $extension = '.conf', bool $force_compile = false, int $time_limit = 0, $max_errors = null) - * @method int clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null) */ class Smarty extends \Smarty_Internal_TemplateBase { @@ -1226,4 +1215,288 @@ class Smarty extends \Smarty_Internal_TemplateBase return $this->isMutingUndefinedOrNullWarnings; } + /** + * Empty cache for a specific template + * + * @api Smarty::clearCache() + * @link https://www.smarty.net/docs/en/api.clear.cache.tpl + * + * @param string $template_name template name + * @param string $cache_id cache id + * @param string $compile_id compile id + * @param integer $exp_time expiration time + * @param string $type resource type + * + * @return int number of cache files deleted + * @throws \SmartyException + */ + public function clearCache( + $template_name, + $cache_id = null, + $compile_id = null, + $exp_time = null, + $type = null + ) { + $this->_clearTemplateCache(); + // load cache resource and call clear + $_cache_resource = \Smarty\Cacheresource\Base::load($this, $type); + return $_cache_resource->clear($this, $template_name, $cache_id, $compile_id, $exp_time); + } + + /** + * Empty cache folder + * + * @api Smarty::clearAllCache() + * @link https://www.smarty.net/docs/en/api.clear.all.cache.tpl + * + * @param integer $exp_time expiration time + * @param string $type resource type + * + * @return int number of cache files deleted + */ + public function clearAllCache($exp_time = null, $type = null) + { + $this->_clearTemplateCache(); + // load cache resource and call clearAll + $_cache_resource = \Smarty\Cacheresource\Base::load($this, $type); + return $_cache_resource->clearAll($this, $exp_time); + } + + /** + * Delete compiled template file + * + * @api Smarty::clearCompiledTemplate() + * @link https://www.smarty.net/docs/en/api.clear.compiled.template.tpl + * + * @param string $resource_name template name + * @param string $compile_id compile id + * @param integer $exp_time expiration time + * + * @return int number of template files deleted + * @throws \SmartyException + */ + public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null) + { + // clear template objects cache + $this->_clearTemplateCache(); + $_compile_dir = $this->getCompileDir(); + if ($_compile_dir === '/') { //We should never want to delete this! + return 0; + } + $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null; + $_dir_sep = $this->use_sub_dirs ? DIRECTORY_SEPARATOR : '^'; + if (isset($resource_name)) { + $_save_stat = $this->caching; + $this->caching = \Smarty\Smarty::CACHING_OFF; + /* @var Smarty_Internal_Template $tpl */ + $tpl = $this->createTemplate($resource_name); + $this->caching = $_save_stat; + if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) { + $_resource_part_1 = basename(str_replace('^', DIRECTORY_SEPARATOR, $tpl->compiled->filepath)); + $_resource_part_1_length = strlen($_resource_part_1); + } else { + return 0; + } + $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1); + $_resource_part_2_length = strlen($_resource_part_2); + } + $_dir = $_compile_dir; + if ($this->use_sub_dirs && isset($_compile_id)) { + $_dir .= $_compile_id . $_dir_sep; + } + if (isset($_compile_id)) { + $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep; + $_compile_id_part_length = strlen($_compile_id_part); + } + $_count = 0; + try { + $_compileDirs = new RecursiveDirectoryIterator($_dir); + // NOTE: UnexpectedValueException thrown for PHP >= 5.3 + } catch (Exception $e) { + return 0; + } + $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); + foreach ($_compile as $_file) { + if (substr(basename($_file->getPathname()), 0, 1) === '.') { + continue; + } + $_filepath = (string)$_file; + if ($_file->isDir()) { + if (!$_compile->isDot()) { + // delete folder if empty + @rmdir($_file->getPathname()); + } + } else { + // delete only php files + if (substr($_filepath, -4) !== '.php') { + continue; + } + $unlink = false; + if ((!isset($_compile_id) || + (isset($_filepath[ $_compile_id_part_length ]) && + $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) + && (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ]) + && substr_compare( + $_filepath, + $_resource_part_1, + -$_resource_part_1_length, + $_resource_part_1_length + ) === 0) || (isset($_filepath[ $_resource_part_2_length ]) + && substr_compare( + $_filepath, + $_resource_part_2, + -$_resource_part_2_length, + $_resource_part_2_length + ) === 0)) + ) { + if (isset($exp_time)) { + if (is_file($_filepath) && time() - filemtime($_filepath) >= $exp_time) { + $unlink = true; + } + } else { + $unlink = true; + } + } + if ($unlink && is_file($_filepath) && @unlink($_filepath)) { + $_count++; + if (function_exists('opcache_invalidate') + && (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api')) < 1) + ) { + opcache_invalidate($_filepath, true); + } elseif (function_exists('apc_delete_file')) { + apc_delete_file($_filepath); + } + } + } + } + return $_count; + } + + /** + * Compile all template files + * + * @api Smarty::compileAllTemplates() + * + * @param string $extension file extension + * @param bool $force_compile force all to recompile + * @param int $time_limit + * @param int $max_errors + * + * @return integer number of template files recompiled + */ + public function compileAllTemplates( + $extension = '.tpl', + $force_compile = false, + $time_limit = 0, + $max_errors = null + ) { + return $this->compileAll($extension, $force_compile, $time_limit, $max_errors); + } + + /** + * Compile all config files + * + * @api Smarty::compileAllConfig() + * + * @param string $extension file extension + * @param bool $force_compile force all to recompile + * @param int $time_limit + * @param int $max_errors + * + * @return int number of template files recompiled + */ + public function compileAllConfig( + $extension = '.conf', + $force_compile = false, + $time_limit = 0, + $max_errors = null + ) { + return $this->compileAll($extension, $force_compile, $time_limit, $max_errors, true); + } + + /** + * Compile all template or config files + * + * @param string $extension template file name extension + * @param bool $force_compile force all to recompile + * @param int $time_limit set maximum execution time + * @param int $max_errors set maximum allowed errors + * @param bool $isConfig flag true if called for config files + * + * @return int number of template files compiled + */ + protected function compileAll( + $extension, + $force_compile, + $time_limit, + $max_errors, + $isConfig = false + ) { + // switch off time limit + if (function_exists('set_time_limit')) { + @set_time_limit($time_limit); + } + $_count = 0; + $_error_count = 0; + $sourceDir = $isConfig ? $this->getConfigDir() : $this->getTemplateDir(); + // loop over array of source directories + foreach ($sourceDir as $_dir) { + $_dir_1 = new RecursiveDirectoryIterator( + $_dir, + defined('FilesystemIterator::FOLLOW_SYMLINKS') ? + FilesystemIterator::FOLLOW_SYMLINKS : 0 + ); + $_dir_2 = new RecursiveIteratorIterator($_dir_1); + foreach ($_dir_2 as $_fileinfo) { + $_file = $_fileinfo->getFilename(); + if (substr(basename($_fileinfo->getPathname()), 0, 1) === '.' || strpos($_file, '.svn') !== false) { + continue; + } + if (substr_compare($_file, $extension, -strlen($extension)) !== 0) { + continue; + } + if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) { + $_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file; + } + echo "\n
", $_dir, '---', $_file; + flush(); + $_start_time = microtime(true); + $_smarty = clone $this; + // + $_smarty->_cache = array(); + $_smarty->ext = new Smarty_Internal_Extension_Handler(); + $_smarty->ext->objType = $_smarty->_objType; + $_smarty->force_compile = $force_compile; + try { + /* @var Smarty_Internal_Template $_tpl */ + $_tpl = new $this->template_class($_file, $_smarty); + $_tpl->caching = self::CACHING_OFF; + $_tpl->source = + $isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl); + if ($_tpl->mustCompile()) { + $_tpl->compileTemplateSource(); + $_count++; + echo ' compiled in ', microtime(true) - $_start_time, ' seconds'; + flush(); + } else { + echo ' is up to date'; + flush(); + } + } catch (Exception $e) { + echo "\n
------>Error: ", $e->getMessage(), "

\n"; + $_error_count++; + } + // free memory + unset($_tpl); + $_smarty->_clearTemplateCache(); + if ($max_errors !== null && $_error_count === $max_errors) { + echo "\n

too many errors\n"; + exit(1); + } + } + } + echo "\n
"; + return $_count; + } + } diff --git a/src/smarty_internal_template.php b/src/smarty_internal_template.php index 25f2e06e..70c0fda4 100644 --- a/src/smarty_internal_template.php +++ b/src/smarty_internal_template.php @@ -8,21 +8,15 @@ * @author Uwe Tews */ +use Smarty\Data; + /** * Main class with template data structures and methods * - * @package Smarty - * @subpackage Template - * * @property Smarty_Template_Compiled $compiled * @property Smarty_Template_Cached $cached * @property \Smarty\Compiler\Template $compiler * @property mixed|\Smarty_Template_Cached registered_plugins - * - * The following methods will be dynamically loaded by the extension handler when they are called. - * They are located in a corresponding Smarty_Internal_Method_xxxx class - * - * @method bool mustCompile() */ #[\AllowDynamicProperties] class Smarty_Internal_Template extends Smarty_Internal_TemplateBase @@ -57,13 +51,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase */ public $_objType = 2; - /** - * Global smarty instance - * - * @var Smarty - */ - public $smarty = null; - /** * Source instance * @@ -319,7 +306,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase $tpl->compile_id = $compile_id; if (isset($uid)) { // for inline templates we can get all resource information from file dependency - list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ]; + [$filepath, $timestamp, $type] = $tpl->compiled->file_dependency[ $uid ]; $tpl->source = new Smarty_Template_Source($smarty, $filepath, $type, $filepath); $tpl->source->filepath = $filepath; $tpl->source->timestamp = $timestamp; @@ -508,7 +495,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase if ($is_valid) { $resource->unifunc = $properties[ 'unifunc' ]; $resource->has_nocache_code = $properties[ 'has_nocache_code' ]; - // $tpl->compiled->nocache_hash = $properties['nocache_hash']; $resource->file_dependency = $properties[ 'file_dependency' ]; } return $is_valid && !function_exists($properties[ 'unifunc' ]); @@ -714,4 +700,115 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase $this->cached->handler->releaseLock($this->smarty, $this->cached); } } + + + /** + * load config variables into template object + * + * @param \Smarty_Internal_Template $tpl + * @param array $new_config_vars + */ + public function _loadConfigVars($new_config_vars) + { + $this->_assignConfigVars($this->parent->config_vars, $new_config_vars); + $tagScope = $this->source->scope; + if ($tagScope >= 0) { + if ($tagScope === \Smarty\Smarty::SCOPE_LOCAL) { + $this->_updateConfigVarStack($new_config_vars); + $tagScope = 0; + if (!$this->scope) { + return; + } + } + if ($this->parent->_isTplObj() && ($tagScope || $this->parent->scope)) { + $mergedScope = $tagScope | $this->scope; + if ($mergedScope) { + // update scopes + /* @var \Smarty_Internal_Template|\Smarty|Data $ptr */ + foreach ($this->smarty->ext->_updateScope->_getAffectedScopes($this->parent, $mergedScope) as $ptr) { + $this->_assignConfigVars($ptr->config_vars, $new_config_vars); + if ($tagScope && $ptr->_isTplObj() && isset($this->_var_stack)) { + $this->_updateConfigVarStack($new_config_vars); + } + } + } + } + } + } + + /** + * Assign all config variables in given scope + * + * @param array $config_vars config variables in scope + * @param array $new_config_vars loaded config variables + */ + private function _assignConfigVars(&$config_vars, $new_config_vars) + { + // copy global config vars + foreach ($new_config_vars[ 'vars' ] as $variable => $value) { + if ($this->smarty->config_overwrite || !isset($config_vars[ $variable ])) { + $config_vars[ $variable ] = $value; + } else { + $config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value); + } + } + // scan sections + $sections = $this->source->config_sections; + if (!empty($sections)) { + foreach ((array)$sections as $tpl_section) { + if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) { + foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) { + if ($this->smarty->config_overwrite || !isset($config_vars[ $variable ])) { + $config_vars[ $variable ] = $value; + } else { + $config_vars[ $variable ] = array_merge((array)$config_vars[ $variable ], (array)$value); + } + } + } + } + } + } + + /** + * Update config variables in template local variable stack + * + * @param array $config_vars + */ + private function _updateConfigVarStack($config_vars) + { + $i = 0; + while (isset($this->_var_stack[ $i ])) { + $this->_assignConfigVars($this->_var_stack[ $i ][ 'config' ], $config_vars); + $i++; + } + } + + /** + * Returns if the current template must be compiled by the Smarty compiler + * It does compare the timestamps of template source and the compiled templates and checks the force compile + * configuration + * + * @return bool + * @throws \SmartyException + */ + public function mustCompile() + { + if (!$this->source->exists) { + if ($this->_isSubTpl()) { + $parent_resource = " in '{$this->parent->template_resource}'"; + } else { + $parent_resource = ''; + } + throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}"); + } + if ($this->mustCompile === null) { + $this->mustCompile = (!$this->source->handler->uncompiled && + ($this->smarty->force_compile || $this->source->handler->recompiled || + !$this->compiled->exists || ($this->compile_check && + $this->compiled->getTimeStamp() < + $this->source->getTimeStamp()))); + } + return $this->mustCompile; + } + } diff --git a/src/smarty_internal_templatebase.php b/src/smarty_internal_templatebase.php index 430f4185..49c05abe 100644 --- a/src/smarty_internal_templatebase.php +++ b/src/smarty_internal_templatebase.php @@ -8,24 +8,17 @@ * @author Uwe Tews */ +use Smarty\Data; +use Smarty\Smarty; + /** * Class with shared smarty/template methods * - * @package Smarty - * @subpackage Template - * * @property int $_objType * * The following methods will be dynamically loaded by the extension handler when they are called. * They are located in a corresponding Smarty_Internal_Method_xxxx class * - * @method Smarty_Internal_TemplateBase addDefaultModifiers(mixed $modifiers) - * @method Smarty_Internal_TemplateBase addLiterals(mixed $literals) - * @method Smarty_Internal_TemplateBase createData(\Smarty\Data $parent = null, string $name = null) - * @method string getDebugTemplate() - * @method array getDefaultModifier() - * @method array getLiterals() - * @method object getRegisteredObject(string $object_name) * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, \Smarty\Cacheresource\Base $resource_handler) * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl) * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback) @@ -34,15 +27,12 @@ * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler) * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name) * @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers) - * @method Smarty_Internal_TemplateBase setLiterals(mixed $literals) - * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name) * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name) * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name) * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name) - * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback) * @method Smarty_Internal_TemplateBase unregisterResource(string $name) */ -abstract class Smarty_Internal_TemplateBase extends \Smarty\Data +abstract class Smarty_Internal_TemplateBase extends Data { /** * Set this if you want different sets of cache files for the same @@ -95,7 +85,15 @@ abstract class Smarty_Internal_TemplateBase extends \Smarty\Data */ public $_var_stack = null; - /** + /** + * Valid filter types + * + * @var array + */ + private $filterTypes = array('pre' => true, 'post' => true, 'output' => true, 'variable' => true); + + + /** * fetches a rendered Smarty template * * @param string $template the resource handle of the template file or template object @@ -302,27 +300,107 @@ abstract class Smarty_Internal_TemplateBase extends \Smarty\Data */ public function loadFilter($type, $name) { - return $this->ext->loadFilter->loadFilter($this, $type, $name); + $smarty = $this->_getSmartyObj(); + $this->_checkFilterType($type); + $_plugin = "smarty_{$type}filter_{$name}"; + $_filter_name = $_plugin; + if (is_callable($_plugin)) { + $smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin; + return true; + } + if (class_exists($_plugin, false)) { + $_plugin = array($_plugin, 'execute'); + } + if (is_callable($_plugin)) { + $smarty->registered_filters[ $type ][ $_filter_name ] = $_plugin; + return true; + } + throw new SmartyException("{$type}filter '{$name}' not found or callable"); } + /** + * load a filter of specified type and name + * + * @api Smarty::unloadFilter() + * + * @link https://www.smarty.net/docs/en/api.unload.filter.tpl + * + * @param string $type filter type + * @param string $name filter name + * + * @return Smarty_Internal_TemplateBase + * @throws \SmartyException + */ + public function unloadFilter($type, $name) + { + $smarty = $this->_getSmartyObj(); + $this->_checkFilterType($type); + if (isset($smarty->registered_filters[ $type ])) { + $_filter_name = "smarty_{$type}filter_{$name}"; + if (isset($smarty->registered_filters[ $type ][ $_filter_name ])) { + unset($smarty->registered_filters[ $type ][ $_filter_name ]); + if (empty($smarty->registered_filters[ $type ])) { + unset($smarty->registered_filters[ $type ]); + } + } + } + return $this; + } + /** * Registers a filter function * - * @api Smarty::registerFilter() - * @link https://www.smarty.net/docs/en/api.register.filter.tpl - * * @param string $type filter type * @param callable $callback * @param string|null $name optional filter name * - * @return \Smarty|\Smarty_Internal_Template + * @return Smarty_Internal_TemplateBase * @throws \SmartyException + *@link https://www.smarty.net/docs/en/api.register.filter.tpl + * + * @api Smarty::registerFilter() */ public function registerFilter($type, $callback, $name = null) { - return $this->ext->registerFilter->registerFilter($this, $type, $callback, $name); + $smarty = $this->_getSmartyObj(); + $this->_checkFilterType($type); + $name = isset($name) ? $name : $this->_getFilterName($callback); + if (!is_callable($callback)) { + throw new SmartyException("{$type}filter '{$name}' not callable"); + } + $smarty->registered_filters[ $type ][ $name ] = $callback; + return $this; } + /** + * Unregisters a filter function + * + * @param string $type filter type + * @param callback|string $callback + * + * @return Smarty_Internal_TemplateBase + * @throws \SmartyException + *@api Smarty::unregisterFilter() + * + * @link https://www.smarty.net/docs/en/api.unregister.filter.tpl + * + */ + public function unregisterFilter($type, $callback) + { + $smarty = $this->_getSmartyObj(); + $this->_checkFilterType($type); + if (isset($smarty->registered_filters[ $type ])) { + $name = is_string($callback) ? $callback : $this->_getFilterName($callback); + if (isset($smarty->registered_filters[ $type ][ $name ])) { + unset($smarty->registered_filters[ $type ][ $name ]); + if (empty($smarty->registered_filters[ $type ])) { + unset($smarty->registered_filters[ $type ]); + } + } + } + return $this; + } + /** * Registers object to be used in templates * @@ -394,4 +472,209 @@ abstract class Smarty_Internal_TemplateBase extends \Smarty\Data { $this->cache_id = $cache_id; } + + /** + * Add default modifiers + * + * @api Smarty::addDefaultModifiers() + * + * @param array|string $modifiers modifier or list of modifiers + * to add + * + * @return \Smarty|\Smarty_Internal_Template + */ + public function addDefaultModifiers($modifiers) + { + $smarty = $this->_getSmartyObj(); + if (is_array($modifiers)) { + $smarty->default_modifiers = array_merge($smarty->default_modifiers, $modifiers); + } else { + $smarty->default_modifiers[] = $modifiers; + } + return $this; + } + + /** + * creates a data object + * + * @param Data|null $parent next higher level of Smarty + * variables + * @param null $name optional data block name + * + * @return Smarty_Data data object + * @throws SmartyException + * @api Smarty::createData() + * @link https://www.smarty.net/docs/en/api.create.data.tpl + * + */ + public function createData(Data $parent = null, $name = null) + { + /* @var Smarty $smarty */ + $smarty = $this->_getSmartyObj(); + $dataObj = new Smarty_Data($parent, $smarty, $name); + if ($smarty->debugging) { + \Smarty\Debug::register_data($dataObj); + } + return $dataObj; + } + + /** + * return name of debugging template + * + * @api Smarty::getDebugTemplate() + * + * @return string + */ + public function getDebugTemplate() + { + $smarty = $this->_getSmartyObj(); + return $smarty->debug_tpl; + } + + /** + * Get default modifiers + * + * @api Smarty::getDefaultModifiers() + * + * @return array list of default modifiers + */ + public function getDefaultModifiers() + { + $smarty = $this->_getSmartyObj(); + return $smarty->default_modifiers; + } + + /** + * return a reference to a registered object + * + * @api Smarty::getRegisteredObject() + * @link https://www.smarty.net/docs/en/api.get.registered.object.tpl + * + * @param string $object_name object name + * + * @return object + * @throws \SmartyException if no such object is found + */ + public function getRegisteredObject($object_name) + { + $smarty = $this->_getSmartyObj(); + if (!isset($smarty->registered_objects[ $object_name ])) { + throw new SmartyException("'$object_name' is not a registered object"); + } + if (!is_object($smarty->registered_objects[ $object_name ][ 0 ])) { + throw new SmartyException("registered '$object_name' is not an object"); + } + return $smarty->registered_objects[ $object_name ][ 0 ]; + } + + + /** + * Get literals + * + * @api Smarty::getLiterals() + * + * @return array list of literals + */ + public function getLiterals() + { + $smarty = $this->_getSmartyObj(); + return (array)$smarty->literals; + } + + /** + * Add literals + * + * @param array|string $literals literal or list of literals + * to addto add + * + * @return Smarty_Internal_TemplateBase + * @throws \SmartyException + * @api Smarty::addLiterals() + * + */ + public function addLiterals($literals = null) + { + if (isset($literals)) { + $this->_setLiterals($this->_getSmartyObj(), (array)$literals); + } + return $this; + } + + /** + * Set literals + * + * @param array|string $literals literal or list of literals + * to setto set + * + * @return Smarty_Internal_TemplateBase + * @throws \SmartyException + * @api Smarty::setLiterals() + * + */ + public function setLiterals($literals = null) + { + $smarty = $this->_getSmartyObj(); + $smarty->literals = array(); + if (!empty($literals)) { + $this->_setLiterals($smarty, (array)$literals); + } + return $this; + } + + /** + * common setter for literals for easier handling of duplicates the + * Smarty::$literals array gets filled with identical key values + * + * @param Smarty $smarty + * @param array $literals + * + * @throws \SmartyException + */ + private function _setLiterals(Smarty $smarty, $literals) + { + $literals = array_combine($literals, $literals); + $error = isset($literals[ $smarty->left_delimiter ]) ? array($smarty->left_delimiter) : array(); + $error = isset($literals[ $smarty->right_delimiter ]) ? $error[] = $smarty->right_delimiter : $error; + if (!empty($error)) { + throw new SmartyException( + 'User defined literal(s) "' . $error . + '" may not be identical with left or right delimiter' + ); + } + $smarty->literals = array_merge((array)$smarty->literals, (array)$literals); + } + + /** + * Check if filter type is valid + * + * @param string $type + * + * @throws \SmartyException + */ + private function _checkFilterType($type) + { + if (!isset($this->filterTypes[ $type ])) { + throw new SmartyException("Illegal filter type '{$type}'"); + } + } + + /** + * Return internal filter name + * + * @param callback $function_name + * + * @return string internal filter name + */ + private function _getFilterName($function_name) + { + if (is_array($function_name)) { + $_class_name = (is_object($function_name[ 0 ]) ? get_class($function_name[ 0 ]) : $function_name[ 0 ]); + return $_class_name . '_' . $function_name[ 1 ]; + } elseif (is_string($function_name)) { + return $function_name; + } else { + return 'closure'; + } + } + }