smarty_template_compiled.php (9221B)
1 <?php 2 3 /** 4 * Smarty Resource Data Object 5 * Meta Data Container for Template Files 6 * 7 * @package Smarty 8 * @subpackage TemplateResources 9 * @author Rodney Rehm 10 * @property string $content compiled content 11 */ 12 class Smarty_Template_Compiled 13 { 14 /** 15 * Compiled Filepath 16 * 17 * @var string 18 */ 19 public $filepath = null; 20 21 /** 22 * Compiled Timestamp 23 * 24 * @var integer 25 */ 26 public $timestamp = null; 27 28 /** 29 * Compiled Existence 30 * 31 * @var boolean 32 */ 33 public $exists = false; 34 35 /** 36 * Compiled Content Loaded 37 * 38 * @var boolean 39 */ 40 public $processed = false; 41 /** 42 * Code of recompiled template resource 43 * 44 * @var string|null 45 */ 46 public $code = null; 47 48 /** 49 * create Compiled Object container 50 */ 51 public function __construct() 52 { 53 } 54 55 /** 56 * get a Compiled Object of this source 57 * 58 * @param Smarty_Internal_Template $_template template object 59 * 60 * @return Smarty_Template_Compiled compiled object 61 */ 62 static function load($_template) 63 { 64 if (!isset($_template->source)) { 65 $_template->loadSource(); 66 } 67 // check runtime cache 68 if (!$_template->source->recompiled && $_template->smarty->resource_caching) { 69 $_cache_key = $_template->source->unique_resource . '#'; 70 if ($_template->caching) { 71 $_cache_key .= 'caching#'; 72 } 73 $_cache_key .= $_template->compile_id; 74 if (isset($_template->source->compileds[$_cache_key])) { 75 return $_template->source->compileds[$_cache_key]; 76 } 77 } 78 $compiled = new Smarty_Template_Compiled(); 79 if (method_exists($_template->source->handler, 'populateCompiledFilepath')) { 80 $_template->source->handler->populateCompiledFilepath($compiled, $_template); 81 } else { 82 $compiled->populateCompiledFilepath($_template); 83 } 84 // runtime cache 85 if (!$_template->source->recompiled && $_template->smarty->resource_caching) { 86 $_template->source->compileds[$_cache_key] = $compiled; 87 } 88 return $compiled; 89 } 90 91 /** 92 * populate Compiled Object with compiled filepath 93 * 94 * @param Smarty_Internal_Template $_template template object 95 **/ 96 public function populateCompiledFilepath(Smarty_Internal_Template $_template) 97 { 98 $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null; 99 if ($_template->source->isConfig) { 100 $_flag = '_' . ((int) $_template->smarty->config_read_hidden + (int) $_template->smarty->config_booleanize * 2 101 + (int) $_template->smarty->config_overwrite * 4); 102 } else { 103 $_flag = '_' . ((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2); 104 } 105 $_filepath = $_template->source->uid . $_flag; 106 // if use_sub_dirs, break file into directories 107 if ($_template->smarty->use_sub_dirs) { 108 $_filepath = substr($_filepath, 0, 2) . DS 109 . substr($_filepath, 2, 2) . DS 110 . substr($_filepath, 4, 2) . DS 111 . $_filepath; 112 } 113 $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^'; 114 if (isset($_compile_id)) { 115 $_filepath = $_compile_id . $_compile_dir_sep . $_filepath; 116 } 117 // caching token 118 if ($_template->caching) { 119 $_cache = '.cache'; 120 } else { 121 $_cache = ''; 122 } 123 $_compile_dir = $_template->smarty->getCompileDir(); 124 // set basename if not specified 125 $_basename = $_template->source->handler->getBasename($_template->source); 126 if ($_basename === null) { 127 $_basename = basename(preg_replace('![^\w\/]+!', '_', $_template->source->name)); 128 } 129 // separate (optional) basename by dot 130 if ($_basename) { 131 $_basename = '.' . $_basename; 132 } 133 134 $this->filepath = $_compile_dir . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php'; 135 $this->timestamp = $this->exists = is_file($this->filepath); 136 if ($this->exists) { 137 $this->timestamp = @filemtime($this->filepath); 138 } 139 } 140 141 /** 142 * load compiled template or compile from source 143 * 144 * @param Smarty_Internal_Template $_template 145 * 146 * @throws Exception 147 */ 148 public function process(Smarty_Internal_Template $_template) 149 { 150 $_smarty_tpl = $_template; 151 if ($_template->source->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile) { 152 $this->compileTemplateSource($_template); 153 $compileCheck = $_template->smarty->compile_check; 154 $_template->smarty->compile_check = false; 155 if ($_template->source->recompiled) { 156 $level = ob_get_level(); 157 ob_start(); 158 try { 159 eval("?>" . $this->code); 160 } 161 catch (Exception $e) { 162 while (ob_get_level() > $level) { 163 ob_end_clean(); 164 } 165 throw $e; 166 } 167 ob_get_clean(); 168 $this->code = null; 169 } else { 170 include($_template->compiled->filepath); 171 } 172 $_template->smarty->compile_check = $compileCheck; 173 } else { 174 include($_template->compiled->filepath); 175 if ($_template->mustCompile) { 176 $this->compileTemplateSource($_template); 177 $compileCheck = $_template->smarty->compile_check; 178 $_template->smarty->compile_check = false; 179 include($_template->compiled->filepath); 180 $_template->smarty->compile_check = $compileCheck; 181 } 182 } 183 $this->unifunc = $_template->properties['unifunc']; 184 $this->processed = true; 185 } 186 187 /** 188 * render compiled template code 189 * 190 * @param Smarty_Internal_Template $_template 191 * 192 * @return string 193 * @throws Exception 194 */ 195 public function render(Smarty_Internal_Template $_template) 196 { 197 198 if (!$this->processed) { 199 $this->process($_template); 200 } 201 $_template->properties['unifunc'] = $this->unifunc; 202 return $_template->getRenderedTemplateCode(); 203 } 204 205 /** 206 * compile template from source 207 * 208 * @param Smarty_Internal_Template $_template 209 * 210 * @return string 211 * @throws Exception 212 */ 213 public function compileTemplateSource(Smarty_Internal_Template $_template) 214 { 215 if (!$_template->source->recompiled) { 216 $_template->properties['file_dependency'] = array(); 217 } 218 // compile locking 219 if (!$_template->source->recompiled) { 220 if ($saved_timestamp = $_template->compiled->timestamp) { 221 touch($_template->compiled->filepath); 222 } 223 } 224 // call compiler 225 try { 226 $code = $_template->compiler->compileTemplate($_template); 227 } 228 catch (Exception $e) { 229 // restore old timestamp in case of error 230 if (!$_template->source->recompiled && $saved_timestamp) { 231 touch($_template->compiled->filepath, $saved_timestamp); 232 } 233 throw $e; 234 } 235 // compiling succeeded 236 if ($_template->compiler->write_compiled_code) { 237 // write compiled template 238 $this->write($_template, $code); 239 $code = ''; 240 } 241 // release compiler object to free memory 242 unset($_template->compiler); 243 return $code; 244 } 245 246 /** 247 * Write compiled code by handler 248 * 249 * @param Smarty_Internal_Template $_template template object 250 * @param string $code compiled code 251 * 252 * @return boolean success 253 */ 254 public function write(Smarty_Internal_Template $_template, $code) 255 { 256 if (!$_template->source->recompiled) { 257 $obj = new Smarty_Internal_Write_File(); 258 if ($obj->writeFile($this->filepath, $code, $_template->smarty) === true) { 259 $this->timestamp = $this->exists = is_file($this->filepath); 260 if ($this->exists) { 261 $this->timestamp = @filemtime($this->filepath); 262 return true; 263 } 264 } 265 return false; 266 } else { 267 $this->code = $code; 268 } 269 $this->timestamp = time(); 270 $this->exists = true; 271 return true; 272 } 273 274 /** 275 * Read compiled content from handler 276 * 277 * @param Smarty_Internal_Template $_template template object 278 * 279 * @return string content 280 */ 281 public function read(Smarty_Internal_Template $_template) 282 { 283 if (!$_template->source->recompiled) { 284 return file_get_contents($this->filepath); 285 } 286 return isset($this->content) ? $this->content : false; 287 } 288 }