move internal createTemplateCodeFrame() method into an extension class

This commit is contained in:
Uwe Tews
2015-01-01 22:59:07 +01:00
parent e8a77171d3
commit 80bf76cc6e
3 changed files with 90 additions and 91 deletions

View File

@@ -0,0 +1,76 @@
<?php
/**
* Smarty Internal Extension
* This file contains the Smarty template extension to create a code frame
*
* @package Smarty
* @subpackage Template
* @author Uwe Tews
*/
/**
* Class Smarty_Internal_Extension_CodeFrame
* Create code frame for compiled and cached templates
*/
class Smarty_Internal_Extension_CodeFrame
{
/**
* Create code frame for compiled and cached templates
*
* @param Smarty_Internal_Template $_template
* @param string $content optional template content
* @param bool $cache flag for cache file
*
* @return string
*/
public static function create(Smarty_Internal_Template $_template, $content = '', $cache = false)
{
// build property code
$_template->properties['has_nocache_code'] = $_template->has_nocache_code || !empty($_template->required_plugins['nocache']);
$_template->properties['version'] = Smarty::SMARTY_VERSION;
if (!isset($_template->properties['unifunc'])) {
$_template->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
}
$output = "<?php\n";
$output .= "/*%%SmartyHeaderCode:{$_template->properties['nocache_hash']}%%*/\n";
if ($_template->smarty->direct_access_security) {
$output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
}
$output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($_template->properties, true) . ',' . ($cache ? 'true' : 'false') . ");\n/*/%%SmartyHeaderCode%%*/\n";
$output .= "if (\$_valid && !is_callable('{$_template->properties['unifunc']}')) {function {$_template->properties['unifunc']} (\$_smarty_tpl) {\n";
// include code for plugins
if (!$cache) {
if (!empty($_template->required_plugins['compiled'])) {
foreach ($_template->required_plugins['compiled'] as $tmp) {
foreach ($tmp as $data) {
$file = addslashes($data['file']);
if (is_Array($data['function'])) {
$output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n";
} else {
$output .= "if (!is_callable('{$data['function']}')) include '{$file}';\n";
}
}
}
}
if (!empty($_template->required_plugins['nocache'])) {
$_template->has_nocache_code = true;
$output .= "echo '/*%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
foreach ($_template->required_plugins['nocache'] as $tmp) {
foreach ($tmp as $data) {
$file = addslashes($data['file']);
if (is_Array($data['function'])) {
$output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n");
} else {
$output .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n");
}
}
}
$output .= "?>/*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%*/';\n";
}
}
$output .= "?>\n" . $content;
$output .= "<?php }\n}\n?>";
return $output;
}
}

View File

@@ -388,82 +388,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
} }
throw new SmartyException("Unable to find template function '{$name}'"); throw new SmartyException("Unable to find template function '{$name}'");
} }
/**
* Create code frame for compiled and cached templates
*
* @param string $content optional template content
* @param bool $cache flag for cache file
*
* @return string
*/
public function createTemplateCodeFrame($content = '', $cache = false)
{
$plugins_string = '';
// include code for plugins
if (!$cache) {
if (!empty($this->required_plugins['compiled'])) {
$plugins_string = '<?php ';
foreach ($this->required_plugins['compiled'] as $tmp) {
foreach ($tmp as $data) {
$file = addslashes($data['file']);
if (is_Array($data['function'])) {
$plugins_string .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n";
} else {
$plugins_string .= "if (!is_callable('{$data['function']}')) include '{$file}';\n";
}
}
}
$plugins_string .= '?>';
}
if (!empty($this->required_plugins['nocache'])) {
$this->has_nocache_code = true;
$plugins_string .= "<?php echo '/*%%SmartyNocache:{$this->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
foreach ($this->required_plugins['nocache'] as $tmp) {
foreach ($tmp as $data) {
$file = addslashes($data['file']);
if (is_Array($data['function'])) {
$plugins_string .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n");
} else {
$plugins_string .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n");
}
}
}
$plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n";
}
}
// build property code
$this->properties['has_nocache_code'] = $this->has_nocache_code;
$output = '<?php ';
if (!$this->source->recompiled) {
$output .= "/*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/";
if ($this->smarty->direct_access_security) {
$output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
}
}
if ($cache) {
$this->properties['type'] = 'cache';
} else {
$this->properties['type'] = 'compiled';
}
$this->properties['version'] = Smarty::SMARTY_VERSION;
if (!isset($this->properties['unifunc'])) {
$this->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
}
if (!$this->source->recompiled) {
$output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . ',' . ($cache ? 'true' : 'false') . "); /*/%%SmartyHeaderCode%%*/?>\n";
$output .= "<?php if (\$_valid && !is_callable('{$this->properties['unifunc']}')) {function {$this->properties['unifunc']} (\$_smarty_tpl) {\n";
}
$output .= "\$_saved_type = \$_smarty_tpl->properties['type'];\n";
$output .= "\$_smarty_tpl->properties['type'] = \$_smarty_tpl->caching ? 'cache' : 'compiled';?>\n";
$output .= $plugins_string . $content;
$output .= "<?php \$_smarty_tpl->properties['type'] = \$_saved_type;?>\n";
if (!$this->source->recompiled) {
$output .= "<?php }\n}\n?>";
}
return $output;
}
/** /**
* This function is executed automatically when a compiled or cached template file is included * This function is executed automatically when a compiled or cached template file is included
* - Decode saved properties from compiled template and cache files * - Decode saved properties from compiled template and cache files

View File

@@ -252,7 +252,6 @@ abstract class Smarty_Internal_TemplateCompilerBase
} else { } else {
$this->nocache_hash = $template->properties['nocache_hash']; $this->nocache_hash = $template->properties['nocache_hash'];
} }
$template->properties['type'] = $template->caching ? 'cache' : 'compiled';
// flag for nochache sections // flag for nochache sections
$this->nocache = $nocache; $this->nocache = $nocache;
$this->tag_nocache = false; $this->tag_nocache = false;
@@ -332,7 +331,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
if ($this->suppressTemplatePropertyHeader) { if ($this->suppressTemplatePropertyHeader) {
$_compiled_code .= $merged_code; $_compiled_code .= $merged_code;
} else { } else {
$_compiled_code = $template_header . $template->createTemplateCodeFrame($_compiled_code) . $merged_code; $_compiled_code = $template_header . Smarty_Internal_Extension_CodeFrame::create($template, $_compiled_code) . $merged_code;
} }
if (!empty($this->templateFunctionCode)) { if (!empty($this->templateFunctionCode)) {
// run postfilter if required on compiled template code // run postfilter if required on compiled template code
@@ -597,22 +596,21 @@ abstract class Smarty_Internal_TemplateCompilerBase
*/ */
public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
{ {
// re-use object if already exists // check if tag allowed by security
if (isset(self::$_tag_objects[$tag])) { if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
// re-use object if already exists
if (!isset(self::$_tag_objects[$tag])) {
// lazy load internal compiler plugin
$class_name = 'Smarty_Internal_Compile_' . $tag;
if ($this->smarty->loadPlugin($class_name)) {
self::$_tag_objects[$tag] = new $class_name;
} else {
return false;
}
}
// compile this tag // compile this tag
return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3); return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
} }
// lazy load internal compiler plugin
$class_name = 'Smarty_Internal_Compile_' . $tag;
if ($this->smarty->loadPlugin($class_name)) {
// check if tag allowed by security
if (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) {
// use plugin if found
self::$_tag_objects[$tag] = new $class_name;
// compile this tag
return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3);
}
}
// no internal compile plugin for this tag // no internal compile plugin for this tag
return false; return false;
} }