PageRenderTime 43ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Associates/Smarty/sysplugins/smarty_internal_template.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 738 lines | 430 code | 45 blank | 263 comment | 125 complexity | 2abb01aa9979f9545fd0a8972ecfbb26 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Template
  4. * This file contains the Smarty template engine
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Main class with template data structures and methods
  12. *
  13. * @package Smarty
  14. * @subpackage Template
  15. * @property Smarty_Template_Source $source
  16. * @property Smarty_Template_Compiled $compiled
  17. * @property Smarty_Template_Cached $cached
  18. */
  19. class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
  20. {
  21. /**
  22. * cache_id
  23. *
  24. * @var string
  25. */
  26. public $cache_id = null;
  27. /**
  28. * $compile_id
  29. * @var string
  30. */
  31. public $compile_id = null;
  32. /**
  33. * caching enabled
  34. *
  35. * @var boolean
  36. */
  37. public $caching = null;
  38. /**
  39. * cache lifetime in seconds
  40. *
  41. * @var integer
  42. */
  43. public $cache_lifetime = null;
  44. /**
  45. * Template resource
  46. *
  47. * @var string
  48. */
  49. public $template_resource = null;
  50. /**
  51. * flag if compiled template is invalid and must be (re)compiled
  52. *
  53. * @var bool
  54. */
  55. public $mustCompile = null;
  56. /**
  57. * flag if template does contain nocache code sections
  58. *
  59. * @var bool
  60. */
  61. public $has_nocache_code = false;
  62. /**
  63. * special compiled and cached template properties
  64. *
  65. * @var array
  66. */
  67. public $properties = array('file_dependency' => array(),
  68. 'nocache_hash' => '',
  69. 'function' => array());
  70. /**
  71. * required plugins
  72. *
  73. * @var array
  74. */
  75. public $required_plugins = array('compiled' => array(), 'nocache' => array());
  76. /**
  77. * Global smarty instance
  78. *
  79. * @var Smarty
  80. */
  81. public $smarty = null;
  82. /**
  83. * blocks for template inheritance
  84. *
  85. * @var array
  86. */
  87. public $block_data = array();
  88. /**
  89. * variable filters
  90. *
  91. * @var array
  92. */
  93. public $variable_filters = array();
  94. /**
  95. * optional log of tag/attributes
  96. *
  97. * @var array
  98. */
  99. public $used_tags = array();
  100. /**
  101. * internal flag to allow relative path in child template blocks
  102. *
  103. * @var bool
  104. */
  105. public $allow_relative_path = false;
  106. /**
  107. * internal capture runtime stack
  108. *
  109. * @var array
  110. */
  111. public $_capture_stack = array(0 => array());
  112. /**
  113. * Create template data object
  114. * Some of the global Smarty settings copied to template scope
  115. * It load the required template resources and cacher plugins
  116. *
  117. * @param string $template_resource template resource string
  118. * @param Smarty $smarty Smarty instance
  119. * @param Smarty_Internal_Template $_parent back pointer to parent object with variables or null
  120. * @param mixed $_cache_id cache id or null
  121. * @param mixed $_compile_id compile id or null
  122. * @param bool $_caching use caching?
  123. * @param int $_cache_lifetime cache life-time in seconds
  124. */
  125. public function __construct($template_resource, $smarty, $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
  126. {
  127. $this->smarty = & $smarty;
  128. // Smarty parameter
  129. $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
  130. $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
  131. $this->caching = $_caching === null ? $this->smarty->caching : $_caching;
  132. if ($this->caching === true) {
  133. $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
  134. }
  135. $this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
  136. $this->parent = $_parent;
  137. // Template resource
  138. $this->template_resource = $template_resource;
  139. // copy block data of template inheritance
  140. if ($this->parent instanceof Smarty_Internal_Template) {
  141. $this->block_data = $this->parent->block_data;
  142. }
  143. }
  144. /**
  145. * Returns if the current template must be compiled by the Smarty compiler
  146. * It does compare the timestamps of template source and the compiled templates and checks the force compile configuration
  147. *
  148. * @throws SmartyException
  149. * @return boolean true if the template must be compiled
  150. */
  151. public function mustCompile()
  152. {
  153. if (!$this->source->exists) {
  154. if ($this->parent instanceof Smarty_Internal_Template) {
  155. $parent_resource = " in '$this->parent->template_resource}'";
  156. } else {
  157. $parent_resource = '';
  158. }
  159. throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
  160. }
  161. if ($this->mustCompile === null) {
  162. $this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || $this->compiled->timestamp === false ||
  163. ($this->smarty->compile_check && $this->compiled->timestamp < $this->source->timestamp)));
  164. }
  165. return $this->mustCompile;
  166. }
  167. /**
  168. * Compiles the template
  169. * If the template is not evaluated the compiled template is saved on disk
  170. */
  171. public function compileTemplateSource()
  172. {
  173. if (!$this->source->recompiled) {
  174. $this->properties['file_dependency'] = array();
  175. if ($this->source->components) {
  176. // for the extends resource the compiler will fill it
  177. // uses real resource for file dependency
  178. // $source = end($this->source->components);
  179. // $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type);
  180. } else {
  181. $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type);
  182. }
  183. }
  184. // compile locking
  185. if ($this->smarty->compile_locking && !$this->source->recompiled) {
  186. if ($saved_timestamp = $this->compiled->timestamp) {
  187. touch($this->compiled->filepath);
  188. }
  189. }
  190. // call compiler
  191. try {
  192. $code = $this->compiler->compileTemplate($this);
  193. }
  194. catch (Exception $e) {
  195. // restore old timestamp in case of error
  196. if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) {
  197. touch($this->compiled->filepath, $saved_timestamp);
  198. }
  199. throw $e;
  200. }
  201. // compiling succeded
  202. if (!$this->source->recompiled && $this->compiler->write_compiled_code) {
  203. // write compiled template
  204. $_filepath = $this->compiled->filepath;
  205. if ($_filepath === false) {
  206. throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to');
  207. }
  208. Smarty_Internal_Write_File::writeFile($_filepath, $code, $this->smarty);
  209. $this->compiled->exists = true;
  210. $this->compiled->isCompiled = true;
  211. }
  212. // release compiler object to free memory
  213. unset($this->compiler);
  214. }
  215. /**
  216. * Writes the cached template output
  217. *
  218. * @param string $content
  219. *
  220. * @return bool
  221. */
  222. public function writeCachedContent($content)
  223. {
  224. if ($this->source->recompiled || !($this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED)) {
  225. // don't write cache file
  226. return false;
  227. }
  228. $this->cached->timestamp = time();
  229. $this->properties['cache_lifetime'] = $this->cache_lifetime;
  230. $this->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
  231. $content = $this->createTemplateCodeFrame($content, true);
  232. /** @var Smarty_Internal_Template $_smarty_tpl
  233. * used in evaluated code
  234. */
  235. $_smarty_tpl = $this;
  236. eval("?>" . $content);
  237. $this->cached->valid = true;
  238. $this->cached->processed = true;
  239. return $this->cached->write($this, $content);
  240. }
  241. /**
  242. * Template code runtime function to get subtemplate content
  243. *
  244. * @param string $template the resource handle of the template file
  245. * @param mixed $cache_id cache id to be used with this template
  246. * @param mixed $compile_id compile id to be used with this template
  247. * @param integer $caching cache mode
  248. * @param integer $cache_lifetime life time of cache data
  249. * @param $data
  250. * @param int $parent_scope scope in which {include} should execute
  251. *
  252. * @returns string template content
  253. */
  254. public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
  255. {
  256. // already in template cache?
  257. if ($this->smarty->allow_ambiguous_resources) {
  258. $_templateId = Smarty_Resource::getUniqueTemplateName($this, $template) . $cache_id . $compile_id;
  259. } else {
  260. $_templateId = $this->smarty->joined_template_dir . '#' . $template . $cache_id . $compile_id;
  261. }
  262. if (isset($_templateId[150])) {
  263. $_templateId = sha1($_templateId);
  264. }
  265. if (isset($this->smarty->template_objects[$_templateId])) {
  266. // clone cached template object because of possible recursive call
  267. $tpl = clone $this->smarty->template_objects[$_templateId];
  268. $tpl->parent = $this;
  269. $tpl->caching = $caching;
  270. $tpl->cache_lifetime = $cache_lifetime;
  271. } else {
  272. $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
  273. }
  274. // get variables from calling scope
  275. if ($parent_scope == Smarty::SCOPE_LOCAL) {
  276. $tpl->tpl_vars = $this->tpl_vars;
  277. $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
  278. } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
  279. $tpl->tpl_vars = & $this->tpl_vars;
  280. } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
  281. $tpl->tpl_vars = & Smarty::$global_tpl_vars;
  282. } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
  283. $tpl->tpl_vars = & $this->tpl_vars;
  284. } else {
  285. $tpl->tpl_vars = & $scope_ptr->tpl_vars;
  286. }
  287. $tpl->config_vars = $this->config_vars;
  288. if (!empty($data)) {
  289. // set up variable values
  290. foreach ($data as $_key => $_val) {
  291. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  292. }
  293. }
  294. return $tpl->fetch(null, null, null, null, false, false, true);
  295. }
  296. /**
  297. * Template code runtime function to set up an inline subtemplate
  298. *
  299. * @param string $template the resource handle of the template file
  300. * @param mixed $cache_id cache id to be used with this template
  301. * @param mixed $compile_id compile id to be used with this template
  302. * @param integer $caching cache mode
  303. * @param integer $cache_lifetime life time of cache data
  304. * @param $data
  305. * @param int $parent_scope scope in which {include} should execute
  306. * @param string $hash nocache hash code
  307. *
  308. * @returns string template content
  309. */
  310. public function setupInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $hash)
  311. {
  312. $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
  313. $tpl->properties['nocache_hash'] = $hash;
  314. // get variables from calling scope
  315. if ($parent_scope == Smarty::SCOPE_LOCAL) {
  316. $tpl->tpl_vars = $this->tpl_vars;
  317. $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
  318. } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
  319. $tpl->tpl_vars = & $this->tpl_vars;
  320. } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
  321. $tpl->tpl_vars = & Smarty::$global_tpl_vars;
  322. } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
  323. $tpl->tpl_vars = & $this->tpl_vars;
  324. } else {
  325. $tpl->tpl_vars = & $scope_ptr->tpl_vars;
  326. }
  327. $tpl->config_vars = $this->config_vars;
  328. if (!empty($data)) {
  329. // set up variable values
  330. foreach ($data as $_key => $_val) {
  331. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  332. }
  333. }
  334. return $tpl;
  335. }
  336. /**
  337. * Create code frame for compiled and cached templates
  338. *
  339. * @param string $content optional template content
  340. * @param bool $cache flag for cache file
  341. *
  342. * @return string
  343. */
  344. public function createTemplateCodeFrame($content = '', $cache = false)
  345. {
  346. $plugins_string = '';
  347. // include code for plugins
  348. if (!$cache) {
  349. if (!empty($this->required_plugins['compiled'])) {
  350. $plugins_string = '<?php ';
  351. foreach ($this->required_plugins['compiled'] as $tmp) {
  352. foreach ($tmp as $data) {
  353. $file = addslashes($data['file']);
  354. if (is_Array($data['function'])) {
  355. $plugins_string .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n";
  356. } else {
  357. $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$file}';\n";
  358. }
  359. }
  360. }
  361. $plugins_string .= '?>';
  362. }
  363. if (!empty($this->required_plugins['nocache'])) {
  364. $this->has_nocache_code = true;
  365. $plugins_string .= "<?php echo '/*%%SmartyNocache:{$this->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
  366. foreach ($this->required_plugins['nocache'] as $tmp) {
  367. foreach ($tmp as $data) {
  368. $file = addslashes($data['file']);
  369. if (is_Array($data['function'])) {
  370. $plugins_string .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n");
  371. } else {
  372. $plugins_string .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n");
  373. }
  374. }
  375. }
  376. $plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n";
  377. }
  378. }
  379. // build property code
  380. $this->properties['has_nocache_code'] = $this->has_nocache_code;
  381. $output = '';
  382. if (!$this->source->recompiled) {
  383. $output = "<?php /*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/";
  384. if ($this->smarty->direct_access_security) {
  385. $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
  386. }
  387. }
  388. if ($cache) {
  389. // remove compiled code of{function} definition
  390. unset($this->properties['function']);
  391. if (!empty($this->smarty->template_functions)) {
  392. // copy code of {function} tags called in nocache mode
  393. foreach ($this->smarty->template_functions as $name => $function_data) {
  394. if (isset($function_data['called_nocache'])) {
  395. foreach ($function_data['called_functions'] as $func_name) {
  396. $this->smarty->template_functions[$func_name]['called_nocache'] = true;
  397. }
  398. }
  399. }
  400. foreach ($this->smarty->template_functions as $name => $function_data) {
  401. if (isset($function_data['called_nocache'])) {
  402. unset($function_data['called_nocache'], $function_data['called_functions'], $this->smarty->template_functions[$name]['called_nocache']);
  403. $this->properties['function'][$name] = $function_data;
  404. }
  405. }
  406. }
  407. }
  408. $this->properties['version'] = Smarty::SMARTY_VERSION;
  409. if (!isset($this->properties['unifunc'])) {
  410. $this->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
  411. }
  412. if (!$this->source->recompiled) {
  413. $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . ',' . ($cache ? 'true' : 'false') . "); /*/%%SmartyHeaderCode%%*/?>\n";
  414. $output .= '<?php if ($_valid && !is_callable(\'' . $this->properties['unifunc'] . '\')) {function ' . $this->properties['unifunc'] . '($_smarty_tpl) {?>';
  415. }
  416. $output .= $plugins_string;
  417. $output .= $content;
  418. if (!$this->source->recompiled) {
  419. $output .= "<?php }} ?>\n";
  420. }
  421. return $output;
  422. }
  423. /**
  424. * This function is executed automatically when a compiled or cached template file is included
  425. * - Decode saved properties from compiled template and cache files
  426. * - Check if compiled or cache file is valid
  427. *
  428. * @param array $properties special template properties
  429. * @param bool $cache flag if called from cache file
  430. *
  431. * @return bool flag if compiled or cache file is valid
  432. */
  433. public function decodeProperties($properties, $cache = false)
  434. {
  435. $this->has_nocache_code = $properties['has_nocache_code'];
  436. $this->properties['nocache_hash'] = $properties['nocache_hash'];
  437. if (isset($properties['cache_lifetime'])) {
  438. $this->properties['cache_lifetime'] = $properties['cache_lifetime'];
  439. }
  440. if (isset($properties['file_dependency'])) {
  441. $this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $properties['file_dependency']);
  442. }
  443. if (!empty($properties['function'])) {
  444. $this->properties['function'] = array_merge($this->properties['function'], $properties['function']);
  445. $this->smarty->template_functions = array_merge($this->smarty->template_functions, $properties['function']);
  446. }
  447. $this->properties['version'] = (isset($properties['version'])) ? $properties['version'] : '';
  448. $this->properties['unifunc'] = $properties['unifunc'];
  449. // check file dependencies at compiled code
  450. $is_valid = true;
  451. if ($this->properties['version'] != Smarty::SMARTY_VERSION) {
  452. $is_valid = false;
  453. } elseif (((!$cache && $this->smarty->compile_check && empty($this->compiled->_properties) && !$this->compiled->isCompiled) || $cache && ($this->smarty->compile_check === true || $this->smarty->compile_check === Smarty::COMPILECHECK_ON)) && !empty($this->properties['file_dependency'])) {
  454. foreach ($this->properties['file_dependency'] as $_file_to_check) {
  455. if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'php') {
  456. if ($this->source->filepath == $_file_to_check[0] && isset($this->source->timestamp)) {
  457. // do not recheck current template
  458. $mtime = $this->source->timestamp;
  459. } else {
  460. // file and php types can be checked without loading the respective resource handlers
  461. $mtime = @filemtime($_file_to_check[0]);
  462. }
  463. } elseif ($_file_to_check[2] == 'string') {
  464. continue;
  465. } else {
  466. $source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]);
  467. $mtime = $source->timestamp;
  468. }
  469. if (!$mtime || $mtime > $_file_to_check[1]) {
  470. $is_valid = false;
  471. break;
  472. }
  473. }
  474. }
  475. if ($cache) {
  476. // CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
  477. if ($this->caching === Smarty::CACHING_LIFETIME_SAVED &&
  478. $this->properties['cache_lifetime'] >= 0 &&
  479. (time() > ($this->cached->timestamp + $this->properties['cache_lifetime']))
  480. ) {
  481. $is_valid = false;
  482. }
  483. $this->cached->valid = $is_valid;
  484. } else {
  485. $this->mustCompile = !$is_valid;
  486. }
  487. // store data in reusable Smarty_Template_Compiled
  488. if (!$cache) {
  489. $this->compiled->_properties = $properties;
  490. }
  491. return $is_valid;
  492. }
  493. /**
  494. * Template code runtime function to create a local Smarty variable for array assignments
  495. *
  496. * @param string $tpl_var tempate variable name
  497. * @param bool $nocache cache mode of variable
  498. * @param int $scope scope of variable
  499. */
  500. public function createLocalArrayVariable($tpl_var, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
  501. {
  502. if (!isset($this->tpl_vars[$tpl_var])) {
  503. $this->tpl_vars[$tpl_var] = new Smarty_variable(array(), $nocache, $scope);
  504. } else {
  505. $this->tpl_vars[$tpl_var] = clone $this->tpl_vars[$tpl_var];
  506. if ($scope != Smarty::SCOPE_LOCAL) {
  507. $this->tpl_vars[$tpl_var]->scope = $scope;
  508. }
  509. if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
  510. settype($this->tpl_vars[$tpl_var]->value, 'array');
  511. }
  512. }
  513. }
  514. /**
  515. * Template code runtime function to get pointer to template variable array of requested scope
  516. *
  517. * @param int $scope requested variable scope
  518. *
  519. * @return array array of template variables
  520. */
  521. public function &getScope($scope)
  522. {
  523. if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
  524. return $this->parent->tpl_vars;
  525. } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
  526. $ptr = $this->parent;
  527. while (!empty($ptr->parent)) {
  528. $ptr = $ptr->parent;
  529. }
  530. return $ptr->tpl_vars;
  531. } elseif ($scope == Smarty::SCOPE_GLOBAL) {
  532. return Smarty::$global_tpl_vars;
  533. }
  534. $null = null;
  535. return $null;
  536. }
  537. /**
  538. * Get parent or root of template parent chain
  539. *
  540. * @param int $scope pqrent or root scope
  541. *
  542. * @return mixed object
  543. */
  544. public function getScopePointer($scope)
  545. {
  546. if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
  547. return $this->parent;
  548. } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
  549. $ptr = $this->parent;
  550. while (!empty($ptr->parent)) {
  551. $ptr = $ptr->parent;
  552. }
  553. return $ptr;
  554. }
  555. return null;
  556. }
  557. /**
  558. * [util function] counts an array, arrayaccess/traversable or PDOStatement object
  559. *
  560. * @param mixed $value
  561. *
  562. * @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0 for empty elements
  563. */
  564. public function _count($value)
  565. {
  566. if (is_array($value) === true || $value instanceof Countable) {
  567. return count($value);
  568. } elseif ($value instanceof IteratorAggregate) {
  569. // Note: getIterator() returns a Traversable, not an Iterator
  570. // thus rewind() and valid() methods may not be present
  571. return iterator_count($value->getIterator());
  572. } elseif ($value instanceof Iterator) {
  573. return iterator_count($value);
  574. } elseif ($value instanceof PDOStatement) {
  575. return $value->rowCount();
  576. } elseif ($value instanceof Traversable) {
  577. return iterator_count($value);
  578. } elseif ($value instanceof ArrayAccess) {
  579. if ($value->offsetExists(0)) {
  580. return 1;
  581. }
  582. } elseif (is_object($value)) {
  583. return count($value);
  584. }
  585. return 0;
  586. }
  587. /**
  588. * runtime error not matching capture tags
  589. */
  590. public function capture_error()
  591. {
  592. throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
  593. }
  594. /**
  595. * Empty cache for this template
  596. *
  597. * @param integer $exp_time expiration time
  598. *
  599. * @return integer number of cache files deleted
  600. */
  601. public function clearCache($exp_time = null)
  602. {
  603. Smarty_CacheResource::invalidLoadedCache($this->smarty);
  604. return $this->cached->handler->clear($this->smarty, $this->template_name, $this->cache_id, $this->compile_id, $exp_time);
  605. }
  606. /**
  607. * set Smarty property in template context
  608. *
  609. * @param string $property_name property name
  610. * @param mixed $value value
  611. *
  612. * @throws SmartyException
  613. */
  614. public function __set($property_name, $value)
  615. {
  616. switch ($property_name) {
  617. case 'source':
  618. case 'compiled':
  619. case 'cached':
  620. case 'compiler':
  621. $this->$property_name = $value;
  622. return;
  623. // FIXME: routing of template -> smarty attributes
  624. default:
  625. if (property_exists($this->smarty, $property_name)) {
  626. $this->smarty->$property_name = $value;
  627. return;
  628. }
  629. }
  630. throw new SmartyException("invalid template property '$property_name'.");
  631. }
  632. /**
  633. * get Smarty property in template context
  634. *
  635. * @param string $property_name property name
  636. *
  637. * @throws SmartyException
  638. */
  639. public function __get($property_name)
  640. {
  641. switch ($property_name) {
  642. case 'source':
  643. if (strlen($this->template_resource) == 0) {
  644. throw new SmartyException('Missing template name');
  645. }
  646. $this->source = Smarty_Resource::source($this);
  647. // cache template object under a unique ID
  648. // do not cache eval resources
  649. if ($this->source->type != 'eval') {
  650. if ($this->smarty->allow_ambiguous_resources) {
  651. $_templateId = $this->source->unique_resource . $this->cache_id . $this->compile_id;
  652. } else {
  653. $_templateId = $this->smarty->joined_template_dir . '#' . $this->template_resource . $this->cache_id . $this->compile_id;
  654. }
  655. if (isset($_templateId[150])) {
  656. $_templateId = sha1($_templateId);
  657. }
  658. $this->smarty->template_objects[$_templateId] = $this;
  659. }
  660. return $this->source;
  661. case 'compiled':
  662. $this->compiled = $this->source->getCompiled($this);
  663. return $this->compiled;
  664. case 'cached':
  665. if (!class_exists('Smarty_Template_Cached')) {
  666. include SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
  667. }
  668. $this->cached = new Smarty_Template_Cached($this);
  669. return $this->cached;
  670. case 'compiler':
  671. $this->smarty->loadPlugin($this->source->compiler_class);
  672. $this->compiler = new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class, $this->smarty);
  673. return $this->compiler;
  674. // FIXME: routing of template -> smarty attributes
  675. default:
  676. if (property_exists($this->smarty, $property_name)) {
  677. return $this->smarty->$property_name;
  678. }
  679. }
  680. throw new SmartyException("template property '$property_name' does not exist.");
  681. }
  682. /**
  683. * Template data object destructor
  684. */
  685. public function __destruct()
  686. {
  687. if ($this->smarty->cache_locking && isset($this->cached) && $this->cached->is_locked) {
  688. $this->cached->handler->releaseLock($this->smarty, $this->cached);
  689. }
  690. }
  691. }