PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/Smarty/sysplugins/smarty_internal_templatebase.php

https://gitlab.com/dleonov/my-framework-two
PHP | 324 lines | 115 code | 19 blank | 190 comment | 22 complexity | 8e8df21425ebb0792b18cd3cd03f60e1 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Smarty Template Base
  4. * This file contains the basic shared methods for template handling
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Class with shared smarty/template methods
  12. *
  13. * @package Smarty
  14. * @subpackage Template
  15. *
  16. * @property Smarty $smarty
  17. *
  18. * The following methods will be dynamically loaded by the extension handler when they are called.
  19. * They are located in a corresponding Smarty_Internal_Method_xxxx class
  20. *
  21. * @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
  22. * @method Smarty_Internal_TemplateBase addDefaultModifier(mixed $modifiers)
  23. * @method Smarty_Internal_TemplateBase createData(Smarty_Internal_Data $parent = null, string $name = null)
  24. * @method array getAutoloadFilters(string $type = null)
  25. * @method string getDebugTemplate()
  26. * @method array getDefaultModifier()
  27. * @method array getTags(mixed $template = null)
  28. * @method object getRegisteredObject(string $object_name)
  29. * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
  30. * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
  31. * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
  32. * @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
  33. * @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
  34. * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
  35. * @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
  36. * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
  37. * @method Smarty_Internal_TemplateBase setDefaultModifier(mixed $modifiers)
  38. * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
  39. * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
  40. * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
  41. * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
  42. * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
  43. * @method Smarty_Internal_TemplateBase unregisterResource(string $name)
  44. */
  45. abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
  46. {
  47. /**
  48. * Set this if you want different sets of cache files for the same
  49. * templates.
  50. *
  51. * @var string
  52. */
  53. public $cache_id = null;
  54. /**
  55. * Set this if you want different sets of compiled files for the same
  56. * templates.
  57. *
  58. * @var string
  59. */
  60. public $compile_id = null;
  61. /**
  62. * caching enabled
  63. *
  64. * @var boolean
  65. */
  66. public $caching = false;
  67. /**
  68. * cache lifetime in seconds
  69. *
  70. * @var integer
  71. */
  72. public $cache_lifetime = 3600;
  73. /**
  74. * universal cache
  75. *
  76. * @var array()
  77. */
  78. public $_cache = array();
  79. /**
  80. * fetches a rendered Smarty template
  81. *
  82. * @param string $template the resource handle of the template file or template object
  83. * @param mixed $cache_id cache id to be used with this template
  84. * @param mixed $compile_id compile id to be used with this template
  85. * @param object $parent next higher level of Smarty variables
  86. *
  87. * @throws Exception
  88. * @throws SmartyException
  89. * @return string rendered template output
  90. */
  91. public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
  92. {
  93. $result = $this->_execute($template, $cache_id, $compile_id, $parent, 0);
  94. return $result === null ? ob_get_clean() : $result;
  95. }
  96. /**
  97. * displays a Smarty template
  98. *
  99. * @param string $template the resource handle of the template file or template object
  100. * @param mixed $cache_id cache id to be used with this template
  101. * @param mixed $compile_id compile id to be used with this template
  102. * @param object $parent next higher level of Smarty variables
  103. */
  104. public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
  105. {
  106. // display template
  107. $this->_execute($template, $cache_id, $compile_id, $parent, 1);
  108. }
  109. /**
  110. * test if cache is valid
  111. *
  112. * @api Smarty::isCached()
  113. * @link http://www.smarty.net/docs/en/api.is.cached.tpl
  114. *
  115. * @param null|string|\Smarty_Internal_Template $template the resource handle of the template file or template object
  116. * @param mixed $cache_id cache id to be used with this template
  117. * @param mixed $compile_id compile id to be used with this template
  118. * @param object $parent next higher level of Smarty variables
  119. *
  120. * @return boolean cache status
  121. */
  122. public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
  123. {
  124. return $this->_execute($template, $cache_id, $compile_id, $parent, 2);
  125. }
  126. /**
  127. * fetches a rendered Smarty template
  128. *
  129. * @param string $template the resource handle of the template file or template object
  130. * @param mixed $cache_id cache id to be used with this template
  131. * @param mixed $compile_id compile id to be used with this template
  132. * @param object $parent next higher level of Smarty variables
  133. * @param string $function function type 0 = fetch, 1 = display, 2 = isCache
  134. *
  135. * @return mixed
  136. * @throws \Exception
  137. * @throws \SmartyException
  138. */
  139. private function _execute($template, $cache_id, $compile_id, $parent, $function)
  140. {
  141. $smarty = $this->_objType == 1 ? $this : $this->smarty;
  142. if ($template === null) {
  143. if ($this->_objType != 2) {
  144. throw new SmartyException($function . '():Missing \'$template\' parameter');
  145. } else {
  146. $template = clone $this;
  147. }
  148. } elseif (is_object($template)) {
  149. if (!isset($template->_objType) || $template->_objType != 2) {
  150. throw new SmartyException($function . '():Template object expected');
  151. } else {
  152. /* @var Smarty_Internal_Template $template */
  153. $template = clone $template;
  154. }
  155. } else {
  156. // get template object
  157. /* @var Smarty_Internal_Template $template */
  158. $template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
  159. if ($this->_objType == 1) {
  160. // set caching in template object
  161. $template->caching = $this->caching;
  162. }
  163. }
  164. // fetch template content
  165. $level = ob_get_level();
  166. try {
  167. $_smarty_old_error_level =
  168. isset($smarty->error_reporting) ? error_reporting($smarty->error_reporting) :
  169. null;
  170. if ($function == 2) {
  171. if ($template->caching) {
  172. // return cache status of template
  173. if (!isset($template->cached)) {
  174. $template->loadCached();
  175. }
  176. $result = $template->cached->isCached($template);
  177. $template->smarty->_cache['isCached'][$template->_getTemplateId()] = $template;
  178. } else {
  179. return false;
  180. }
  181. } else {
  182. ob_start();
  183. $template->_mergeVars();
  184. if (!empty(Smarty::$global_tpl_vars)) {
  185. $template->tpl_vars = array_merge(Smarty::$global_tpl_vars, $template->tpl_vars);
  186. }
  187. $result = $template->render(false, $function);
  188. }
  189. if (isset($_smarty_old_error_level)) {
  190. error_reporting($_smarty_old_error_level);
  191. }
  192. return $result;
  193. }
  194. catch (Exception $e) {
  195. while (ob_get_level() > $level) {
  196. ob_end_clean();
  197. }
  198. if (isset($_smarty_old_error_level)) {
  199. error_reporting($_smarty_old_error_level);
  200. }
  201. throw $e;
  202. }
  203. }
  204. /**
  205. * Registers plugin to be used in templates
  206. *
  207. * @api Smarty::registerPlugin()
  208. * @link http://www.smarty.net/docs/en/api.register.plugin.tpl
  209. *
  210. * @param string $type plugin type
  211. * @param string $name name of template tag
  212. * @param callback $callback PHP callback to register
  213. * @param bool $cacheable if true (default) this function is cache able
  214. * @param mixed $cache_attr caching attributes if any
  215. *
  216. * @return \Smarty|\Smarty_Internal_Template
  217. * @throws SmartyException when the plugin tag is invalid
  218. */
  219. public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null)
  220. {
  221. return $this->ext->registerPlugin->registerPlugin($this, $type, $name, $callback, $cacheable, $cache_attr);
  222. }
  223. /**
  224. * load a filter of specified type and name
  225. *
  226. * @api Smarty::loadFilter()
  227. * @link http://www.smarty.net/docs/en/api.load.filter.tpl
  228. *
  229. * @param string $type filter type
  230. * @param string $name filter name
  231. *
  232. * @return bool
  233. * @throws SmartyException if filter could not be loaded
  234. */
  235. public function loadFilter($type, $name)
  236. {
  237. return $this->ext->loadFilter->loadFilter($this, $type, $name);
  238. }
  239. /**
  240. * Registers a filter function
  241. *
  242. * @api Smarty::registerFilter()
  243. * @link http://www.smarty.net/docs/en/api.register.filter.tpl
  244. *
  245. * @param string $type filter type
  246. * @param callback $callback
  247. * @param string|null $name optional filter name
  248. *
  249. * @return \Smarty|\Smarty_Internal_Template
  250. * @throws \SmartyException
  251. */
  252. public function registerFilter($type, $callback, $name = null)
  253. {
  254. return $this->ext->registerFilter->registerFilter($this, $type, $callback, $name);
  255. }
  256. /**
  257. * Registers object to be used in templates
  258. *
  259. * @api Smarty::registerObject()
  260. * @link http://www.smarty.net/docs/en/api.register.object.tpl
  261. *
  262. * @param string $object_name
  263. * @param object $object the referenced PHP object to register
  264. * @param array $allowed_methods_properties list of allowed methods (empty = all)
  265. * @param bool $format smarty argument format, else traditional
  266. * @param array $block_methods list of block-methods
  267. *
  268. * @return \Smarty|\Smarty_Internal_Template
  269. * @throws \SmartyException
  270. */
  271. public function registerObject($object_name, $object, $allowed_methods_properties = array(), $format = true,
  272. $block_methods = array())
  273. {
  274. return $this->ext->registerObject->registerObject($this, $object_name, $object, $allowed_methods_properties,
  275. $format, $block_methods);
  276. }
  277. /**
  278. * @param boolean $caching
  279. */
  280. public function setCaching($caching)
  281. {
  282. $this->caching = $caching;
  283. }
  284. /**
  285. * @param int $cache_lifetime
  286. */
  287. public function setCacheLifetime($cache_lifetime)
  288. {
  289. $this->cache_lifetime = $cache_lifetime;
  290. }
  291. /**
  292. * @param string $compile_id
  293. */
  294. public function setCompileId($compile_id)
  295. {
  296. $this->compile_id = $compile_id;
  297. }
  298. /**
  299. * @param string $cache_id
  300. */
  301. public function setCacheId($cache_id)
  302. {
  303. $this->cache_id = $cache_id;
  304. }
  305. }