/** * 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); $_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)) . DS . $_file; } echo "\n<br>", $_dir, '---', $_file; flush(); $_start_time = microtime(true); $_smarty = clone $smarty; $_smarty->force_compile = $force_compile; try { /* @var Smarty_Internal_Template $_tpl */ $_tpl = new $smarty->template_class($_file, $_smarty); $_tpl->caching = 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<br> ------>Error: ", $e->getMessage(), "<br><br>\n"; $_error_count++; } // free memory unset($_tpl); $_smarty->_clearTemplateCache(); if ($max_errors !== null && $_error_count == $max_errors) { echo "\n<br><br>too many errors\n"; exit; } } } echo "\n<br>"; return $_count; }
/** * 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 * * @throws SmartyException * @return boolean true if the template must be compiled */ public function mustCompile() { if (!$this->source->exists) { if ($this->parent instanceof Smarty_Internal_Template) { $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->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || !$this->compiled->exists || $this->smarty->compile_check && $this->compiled->getTimeStamp() < $this->source->getTimeStamp()); } return $this->mustCompile; }
/** * load a config file, optionally load just selected sections * * @api Smarty::configLoad() * @link http://www.smarty.net/docs/en/api.config.load.tpl * * @param \Smarty|\Smarty_Internal_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 * * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template * @throws \SmartyException */ public function _loadConfigFile(Smarty_Internal_Data $data, $config_file, $sections = null, $scope = 0) { /* @var \Smarty $smarty */ $smarty = isset($data->smarty) ? $data->smarty : $data; /* @var \Smarty_Internal_Template $confObj */ $confObj = new Smarty_Internal_Template($config_file, $smarty, $data); $confObj->caching = Smarty::CACHING_OFF; $confObj->source = Smarty_Template_Config::load($confObj); $confObj->source->config_sections = $sections; $confObj->source->scope = $scope; $confObj->compiled = Smarty_Template_Compiled::load($confObj); $confObj->compiled->render($confObj); if ($data->_objType == 2) { $data->compiled->file_dependency[$confObj->source->uid] = array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type); } }
/** * @param $obj * @param $config_file * @param null $sections * @param string $scope */ static function configLoad($obj, $config_file, $sections = null, $scope = 'local') { $smarty = isset($obj->smarty) ? $obj->smarty : $obj; $confObj = new $smarty->template_class($config_file, $smarty, $obj); $confObj->caching = Smarty::CACHING_OFF; $confObj->source = Smarty_Template_Config::load($confObj); $confObj->source->config_sections = $sections; $confObj->source->scope = $scope; $confObj->compiled = Smarty_Template_Compiled::load($confObj); if ($confObj->smarty->debugging) { Smarty_Internal_Debug::start_render($confObj); } $confObj->compiled->render($confObj); if ($confObj->smarty->debugging) { Smarty_Internal_Debug::end_render($confObj); } if ($obj instanceof Smarty_Internal_Template) { $obj->properties['file_dependency'][$confObj->source->uid] = array($confObj->source->filepath, $confObj->source->timestamp, $confObj->source->type); } }
/** * @param $obj * @param $config_file * @param null $sections * @param string $scope * * @throws \SmartyException */ static function configLoad($obj, $config_file, $sections = null, $scope = 'local') { $smarty = isset($obj->smarty) ? $obj->smarty : $obj; $confObj = new $smarty->template_class($config_file, $smarty, $obj); $confObj->context = new Smarty_Internal_Context(); $confObj->caching = Smarty::CACHING_OFF; $confObj->source = Smarty_Template_Config::load($confObj); $confObj->source->config_sections = $sections; $confObj->source->scope = $scope; $confObj->compiled = Smarty_Template_Compiled::load($confObj); if ($confObj->smarty->debugging) { Smarty_Internal_Debug::start_render($confObj); } $confObj->compiled->render($confObj); $confObj->context = null; if ($confObj->smarty->debugging) { Smarty_Internal_Debug::end_render($confObj); } if ($obj instanceof Smarty_Internal_Template && isset($obj->context)) { $obj->context->resourceInfo[$confObj->source->uid] = array($confObj->source->filepath, $confObj->source->timestamp, $confObj->source->type); } else { // TODO config file dependency if loaded in Smarty, Data or Template object } }
/** * render template * * @param bool $merge_tpl_vars if true parent template variables merged in to local scope * @param bool $no_output_filter if true do not run output filter * @param bool $display true: display, false: fetch null: subtemplate * * @throws Exception * @throws SmartyException * @return string rendered template output */ public function render($merge_tpl_vars = false, $no_output_filter = true, $display = null) { $parentIsTpl = isset($this->parent) && $this->parent->_objType == 2; if ($this->smarty->debugging) { $this->smarty->_debug->start_template($this, $display); } // checks if template exists if (!$this->source->exists) { if ($parentIsTpl) { $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}"); } $save_tpl_vars = null; $save_config_vars = null; // merge all variable scopes into template if ($merge_tpl_vars) { // save local variables $save_tpl_vars = $this->tpl_vars; $save_config_vars = $this->config_vars; $ptr_array = array($this); $ptr = $this; while (isset($ptr->parent)) { $ptr_array[] = $ptr = $ptr->parent; } $ptr_array = array_reverse($ptr_array); $parent_ptr = reset($ptr_array); $tpl_vars = $parent_ptr->tpl_vars; $config_vars = $parent_ptr->config_vars; while ($parent_ptr = next($ptr_array)) { if (!empty($parent_ptr->tpl_vars)) { $tpl_vars = array_merge($tpl_vars, $parent_ptr->tpl_vars); } if (!empty($parent_ptr->config_vars)) { $config_vars = array_merge($config_vars, $parent_ptr->config_vars); } } if (!empty(Smarty::$global_tpl_vars)) { $tpl_vars = array_merge(Smarty::$global_tpl_vars, $tpl_vars); } $this->tpl_vars = $tpl_vars; $this->config_vars = $config_vars; } // check URL debugging control if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') { $this->smarty->_debug->debugUrl($this); } // disable caching for evaluated code if ($this->source->handler->recompiled) { $this->caching = false; } // read from cache or render $isCacheTpl = $this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED; if ($isCacheTpl) { if (!isset($this->cached)) { $this->loadCached(); } $this->cached->render($this, $no_output_filter); } elseif ($this->source->handler->uncompiled) { $this->source->render($this); } else { if (!isset($this->compiled)) { $this->loadCompiled(); } $this->compiled->render($this); } $content = null; if ((!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { $content = Smarty_Internal_Filter_Handler::runFilter('output', ob_get_clean(), $this); } // display or fetch if ($display) { if ($this->caching && $this->smarty->cache_modified_check) { $this->cached->cacheModifiedCheck($this, isset($content) ? $content : ob_get_clean()); } else { echo isset($content) ? $content : ob_get_clean(); } if ($this->smarty->debugging) { $this->smarty->_debug->end_template($this); } // debug output if ($this->smarty->debugging) { $this->smarty->_debug->display_debug($this, true); } if ($merge_tpl_vars) { // restore local variables $this->tpl_vars = $save_tpl_vars; $this->config_vars = $save_config_vars; } return ''; } else { if ($merge_tpl_vars) { // restore local variables $this->tpl_vars = $save_tpl_vars; $this->config_vars = $save_config_vars; } if ($this->smarty->debugging) { $this->smarty->_debug->end_template($this); } if ($this->smarty->debugging == 2 and $display === false) { if ($this->smarty->debugging) { $this->smarty->_debug->display_debug($this, true); } } if ($parentIsTpl) { if (!empty($this->tpl_function)) { $this->parent->tpl_function = array_merge($this->parent->tpl_function, $this->tpl_function); } foreach ($this->compiled->required_plugins as $code => $tmp1) { foreach ($tmp1 as $name => $tmp) { foreach ($tmp as $type => $data) { $this->parent->compiled->required_plugins[$code][$name][$type] = $data; } } } } // return cache content return $content === null ? null : $content; } }
/** * render template * * @param bool $no_output_filter if true do not run output filter * @param null|bool $display true: display, false: fetch null: sub-template * * @return string * @throws \SmartyException */ public function render($no_output_filter = true, $display = null) { $parentIsTpl = isset($this->parent) && $this->parent->_objType == 2; if ($this->smarty->debugging) { $this->smarty->_debug->start_template($this, $display); } // checks if template exists if (!$this->source->exists) { if ($parentIsTpl) { $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}"); } // disable caching for evaluated code if ($this->source->handler->recompiled) { $this->caching = false; } // read from cache or render $isCacheTpl = $this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED; if ($isCacheTpl) { if (!isset($this->cached)) { $this->loadCached(); } $this->cached->render($this, $no_output_filter); } elseif ($this->source->handler->uncompiled) { $this->source->render($this); } else { if (!isset($this->compiled)) { $this->loadCompiled(); } $this->compiled->render($this); } // display or fetch if ($display) { if ($this->caching && $this->smarty->cache_modified_check) { $this->smarty->ext->_cacheModify->cacheModifiedCheck($this->cached, $this, isset($content) ? $content : ob_get_clean()); } else { if ((!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { echo $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this); } else { ob_end_flush(); flush(); } } if ($this->smarty->debugging) { $this->smarty->_debug->end_template($this); // debug output $this->smarty->_debug->display_debug($this, true); } return ''; } else { if ($this->smarty->debugging) { $this->smarty->_debug->end_template($this); if ($this->smarty->debugging === 2 && $display === false) { $this->smarty->_debug->display_debug($this, true); } } if ($parentIsTpl) { if (!empty($this->tpl_function)) { $this->parent->tpl_function = array_merge($this->parent->tpl_function, $this->tpl_function); } foreach ($this->compiled->required_plugins as $code => $tmp1) { foreach ($tmp1 as $name => $tmp) { foreach ($tmp as $type => $data) { $this->parent->compiled->required_plugins[$code][$name][$type] = $data; } } } } if (!$no_output_filter && (!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { return $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this); } // return cache content return null; } }
/** * Create template data object * Some of the global Smarty settings copied to template scope * It load the required template resources and caching plugins * * @param string $template_resource template resource string * @param Smarty $smarty Smarty instance * @param null|\Smarty_Internal_Template|\Smarty|\Smarty_Internal_Data $_parent back pointer to parent object * with variables or null * @param mixed $_cache_id cache id or null * @param mixed $_compile_id compile id or null * @param bool|int|null $_caching use caching? * @param int|null $_cache_lifetime cache life-time in seconds * @param bool $_isConfig * * @throws \SmartyException */ public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null, $_isConfig = false) { $this->smarty = $smarty; // Smarty parameter $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id; $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id; $this->caching = $_caching === null ? $this->smarty->caching : $_caching; if ($this->caching === true) { $this->caching = Smarty::CACHING_LIFETIME_CURRENT; } $this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime; $this->parent = $_parent; // Template resource $this->template_resource = $template_resource; $this->source = $_isConfig ? Smarty_Template_Config::load($this) : Smarty_Template_Source::load($this); parent::__construct(); if ($smarty->security_policy && method_exists($smarty->security_policy, 'registerCallBacks')) { $smarty->security_policy->registerCallBacks($this); } }
/** * Compile all config files * * @param string $extension config 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 Smarty $smarty Smarty instance * * @return integer number of config files compiled */ public static function compileAllConfig($extension, $force_compile, $time_limit, $max_errors, Smarty $smarty) { // switch off time limit if (function_exists('set_time_limit')) { @set_time_limit($time_limit); } $smarty->force_compile = $force_compile; $_count = 0; $_error_count = 0; // loop over array of template directories foreach ($smarty->getConfigDir() as $_dir) { $_compileDirs = new RecursiveDirectoryIterator($_dir); $_compile = new RecursiveIteratorIterator($_compileDirs); foreach ($_compile 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)) { $_config_file = $_file; } else { $_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file; } echo '<br>', $_dir, '---', $_config_file; flush(); $_start_time = microtime(true); try { $confObj = new $smarty->template_class($_config_file, $smarty); $confObj->caching = Smarty::CACHING_OFF; $confObj->source = Smarty_Template_Config::load($confObj); if ($confObj->mustCompile()) { $confObj->compileTemplateSource(); $_count++; echo ' compiled in ', microtime(true) - $_start_time, ' seconds'; flush(); } else { echo ' is up to date'; flush(); } } catch (Exception $e) { echo 'Error: ', $e->getMessage(), "<br><br>"; $_error_count++; } if ($max_errors !== null && $_error_count == $max_errors) { echo '<br><br>too many errors'; exit; } } } return $_count; }