2015-01-01 23:28:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smarty Resource Data Object
|
|
|
|
* Meta Data Container for Template Files
|
|
|
|
*
|
|
|
|
* @package Smarty
|
|
|
|
* @subpackage TemplateResources
|
|
|
|
* @author Rodney Rehm
|
|
|
|
* @property string $content compiled content
|
|
|
|
*/
|
2015-08-09 20:43:04 +02:00
|
|
|
class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
|
2015-01-01 23:28:09 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2015-08-09 20:43:04 +02:00
|
|
|
* nocache hash
|
2015-01-01 23:28:09 +01:00
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2015-08-09 20:43:04 +02:00
|
|
|
public $nocache_hash = null;
|
2015-08-06 00:57:36 +02:00
|
|
|
|
2015-08-18 02:55:53 +02:00
|
|
|
|
2015-01-01 23:28:09 +01:00
|
|
|
/**
|
|
|
|
* create Compiled Object container
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a Compiled Object of this source
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template template object
|
|
|
|
*
|
|
|
|
* @return Smarty_Template_Compiled compiled object
|
|
|
|
*/
|
|
|
|
static function load($_template)
|
|
|
|
{
|
2015-06-21 13:51:50 +02:00
|
|
|
$smarty = $_template->smarty;
|
|
|
|
$source = $_template->source;
|
2015-01-01 23:28:09 +01:00
|
|
|
// check runtime cache
|
2015-08-09 20:43:04 +02:00
|
|
|
if (!$source->recompiled && ($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)) {
|
2015-06-21 13:51:50 +02:00
|
|
|
$_cache_key = $source->unique_resource . '#';
|
2015-01-01 23:28:09 +01:00
|
|
|
if ($_template->caching) {
|
|
|
|
$_cache_key .= 'caching#';
|
|
|
|
}
|
|
|
|
$_cache_key .= $_template->compile_id;
|
2015-06-21 13:51:50 +02:00
|
|
|
if (isset($source->compileds[$_cache_key])) {
|
|
|
|
return $source->compileds[$_cache_key];
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$compiled = new Smarty_Template_Compiled();
|
2015-06-21 13:51:50 +02:00
|
|
|
if (method_exists($source->handler, 'populateCompiledFilepath')) {
|
|
|
|
$source->handler->populateCompiledFilepath($compiled, $_template);
|
2015-01-01 23:28:09 +01:00
|
|
|
} else {
|
|
|
|
$compiled->populateCompiledFilepath($_template);
|
|
|
|
}
|
|
|
|
// runtime cache
|
2015-08-09 20:43:04 +02:00
|
|
|
if (!$source->recompiled && ($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)) {
|
2015-06-21 13:51:50 +02:00
|
|
|
$source->compileds[$_cache_key] = $compiled;
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
return $compiled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* populate Compiled Object with compiled filepath
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template template object
|
|
|
|
**/
|
|
|
|
public function populateCompiledFilepath(Smarty_Internal_Template $_template)
|
|
|
|
{
|
2015-08-06 21:42:20 +02:00
|
|
|
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w]+!', '_', $_template->compile_id) : null;
|
2015-01-03 07:36:07 +01:00
|
|
|
if ($_template->source->isConfig) {
|
2015-07-20 04:01:43 +02:00
|
|
|
$_flag = '_' .
|
|
|
|
((int) $_template->smarty->config_read_hidden + (int) $_template->smarty->config_booleanize * 2 +
|
|
|
|
(int) $_template->smarty->config_overwrite * 4);
|
2015-01-03 07:36:07 +01:00
|
|
|
} else {
|
2015-07-20 04:01:43 +02:00
|
|
|
$_flag = '_' .
|
|
|
|
((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2);
|
2015-01-03 07:36:07 +01:00
|
|
|
}
|
|
|
|
$_filepath = $_template->source->uid . $_flag;
|
2015-01-01 23:28:09 +01:00
|
|
|
// if use_sub_dirs, break file into directories
|
|
|
|
if ($_template->smarty->use_sub_dirs) {
|
2015-07-20 04:01:43 +02:00
|
|
|
$_filepath = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS .
|
|
|
|
$_filepath;
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
$_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
|
|
|
|
if (isset($_compile_id)) {
|
|
|
|
$_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
|
|
|
|
}
|
|
|
|
// caching token
|
|
|
|
if ($_template->caching) {
|
|
|
|
$_cache = '.cache';
|
|
|
|
} else {
|
|
|
|
$_cache = '';
|
|
|
|
}
|
|
|
|
$_compile_dir = $_template->smarty->getCompileDir();
|
|
|
|
// set basename if not specified
|
|
|
|
$_basename = $_template->source->handler->getBasename($_template->source);
|
|
|
|
if ($_basename === null) {
|
2015-08-06 21:42:20 +02:00
|
|
|
$_basename = basename(preg_replace('![^\w]+!', '_', $_template->source->name));
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
// separate (optional) basename by dot
|
|
|
|
if ($_basename) {
|
|
|
|
$_basename = '.' . $_basename;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->filepath = $_compile_dir . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php';
|
2015-07-01 03:27:06 +02:00
|
|
|
$this->exists = is_file($this->filepath);
|
|
|
|
if (!$this->exists) {
|
|
|
|
$this->timestamp = false;
|
2015-05-05 00:41:30 +02:00
|
|
|
}
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* load compiled template or compile from source
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function process(Smarty_Internal_Template $_template)
|
|
|
|
{
|
|
|
|
$_smarty_tpl = $_template;
|
2015-07-20 04:01:43 +02:00
|
|
|
if ($_template->source->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile ||
|
|
|
|
($_template->smarty->compile_check &&
|
|
|
|
$_template->source->getTimeStamp() > $_template->compiled->getTimeStamp())
|
|
|
|
) {
|
2015-01-01 23:28:09 +01:00
|
|
|
$this->compileTemplateSource($_template);
|
|
|
|
$compileCheck = $_template->smarty->compile_check;
|
|
|
|
$_template->smarty->compile_check = false;
|
|
|
|
if ($_template->source->recompiled) {
|
2015-03-01 21:39:54 +01:00
|
|
|
$level = ob_get_level();
|
|
|
|
ob_start();
|
2015-01-01 23:28:09 +01:00
|
|
|
try {
|
2015-08-15 18:25:06 +02:00
|
|
|
eval("?>" . $this->content);
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
2015-03-01 21:39:54 +01:00
|
|
|
while (ob_get_level() > $level) {
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
2015-01-01 23:28:09 +01:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
ob_get_clean();
|
2015-08-15 18:25:06 +02:00
|
|
|
$this->content = null;
|
2015-01-01 23:28:09 +01:00
|
|
|
} else {
|
2015-08-06 00:57:36 +02:00
|
|
|
$this->loadCompiledTemplate($_template);
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
$_template->smarty->compile_check = $compileCheck;
|
|
|
|
} else {
|
|
|
|
include($_template->compiled->filepath);
|
|
|
|
if ($_template->mustCompile) {
|
|
|
|
$this->compileTemplateSource($_template);
|
|
|
|
$compileCheck = $_template->smarty->compile_check;
|
|
|
|
$_template->smarty->compile_check = false;
|
2015-08-06 00:57:36 +02:00
|
|
|
$this->loadCompiledTemplate($_template);
|
2015-01-01 23:28:09 +01:00
|
|
|
$_template->smarty->compile_check = $compileCheck;
|
2015-08-18 02:55:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$_template->source->isConfig && !isset($_template->smarty->template_objects[$_template->templateId]) &&
|
|
|
|
$_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC &&
|
|
|
|
$_template->parent instanceof Smarty_Internal_Template && isset($_template->parent->compiled)
|
|
|
|
) {
|
|
|
|
foreach ($_template->parent->compiled->includes as $key => $count) {
|
|
|
|
$_template->compiled->includes[$key] = isset($_template->compiled->includes[$key]) ? $_template->compiled->includes[$key] +
|
|
|
|
$count : $count;
|
|
|
|
}
|
|
|
|
if (!in_array($_template->source->type, array('eval', 'string')) && $_template->compiled->includes[$_template->source->type . ':' . $_template->source->name] > 1) {
|
|
|
|
$_template->smarty->template_objects[$_template->templateId] = $_template;
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->processed = true;
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:57:36 +02:00
|
|
|
/**
|
|
|
|
* Load fresh compiled template by including the PHP file
|
|
|
|
* HHVM requires a work around because of a PHP incompatibility
|
|
|
|
*
|
|
|
|
* @param \Smarty_Internal_Template $_template
|
|
|
|
*/
|
|
|
|
private function loadCompiledTemplate(Smarty_Internal_Template $_template)
|
|
|
|
{
|
|
|
|
if (function_exists('opcache_invalidate')) {
|
|
|
|
opcache_invalidate($_template->compiled->filepath);
|
|
|
|
}
|
|
|
|
$_smarty_tpl = $_template;
|
|
|
|
if (strpos(phpversion(), 'hhvm') !== false) {
|
|
|
|
Smarty_Internal_Extension_Hhvm::includeHhvm($_template, $_template->compiled->filepath);
|
|
|
|
} else {
|
|
|
|
include($_template->compiled->filepath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:28:09 +01:00
|
|
|
/**
|
|
|
|
* render compiled template code
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function render(Smarty_Internal_Template $_template)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$this->processed) {
|
|
|
|
$this->process($_template);
|
|
|
|
}
|
2015-08-15 18:25:06 +02:00
|
|
|
if (isset($_template->cached)) {
|
2015-08-09 20:43:04 +02:00
|
|
|
$_template->cached->file_dependency = array_merge($_template->cached->file_dependency, $this->file_dependency);
|
|
|
|
}
|
|
|
|
return $_template->getRenderedTemplateCode($this->unifunc);
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* compile template from source
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function compileTemplateSource(Smarty_Internal_Template $_template)
|
|
|
|
{
|
2015-08-06 21:42:20 +02:00
|
|
|
$_template->source->compileds = array();
|
2015-08-09 20:43:04 +02:00
|
|
|
$this->file_dependency = array();
|
|
|
|
$this->tpl_function = array();
|
|
|
|
$this->nocache_hash = null;
|
|
|
|
$this->unifunc = null;
|
2015-01-01 23:28:09 +01:00
|
|
|
// compile locking
|
|
|
|
if (!$_template->source->recompiled) {
|
2015-07-01 03:27:06 +02:00
|
|
|
if ($saved_timestamp = $_template->compiled->getTimeStamp()) {
|
2015-01-01 23:28:09 +01:00
|
|
|
touch($_template->compiled->filepath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// call compiler
|
|
|
|
try {
|
2015-07-06 03:25:03 +02:00
|
|
|
$_template->loadCompiler();
|
2015-01-01 23:28:09 +01:00
|
|
|
$code = $_template->compiler->compileTemplate($_template);
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
// restore old timestamp in case of error
|
|
|
|
if (!$_template->source->recompiled && $saved_timestamp) {
|
|
|
|
touch($_template->compiled->filepath, $saved_timestamp);
|
|
|
|
}
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
// compiling succeeded
|
|
|
|
if ($_template->compiler->write_compiled_code) {
|
|
|
|
// write compiled template
|
|
|
|
$this->write($_template, $code);
|
|
|
|
$code = '';
|
|
|
|
}
|
|
|
|
// release compiler object to free memory
|
|
|
|
unset($_template->compiler);
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write compiled code by handler
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template template object
|
|
|
|
* @param string $code compiled code
|
|
|
|
*
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
|
|
|
public function write(Smarty_Internal_Template $_template, $code)
|
|
|
|
{
|
|
|
|
if (!$_template->source->recompiled) {
|
2015-01-21 21:12:00 +01:00
|
|
|
$obj = new Smarty_Internal_Write_File();
|
|
|
|
if ($obj->writeFile($this->filepath, $code, $_template->smarty) === true) {
|
2015-05-05 00:41:30 +02:00
|
|
|
$this->timestamp = $this->exists = is_file($this->filepath);
|
2015-01-01 23:28:09 +01:00
|
|
|
if ($this->exists) {
|
2015-07-01 03:27:06 +02:00
|
|
|
$this->timestamp = filemtime($this->filepath);
|
2015-01-01 23:28:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-05-10 11:53:09 +02:00
|
|
|
}
|
2015-01-01 23:28:09 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2015-08-15 18:25:06 +02:00
|
|
|
$this->content = $code;
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|
|
|
|
$this->timestamp = time();
|
|
|
|
$this->exists = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read compiled content from handler
|
|
|
|
*
|
|
|
|
* @param Smarty_Internal_Template $_template template object
|
|
|
|
*
|
|
|
|
* @return string content
|
|
|
|
*/
|
|
|
|
public function read(Smarty_Internal_Template $_template)
|
|
|
|
{
|
|
|
|
if (!$_template->source->recompiled) {
|
|
|
|
return file_get_contents($this->filepath);
|
|
|
|
}
|
2015-08-15 18:25:06 +02:00
|
|
|
return isset($this->content) ? $this->content : false;
|
2015-07-01 03:27:06 +02:00
|
|
|
}
|
2015-01-01 23:28:09 +01:00
|
|
|
}
|