1: <?php
2: /**
3: * Smarty Internal Extension
4: * This file contains the Smarty template extension to create a code frame
5: *
6: * @package Smarty
7: * @subpackage Template
8: * @author Uwe Tews
9: */
10:
11: /**
12: * Class Smarty_Internal_Extension_CodeFrame
13: * Create code frame for compiled and cached templates
14: */
15: class Smarty_Internal_Runtime_CodeFrame
16: {
17: /**
18: * Create code frame for compiled and cached templates
19: *
20: * @param Smarty_Internal_Template $_template
21: * @param string $content optional template content
22: * @param string $functions compiled template function and block code
23: * @param bool $cache flag for cache file
24: * @param \Smarty_Internal_TemplateCompilerBase $compiler
25: *
26: * @return string
27: */
28: public function create(
29: Smarty_Internal_Template $_template,
30: $content = '',
31: $functions = '',
32: $cache = false,
33: Smarty_Internal_TemplateCompilerBase $compiler = null
34: ) {
35: // build property code
36: $properties[ 'version' ] = Smarty::SMARTY_VERSION;
37: $properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
38: if (!$cache) {
39: $properties[ 'has_nocache_code' ] = $_template->compiled->has_nocache_code;
40: $properties[ 'file_dependency' ] = $_template->compiled->file_dependency;
41: $properties[ 'includes' ] = $_template->compiled->includes;
42: } else {
43: $properties[ 'has_nocache_code' ] = $_template->cached->has_nocache_code;
44: $properties[ 'file_dependency' ] = $_template->cached->file_dependency;
45: $properties[ 'cache_lifetime' ] = $_template->cache_lifetime;
46: }
47: $output = sprintf(
48: "<?php\n/* Smarty version %s, created on %s\n from '%s' */\n\n",
49: $properties[ 'version' ],
50: date("Y-m-d H:i:s"),
51: str_replace('*/', '* /', $_template->source->filepath)
52: );
53: $output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n";
54: $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' .
55: ($cache ? 'true' : 'false') . ')';
56: $output .= "if ({$dec}) {\n";
57: $output .= "function {$properties['unifunc']} (Smarty_Internal_Template \$_smarty_tpl) {\n";
58: if (!$cache && !empty($compiler->tpl_function)) {
59: $output .= '$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions($_smarty_tpl, ';
60: $output .= var_export($compiler->tpl_function, true);
61: $output .= ");\n";
62: }
63: if ($cache && isset($_template->smarty->ext->_tplFunction)) {
64: $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
65: var_export($_template->smarty->ext->_tplFunction->getTplFunction($_template), true) . ");\n";
66: }
67: $output .= "?>";
68: $output .= $content;
69: $output .= "<?php }\n?>";
70: $output .= $functions;
71: $output .= "<?php }\n";
72: // remove unneeded PHP tags
73: if (preg_match('/\s*\?>[\n]?<\?php\s*/', $output)) {
74: $curr_split = preg_split(
75: '/\s*\?>[\n]?<\?php\s*/',
76: $output
77: );
78: preg_match_all(
79: '/\s*\?>[\n]?<\?php\s*/',
80: $output,
81: $curr_parts
82: );
83: $output = '';
84: foreach ($curr_split as $idx => $curr_output) {
85: $output .= $curr_output;
86: if (isset($curr_parts[ 0 ][ $idx ])) {
87: $output .= "\n";
88: }
89: }
90: }
91: if (preg_match('/\?>\s*$/', $output)) {
92: $curr_split = preg_split(
93: '/\?>\s*$/',
94: $output
95: );
96: $output = '';
97: foreach ($curr_split as $idx => $curr_output) {
98: $output .= $curr_output;
99: }
100: }
101: return $output;
102: }
103: }
104: