1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/piwigo_ynh.git synced 2024-09-03 20:06:03 +02:00
piwigo_ynh/sources/include/smarty/libs/sysplugins/smarty_internal_template.php

377 lines
12 KiB
PHP
Raw Normal View History

2014-07-06 13:21:10 +02:00
<?php
/**
* Smarty Internal Plugin Template
* This file contains the Smarty template engine
*
2016-08-27 21:53:29 +02:00
* @package Smarty
2014-07-06 13:21:10 +02:00
* @subpackage Template
2016-08-27 21:53:29 +02:00
* @author Uwe Tews
2014-07-06 13:21:10 +02:00
*/
/**
* Main class with template data structures and methods
*
2016-08-27 21:53:29 +02:00
* @package Smarty
2014-07-06 13:21:10 +02:00
* @subpackage Template
*
2016-08-27 21:53:29 +02:00
* @property Smarty_Template_Source|Smarty_Template_Config $source
* @property Smarty_Template_Compiled $compiled
* @property Smarty_Template_Cached $cached
* @method bool mustCompile()
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
{
2014-07-06 13:21:10 +02:00
/**
2016-08-27 21:53:29 +02:00
* This object type (Smarty = 1, template = 2, data = 4)
*
* @var int
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public $_objType = 2;
2014-07-06 13:21:10 +02:00
/**
2016-08-27 21:53:29 +02:00
* Global smarty instance
*
* @var Smarty
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public $smarty = null;
2014-07-06 13:21:10 +02:00
/**
2016-08-27 21:53:29 +02:00
* Source instance
*
* @var Smarty_Template_Source|Smarty_Template_Config
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public $source = null;
2014-07-06 13:21:10 +02:00
/**
* Template resource
2016-08-27 21:53:29 +02:00
*
2014-07-06 13:21:10 +02:00
* @var string
*/
public $template_resource = null;
2016-08-27 21:53:29 +02:00
2014-07-06 13:21:10 +02:00
/**
* flag if compiled template is invalid and must be (re)compiled
2016-08-27 21:53:29 +02:00
*
2014-07-06 13:21:10 +02:00
* @var bool
*/
public $mustCompile = null;
2016-08-27 21:53:29 +02:00
2014-07-06 13:21:10 +02:00
/**
2016-08-27 21:53:29 +02:00
* Template Id
*
* @var null|string
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public $templateId = null;
2014-07-06 13:21:10 +02:00
/**
2016-08-27 21:53:29 +02:00
* Known template functions
*
2014-07-06 13:21:10 +02:00
* @var array
*/
2016-08-27 21:53:29 +02:00
public $tpl_function = array();
2014-07-06 13:21:10 +02:00
/**
2016-08-27 21:53:29 +02:00
* Scope in which template is rendered
*
* @var int
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public $scope = 0;
2014-07-06 13:21:10 +02:00
/**
* Create template data object
* Some of the global Smarty settings copied to template scope
2016-08-27 21:53:29 +02:00
* It load the required template resources and caching plugins
*
* @param string $template_resource template resource string
* @param Smarty $smarty Smarty instance
* @param \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 $_caching use caching?
* @param int $_cache_lifetime cache life-time in seconds
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @throws \SmartyException
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null,
$_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
2014-07-06 13:21:10 +02:00
{
$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;
2016-08-27 21:53:29 +02:00
if ($this->caching === true) {
2014-07-06 13:21:10 +02:00
$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
2016-08-27 21:53:29 +02:00
}
2014-07-06 13:21:10 +02:00
$this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
$this->parent = $_parent;
// Template resource
$this->template_resource = $template_resource;
2016-08-27 21:53:29 +02:00
$this->source = Smarty_Template_Source::load($this);
parent::__construct();
2014-07-06 13:21:10 +02:00
}
/**
2016-08-27 21:53:29 +02:00
* render template
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @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
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @throws Exception
* @throws SmartyException
* @return string rendered template output
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function render($no_output_filter = true, $display = null)
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
$parentIsTpl = isset($this->parent) && $this->parent->_objType == 2;
if ($this->smarty->debugging) {
$this->smarty->_debug->start_template($this, $display);
}
// checks if template exists
2014-07-06 13:21:10 +02:00
if (!$this->source->exists) {
2016-08-27 21:53:29 +02:00
if ($parentIsTpl) {
$parent_resource = " in '{$this->parent->template_resource}'";
2014-07-06 13:21:10 +02:00
} else {
$parent_resource = '';
}
throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
}
2016-08-27 21:53:29 +02:00
// 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);
2014-07-06 13:21:10 +02:00
}
2016-08-27 21:53:29 +02:00
// 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());
2014-07-06 13:21:10 +02:00
} else {
2016-08-27 21:53:29 +02:00
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();
}
2014-07-06 13:21:10 +02:00
}
2016-08-27 21:53:29 +02:00
if ($this->smarty->debugging) {
$this->smarty->_debug->end_template($this);
// debug output
$this->smarty->_debug->display_debug($this, true);
2014-07-06 13:21:10 +02:00
}
2016-08-27 21:53:29 +02:00
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);
}
2014-07-06 13:21:10 +02:00
}
2016-08-27 21:53:29 +02:00
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;
2014-07-06 13:21:10 +02:00
}
}
/**
2016-08-27 21:53:29 +02:00
* Compiles the template
* If the template is not evaluated the compiled template is saved on disk
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function compileTemplateSource()
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
return $this->compiled->compileTemplateSource($this);
2014-07-06 13:21:10 +02:00
}
/**
2016-08-27 21:53:29 +02:00
* Writes the content to cache resource
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @param string $content
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @return bool
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function writeCachedContent($content)
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
return $this->smarty->ext->_updateCache->writeCachedContent($this->cached, $this, $content);
2014-07-06 13:21:10 +02:00
}
/**
2016-08-27 21:53:29 +02:00
* Get unique template id
2014-07-06 13:21:10 +02:00
*
* @return string
*/
2016-08-27 21:53:29 +02:00
public function _getTemplateId()
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
return isset($this->templateId) ? $this->templateId : $this->templateId =
$this->smarty->_getTemplateId($this->template_resource, $this->cache_id, $this->compile_id);
2014-07-06 13:21:10 +02:00
}
/**
2016-08-27 21:53:29 +02:00
* runtime error not matching capture tags
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function capture_error()
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
2014-07-06 13:21:10 +02:00
}
/**
2016-08-27 21:53:29 +02:00
* Load compiled object
2014-07-06 13:21:10 +02:00
*
*/
2016-08-27 21:53:29 +02:00
public function loadCompiled()
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
if (!isset($this->compiled)) {
$this->compiled = Smarty_Template_Compiled::load($this);
2014-07-06 13:21:10 +02:00
}
}
/**
2016-08-27 21:53:29 +02:00
* Load cached object
2014-07-06 13:21:10 +02:00
*
*/
2016-08-27 21:53:29 +02:00
public function loadCached()
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
if (!isset($this->cached)) {
$this->cached = Smarty_Template_Cached::load($this);
2014-07-06 13:21:10 +02:00
}
}
/**
2016-08-27 21:53:29 +02:00
* Load compiler object
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @throws \SmartyException
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function loadCompiler()
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
if (!class_exists($this->source->handler->compiler_class)) {
$this->smarty->loadPlugin($this->source->handler->compiler_class);
2014-07-06 13:21:10 +02:00
}
2016-08-27 21:53:29 +02:00
$this->compiler = new $this->source->handler->compiler_class($this->source->handler->template_lexer_class,
$this->source->handler->template_parser_class,
$this->smarty);
2014-07-06 13:21:10 +02:00
}
/**
2016-08-27 21:53:29 +02:00
* Handle unknown class methods
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @param string $name unknown method-name
* @param array $args argument array
2014-07-06 13:21:10 +02:00
*
2016-08-27 21:53:29 +02:00
* @return mixed
* @throws SmartyException
2014-07-06 13:21:10 +02:00
*/
2016-08-27 21:53:29 +02:00
public function __call($name, $args)
2014-07-06 13:21:10 +02:00
{
2016-08-27 21:53:29 +02:00
// method of Smarty object?
if (method_exists($this->smarty, $name)) {
return call_user_func_array(array($this->smarty, $name), $args);
}
// parent
return parent::__call($name, $args);
2014-07-06 13:21:10 +02:00
}
/**
* set Smarty property in template context
*
* @param string $property_name property name
* @param mixed $value value
2016-08-27 21:53:29 +02:00
*
* @throws SmartyException
2014-07-06 13:21:10 +02:00
*/
public function __set($property_name, $value)
{
switch ($property_name) {
case 'compiled':
case 'cached':
case 'compiler':
$this->$property_name = $value;
return;
default:
2016-08-27 21:53:29 +02:00
// Smarty property ?
2014-07-06 13:21:10 +02:00
if (property_exists($this->smarty, $property_name)) {
$this->smarty->$property_name = $value;
return;
}
}
throw new SmartyException("invalid template property '$property_name'.");
}
/**
* get Smarty property in template context
*
* @param string $property_name property name
2016-08-27 21:53:29 +02:00
*
* @return mixed|Smarty_Template_Cached
* @throws SmartyException
2014-07-06 13:21:10 +02:00
*/
public function __get($property_name)
{
switch ($property_name) {
case 'compiled':
2016-08-27 21:53:29 +02:00
$this->loadCompiled();
2014-07-06 13:21:10 +02:00
return $this->compiled;
case 'cached':
2016-08-27 21:53:29 +02:00
$this->loadCached();
2014-07-06 13:21:10 +02:00
return $this->cached;
case 'compiler':
2016-08-27 21:53:29 +02:00
$this->loadCompiler();
2014-07-06 13:21:10 +02:00
return $this->compiler;
default:
2016-08-27 21:53:29 +02:00
// Smarty property ?
2014-07-06 13:21:10 +02:00
if (property_exists($this->smarty, $property_name)) {
return $this->smarty->$property_name;
}
}
throw new SmartyException("template property '$property_name' does not exist.");
}
/**
2016-08-27 21:53:29 +02:00
* Template data object destructor
2014-07-06 13:21:10 +02:00
*/
public function __destruct()
{
if ($this->smarty->cache_locking && isset($this->cached) && $this->cached->is_locked) {
$this->cached->handler->releaseLock($this->smarty, $this->cached);
}
}
}