Files
smarty/libs/sysplugins/smarty_internal_templatebase.php

304 lines
11 KiB
PHP
Raw Normal View History

2014-10-18 00:18:11 +02:00
<?php
/**
* Smarty Internal Plugin Smarty Template Base
* This file contains the basic shared methods for template handling
*
* @package Smarty
* @subpackage Template
* @author Uwe Tews
*/
/**
* Class with shared template methods
*
* @package Smarty
* @subpackage Template
*
* @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
* @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
* @method array getAutoloadFilters(string $type = null)
* @local_method Smarty_Internal_TemplateBase registerFilter(string $type, callback $callback, string $name = null)
* @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
* @local_method bool loadFilter(string $type, string $name)
* @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
* @method string getDebugTemplate()
* @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
* @method Smarty_Internal_TemplateBase setDefaultModifier(mixed $modifiers)
* @method Smarty_Internal_TemplateBase addDefaultModifier(mixed $modifiers)
* @method array getDefaultModifier()
* @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
* @method Smarty_Internal_TemplateBase registerResource(string $name, Smarty_Resource $resource_handler)
* @method Smarty_Internal_TemplateBase unregisterResource(string $name)
* @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
* @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
* @local_method Smarty_Internal_TemplateBase registerPlugin(string $type, string $name, callback $callback, bool
* $cacheable = true, mixed $cache_attr = null)
* @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
* @local_method Smarty_Internal_TemplateBase registerObject(string $object_name, object $object, array
* $allowed_methods_properties = array(), bool $format = true, array $block_methods = array())
* @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
* @method object getRegisteredObject(string $object_name)
* @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
* @method Smarty_Internal_TemplateBase createData(Smarty_Internal_Data $parent = null, string $name = null)
* @method array getTags(mixed $template = null)
2014-10-18 00:18:11 +02:00
*/
abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
{
/**
* Set this if you want different sets of cache files for the same
* templates.
*
* @var string
*/
public $cache_id = null;
/**
* Set this if you want different sets of compiled files for the same
* templates.
*
* @var string
*/
public $compile_id = null;
/**
* caching enabled
*
* @var boolean
*/
public $caching = false;
/**
* cache lifetime in seconds
*
* @var integer
*/
public $cache_lifetime = 3600;
2014-10-18 00:18:11 +02:00
/**
* Registers plugin to be used in templates
* NOTE: this method can be safely removed for dynamic loading
*
* @api Smarty::registerPlugin()
* @link http://www.smarty.net/docs/en/api.register.plugin.tpl
2014-10-18 00:18:11 +02:00
*
* @param string $type plugin type
* @param string $name name of template tag
2014-10-18 00:18:11 +02:00
* @param callback $callback PHP callback to register
* @param bool $cacheable if true (default) this function is cache able
* @param mixed $cache_attr caching attributes if any
2014-10-18 00:18:11 +02:00
*
* @return \Smarty|\Smarty_Internal_Template
2014-10-18 00:18:11 +02:00
* @throws SmartyException when the plugin tag is invalid
*/
public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null)
2014-10-18 00:18:11 +02:00
{
/* @var Smarty $smarty */
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (isset($smarty->registered_plugins[$type][$name])) {
throw new SmartyException("Plugin tag \"{$name}\" already registered");
2014-10-18 00:18:11 +02:00
} elseif (!is_callable($callback)) {
throw new SmartyException("Plugin \"{$name}\" not callable");
2014-10-18 00:18:11 +02:00
} else {
$smarty->registered_plugins[$type][$name] = array($callback, (bool) $cacheable, (array) $cache_attr);
2014-10-18 00:18:11 +02:00
}
return $this;
}
/**
* load a filter of specified type and name
* NOTE: this method can be safely removed for dynamic loading
2014-10-18 00:18:11 +02:00
*
* @api Smarty::loadFilter()
* @link http://www.smarty.net/docs/en/api.load.filter.tpl
2014-10-18 00:18:11 +02:00
*
* @param string $type filter type
* @param string $name filter name
*
* @return bool
* @throws SmartyException if filter could not be loaded
2014-10-18 00:18:11 +02:00
*/
public function loadFilter($type, $name)
2014-10-18 00:18:11 +02:00
{
/* @var Smarty $smarty */
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (!in_array($type, array('pre', 'post', 'output', 'variable'))) {
throw new SmartyException("Illegal filter type \"{$type}\"");
2014-10-18 00:18:11 +02:00
}
$_plugin = "smarty_{$type}filter_{$name}";
$_filter_name = $_plugin;
if (is_callable($_plugin)) {
$smarty->registered_filters[$type][$_filter_name] = $_plugin;
return true;
}
if ($smarty->loadPlugin($_plugin)) {
if (class_exists($_plugin, false)) {
$_plugin = array($_plugin, 'execute');
}
if (is_callable($_plugin)) {
$smarty->registered_filters[$type][$_filter_name] = $_plugin;
return true;
}
}
throw new SmartyException("{$type}filter \"{$name}\" not found or callable");
2014-10-18 00:18:11 +02:00
}
/**
* Registers a filter function
* NOTE: this method can be safely removed for dynamic loading
2014-10-18 00:18:11 +02:00
*
* @api Smarty::registerFilter()
* @link http://www.smarty.net/docs/en/api.register.filter.tpl
2014-10-18 00:18:11 +02:00
*
* @param string $type filter type
* @param callback $callback
* @param string|null $name optional filter name
2014-10-18 00:18:11 +02:00
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
2014-10-18 00:18:11 +02:00
*/
public function registerFilter($type, $callback, $name = null)
2014-10-18 00:18:11 +02:00
{
/* @var Smarty $smarty */
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
if (!in_array($type, array('pre', 'post', 'output', 'variable'))) {
throw new SmartyException("Illegal filter type \"{$type}\"");
2014-10-18 00:18:11 +02:00
}
$name = isset($name) ? $name : $this->_getFilterName($callback);
if (!is_callable($callback)) {
throw new SmartyException("{$type}filter \"{$name}\" not callable");
}
$smarty->registered_filters[$type][$name] = $callback;
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* Return internal filter name
2014-10-18 00:18:11 +02:00
*
* @param callback $function_name
2014-10-18 00:18:11 +02:00
*
* @return string internal filter name
2014-10-18 00:18:11 +02:00
*/
public function _getFilterName($function_name)
2014-10-18 00:18:11 +02:00
{
if (is_array($function_name)) {
$_class_name = (is_object($function_name[0]) ? get_class($function_name[0]) : $function_name[0]);
2014-10-18 00:18:11 +02:00
return $_class_name . '_' . $function_name[1];
} elseif (is_string($function_name)) {
return $function_name;
} else {
return 'closure';
2014-10-18 00:18:11 +02:00
}
}
/**
* Registers object to be used in templates
* NOTE: this method can be safely removed for dynamic loading
2014-10-18 00:18:11 +02:00
*
* @api Smarty::registerObject()
* @link http://www.smarty.net/docs/en/api.register.object.tpl
2014-10-18 00:18:11 +02:00
*
* @param string $object_name
* @param object $object the referenced PHP object to register
* @param array $allowed_methods_properties list of allowed methods (empty = all)
* @param bool $format smarty argument format, else traditional
* @param array $block_methods list of block-methods
2014-10-18 00:18:11 +02:00
*
* @return \Smarty|\Smarty_Internal_Template
* @throws \SmartyException
2014-10-18 00:18:11 +02:00
*/
public function registerObject($object_name, $object, $allowed_methods_properties = array(), $format = true, $block_methods = array())
2014-10-18 00:18:11 +02:00
{
/* @var Smarty $smarty */
2015-02-15 01:45:37 +01:00
$smarty = isset($this->smarty) ? $this->smarty : $this;
// test if allowed methods callable
if (!empty($allowed_methods_properties)) {
foreach ((array) $allowed_methods_properties as $method) {
if (!is_callable(array($object, $method)) && !property_exists($object, $method)) {
throw new SmartyException("Undefined method or property '$method' in registered object");
}
}
2014-10-18 00:18:11 +02:00
}
// test if block methods callable
if (!empty($block_methods)) {
foreach ((array) $block_methods as $method) {
if (!is_callable(array($object, $method))) {
throw new SmartyException("Undefined method '$method' in registered object");
}
}
}
// register the object
$smarty->registered_objects[$object_name] = array($object, (array) $allowed_methods_properties,
(boolean) $format, (array) $block_methods);
2014-10-18 00:18:11 +02:00
return $this;
}
/**
* test if cache is valid
2014-10-18 00:18:11 +02:00
*
* @api Smarty::isCached()
* @link http://www.smarty.net/docs/en/api.is.cached.tpl
2014-10-18 00:18:11 +02:00
*
* @param string|\Smarty_Internal_Template $template the resource handle of the template file or template object
* @param mixed $cache_id cache id to be used with this template
* @param mixed $compile_id compile id to be used with this template
* @param object $parent next higher level of Smarty variables
2014-10-18 00:18:11 +02:00
*
* @return boolean cache status
2014-10-18 00:18:11 +02:00
*/
public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
2014-10-18 00:18:11 +02:00
{
if ($template === null && $this instanceof $this->template_class) {
$template = $this;
} else {
if (!($template instanceof $this->template_class)) {
if ($parent === null) {
$parent = $this;
}
/* @var Smarty $smarty */
$smarty = isset($this->smarty) ? $this->smarty : $this;
$template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
}
}
// return cache status of template
if (!isset($template->cached)) {
$template->loadCached();
}
return $template->cached->isCached($template);
2014-10-18 00:18:11 +02:00
}
/**
* @param boolean $caching
2014-10-18 00:18:11 +02:00
*/
public function setCaching($caching)
2014-10-18 00:18:11 +02:00
{
$this->caching = $caching;
2014-10-18 00:18:11 +02:00
}
/**
* @param int $cache_lifetime
2014-10-18 00:18:11 +02:00
*/
public function setCacheLifetime($cache_lifetime)
2014-10-18 00:18:11 +02:00
{
$this->cache_lifetime = $cache_lifetime;
2014-10-18 00:18:11 +02:00
}
/**
* @param string $compile_id
2014-10-18 00:18:11 +02:00
*/
public function setCompileId($compile_id)
2014-10-18 00:18:11 +02:00
{
$this->compile_id = $compile_id;
2014-10-18 00:18:11 +02:00
}
/**
* @param string $cache_id
2014-10-18 00:18:11 +02:00
*/
public function setCacheId($cache_id)
2014-10-18 00:18:11 +02:00
{
$this->cache_id = $cache_id;
2014-10-18 00:18:11 +02:00
}
}
2015-02-15 01:45:37 +01:00