PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Associates/Smarty/sysplugins/smarty_resource.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 913 lines | 438 code | 95 blank | 380 comment | 93 complexity | 007811ae74857de5ce657bbe37015422 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Resource Plugin
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Rodney Rehm
  8. */
  9. /**
  10. * Smarty Resource Plugin
  11. * Base implementation for resource plugins
  12. *
  13. * @package Smarty
  14. * @subpackage TemplateResources
  15. */
  16. abstract class Smarty_Resource
  17. {
  18. /**
  19. * cache for Smarty_Template_Source instances
  20. *
  21. * @var array
  22. */
  23. public static $sources = array();
  24. /**
  25. * cache for Smarty_Template_Compiled instances
  26. *
  27. * @var array
  28. */
  29. public static $compileds = array();
  30. /**
  31. * cache for Smarty_Resource instances
  32. *
  33. * @var array
  34. */
  35. public static $resources = array();
  36. /**
  37. * resource types provided by the core
  38. *
  39. * @var array
  40. */
  41. protected static $sysplugins = array(
  42. 'file' => true,
  43. 'string' => true,
  44. 'extends' => true,
  45. 'stream' => true,
  46. 'eval' => true,
  47. 'php' => true
  48. );
  49. /**
  50. * Name of the Class to compile this resource's contents with
  51. *
  52. * @var string
  53. */
  54. public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
  55. /**
  56. * Name of the Class to tokenize this resource's contents with
  57. *
  58. * @var string
  59. */
  60. public $template_lexer_class = 'Smarty_Internal_Templatelexer';
  61. /**
  62. * Name of the Class to parse this resource's contents with
  63. *
  64. * @var string
  65. */
  66. public $template_parser_class = 'Smarty_Internal_Templateparser';
  67. /**
  68. * Load template's source into current template object
  69. * {@internal The loaded source is assigned to $_template->source->content directly.}}
  70. *
  71. * @param Smarty_Template_Source $source source object
  72. *
  73. * @return string template source
  74. * @throws SmartyException if source cannot be loaded
  75. */
  76. abstract public function getContent(Smarty_Template_Source $source);
  77. /**
  78. * populate Source Object with meta data from Resource
  79. *
  80. * @param Smarty_Template_Source $source source object
  81. * @param Smarty_Internal_Template $_template template object
  82. */
  83. abstract public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null);
  84. /**
  85. * populate Source Object with timestamp and exists from Resource
  86. *
  87. * @param Smarty_Template_Source $source source object
  88. */
  89. public function populateTimestamp(Smarty_Template_Source $source)
  90. {
  91. // intentionally left blank
  92. }
  93. /**
  94. * modify resource_name according to resource handlers specifications
  95. *
  96. * @param Smarty $smarty Smarty instance
  97. * @param string $resource_name resource_name to make unique
  98. * @param boolean $is_config flag for config resource
  99. *
  100. * @return string unique resource name
  101. */
  102. protected function buildUniqueResourceName(Smarty $smarty, $resource_name, $is_config = false)
  103. {
  104. if ($is_config) {
  105. return get_class($this) . '#' . $smarty->joined_config_dir . '#' . $resource_name;
  106. } else {
  107. return get_class($this) . '#' . $smarty->joined_template_dir . '#' . $resource_name;
  108. }
  109. }
  110. /**
  111. * populate Compiled Object with compiled filepath
  112. *
  113. * @param Smarty_Template_Compiled $compiled compiled object
  114. * @param Smarty_Internal_Template $_template template object
  115. */
  116. public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
  117. {
  118. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
  119. $_filepath = $compiled->source->uid;
  120. // if use_sub_dirs, break file into directories
  121. if ($_template->smarty->use_sub_dirs) {
  122. $_filepath = substr($_filepath, 0, 2) . DS
  123. . substr($_filepath, 2, 2) . DS
  124. . substr($_filepath, 4, 2) . DS
  125. . $_filepath;
  126. }
  127. $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
  128. if (isset($_compile_id)) {
  129. $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
  130. }
  131. // caching token
  132. if ($_template->caching) {
  133. $_cache = '.cache';
  134. } else {
  135. $_cache = '';
  136. }
  137. $_compile_dir = $_template->smarty->getCompileDir();
  138. // set basename if not specified
  139. $_basename = $this->getBasename($compiled->source);
  140. if ($_basename === null) {
  141. $_basename = basename(preg_replace('![^\w\/]+!', '_', $compiled->source->name));
  142. }
  143. // separate (optional) basename by dot
  144. if ($_basename) {
  145. $_basename = '.' . $_basename;
  146. }
  147. $compiled->filepath = $_compile_dir . $_filepath . '.' . $compiled->source->type . $_basename . $_cache . '.php';
  148. }
  149. /**
  150. * Normalize Paths "foo/../bar" to "bar"
  151. *
  152. * @param string $_path path to normalize
  153. * @param boolean $ds respect windows directory separator
  154. *
  155. * @return string normalized path
  156. */
  157. protected function normalizePath($_path, $ds = true)
  158. {
  159. if ($ds) {
  160. // don't we all just love windows?
  161. $_path = str_replace('\\', '/', $_path);
  162. }
  163. $offset = 0;
  164. // resolve simples
  165. $_path = preg_replace('#/\./(\./)*#', '/', $_path);
  166. // resolve parents
  167. while (true) {
  168. $_parent = strpos($_path, '/../', $offset);
  169. if (!$_parent) {
  170. break;
  171. } elseif ($_path[$_parent - 1] === '.') {
  172. $offset = $_parent + 3;
  173. continue;
  174. }
  175. $_pos = strrpos($_path, '/', $_parent - strlen($_path) - 1);
  176. if ($_pos === false) {
  177. // don't we all just love windows?
  178. $_pos = $_parent;
  179. }
  180. $_path = substr_replace($_path, '', $_pos, $_parent + 3 - $_pos);
  181. }
  182. if ($ds && DS != '/') {
  183. // don't we all just love windows?
  184. $_path = str_replace('/', '\\', $_path);
  185. }
  186. return $_path;
  187. }
  188. /**
  189. * build template filepath by traversing the template_dir array
  190. *
  191. * @param Smarty_Template_Source $source source object
  192. * @param Smarty_Internal_Template $_template template object
  193. *
  194. * @return string fully qualified filepath
  195. * @throws SmartyException if default template handler is registered but not callable
  196. */
  197. protected function buildFilepath(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  198. {
  199. $file = $source->name;
  200. if ($source instanceof Smarty_Config_Source) {
  201. $_directories = $source->smarty->getConfigDir();
  202. $_default_handler = $source->smarty->default_config_handler_func;
  203. } else {
  204. $_directories = $source->smarty->getTemplateDir();
  205. $_default_handler = $source->smarty->default_template_handler_func;
  206. }
  207. // go relative to a given template?
  208. $_file_is_dotted = $file[0] == '.' && ($file[1] == '.' || $file[1] == '/' || $file[1] == "\\");
  209. if ($_template && $_template->parent instanceof Smarty_Internal_Template && $_file_is_dotted) {
  210. if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) {
  211. throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
  212. }
  213. $file = dirname($_template->parent->source->filepath) . DS . $file;
  214. $_file_exact_match = true;
  215. if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $file)) {
  216. // the path gained from the parent template is relative to the current working directory
  217. // as expansions (like include_path) have already been done
  218. $file = getcwd() . DS . $file;
  219. }
  220. }
  221. // resolve relative path
  222. if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $file)) {
  223. // don't we all just love windows?
  224. $_path = DS . trim($file, '/');
  225. $_was_relative = true;
  226. } else {
  227. // don't we all just love windows?
  228. $_path = str_replace('\\', '/', $file);
  229. }
  230. $_path = $this->normalizePath($_path, false);
  231. if (DS != '/') {
  232. // don't we all just love windows?
  233. $_path = str_replace('/', '\\', $_path);
  234. }
  235. // revert to relative
  236. if (isset($_was_relative)) {
  237. $_path = substr($_path, 1);
  238. }
  239. // this is only required for directories
  240. $file = rtrim($_path, '/\\');
  241. // files relative to a template only get one shot
  242. if (isset($_file_exact_match)) {
  243. return $this->fileExists($source, $file) ? $file : false;
  244. }
  245. // template_dir index?
  246. if (preg_match('#^\[(?P<key>[^\]]+)\](?P<file>.+)$#', $file, $match)) {
  247. $_directory = null;
  248. // try string indexes
  249. if (isset($_directories[$match['key']])) {
  250. $_directory = $_directories[$match['key']];
  251. } elseif (is_numeric($match['key'])) {
  252. // try numeric index
  253. $match['key'] = (int) $match['key'];
  254. if (isset($_directories[$match['key']])) {
  255. $_directory = $_directories[$match['key']];
  256. } else {
  257. // try at location index
  258. $keys = array_keys($_directories);
  259. $_directory = $_directories[$keys[$match['key']]];
  260. }
  261. }
  262. if ($_directory) {
  263. $_file = substr($file, strpos($file, ']') + 1);
  264. $_filepath = $_directory . $_file;
  265. if ($this->fileExists($source, $_filepath)) {
  266. return $_filepath;
  267. }
  268. }
  269. }
  270. $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
  271. // relative file name?
  272. if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $file)) {
  273. foreach ($_directories as $_directory) {
  274. $_filepath = $_directory . $file;
  275. if ($this->fileExists($source, $_filepath)) {
  276. return $this->normalizePath($_filepath);
  277. }
  278. if ($source->smarty->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_directory)) {
  279. // try PHP include_path
  280. if ($_stream_resolve_include_path) {
  281. $_filepath = stream_resolve_include_path($_filepath);
  282. } else {
  283. $_filepath = Smarty_Internal_Get_Include_Path::getIncludePath($_filepath);
  284. }
  285. if ($_filepath !== false) {
  286. if ($this->fileExists($source, $_filepath)) {
  287. return $this->normalizePath($_filepath);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. // try absolute filepath
  294. if ($this->fileExists($source, $file)) {
  295. return $file;
  296. }
  297. // no tpl file found
  298. if ($_default_handler) {
  299. if (!is_callable($_default_handler)) {
  300. if ($source instanceof Smarty_Config_Source) {
  301. throw new SmartyException("Default config handler not callable");
  302. } else {
  303. throw new SmartyException("Default template handler not callable");
  304. }
  305. }
  306. $_return = call_user_func_array($_default_handler,
  307. array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty));
  308. if (is_string($_return)) {
  309. $source->timestamp = @filemtime($_return);
  310. $source->exists = !!$source->timestamp;
  311. return $_return;
  312. } elseif ($_return === true) {
  313. $source->content = $_content;
  314. $source->timestamp = $_timestamp;
  315. $source->exists = true;
  316. return $_filepath;
  317. }
  318. }
  319. // give up
  320. return false;
  321. }
  322. /**
  323. * test is file exists and save timestamp
  324. *
  325. * @param Smarty_Template_Source $source source object
  326. * @param string $file file name
  327. *
  328. * @return bool true if file exists
  329. */
  330. protected function fileExists(Smarty_Template_Source $source, $file)
  331. {
  332. $source->timestamp = is_file($file) ? @filemtime($file) : false;
  333. return $source->exists = !!$source->timestamp;
  334. }
  335. /**
  336. * Determine basename for compiled filename
  337. *
  338. * @param Smarty_Template_Source $source source object
  339. *
  340. * @return string resource's basename
  341. */
  342. protected function getBasename(Smarty_Template_Source $source)
  343. {
  344. return null;
  345. }
  346. /**
  347. * Load Resource Handler
  348. *
  349. * @param Smarty $smarty smarty object
  350. * @param string $type name of the resource
  351. *
  352. * @throws SmartyException
  353. * @return Smarty_Resource Resource Handler
  354. */
  355. public static function load(Smarty $smarty, $type)
  356. {
  357. // try smarty's cache
  358. if (isset($smarty->_resource_handlers[$type])) {
  359. return $smarty->_resource_handlers[$type];
  360. }
  361. // try registered resource
  362. if (isset($smarty->registered_resources[$type])) {
  363. if ($smarty->registered_resources[$type] instanceof Smarty_Resource) {
  364. $smarty->_resource_handlers[$type] = $smarty->registered_resources[$type];
  365. // note registered to smarty is not kept unique!
  366. return $smarty->_resource_handlers[$type];
  367. }
  368. if (!isset(self::$resources['registered'])) {
  369. self::$resources['registered'] = new Smarty_Internal_Resource_Registered();
  370. }
  371. if (!isset($smarty->_resource_handlers[$type])) {
  372. $smarty->_resource_handlers[$type] = self::$resources['registered'];
  373. }
  374. return $smarty->_resource_handlers[$type];
  375. }
  376. // try sysplugins dir
  377. if (isset(self::$sysplugins[$type])) {
  378. if (!isset(self::$resources[$type])) {
  379. $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
  380. self::$resources[$type] = new $_resource_class();
  381. }
  382. return $smarty->_resource_handlers[$type] = self::$resources[$type];
  383. }
  384. // try plugins dir
  385. $_resource_class = 'Smarty_Resource_' . ucfirst($type);
  386. if ($smarty->loadPlugin($_resource_class)) {
  387. if (isset(self::$resources[$type])) {
  388. return $smarty->_resource_handlers[$type] = self::$resources[$type];
  389. }
  390. if (class_exists($_resource_class, false)) {
  391. self::$resources[$type] = new $_resource_class();
  392. return $smarty->_resource_handlers[$type] = self::$resources[$type];
  393. } else {
  394. $smarty->registerResource($type, array(
  395. "smarty_resource_{$type}_source",
  396. "smarty_resource_{$type}_timestamp",
  397. "smarty_resource_{$type}_secure",
  398. "smarty_resource_{$type}_trusted"
  399. ));
  400. // give it another try, now that the resource is registered properly
  401. return self::load($smarty, $type);
  402. }
  403. }
  404. // try streams
  405. $_known_stream = stream_get_wrappers();
  406. if (in_array($type, $_known_stream)) {
  407. // is known stream
  408. if (is_object($smarty->security_policy)) {
  409. $smarty->security_policy->isTrustedStream($type);
  410. }
  411. if (!isset(self::$resources['stream'])) {
  412. self::$resources['stream'] = new Smarty_Internal_Resource_Stream();
  413. }
  414. return $smarty->_resource_handlers[$type] = self::$resources['stream'];
  415. }
  416. // TODO: try default_(template|config)_handler
  417. // give up
  418. throw new SmartyException("Unknown resource type '{$type}'");
  419. }
  420. /**
  421. * extract resource_type and resource_name from template_resource and config_resource
  422. * @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
  423. *
  424. * @param string $resource_name template_resource or config_resource to parse
  425. * @param string $default_resource the default resource_type defined in $smarty
  426. * @param string &$name the parsed resource name
  427. * @param string &$type the parsed resource type
  428. *
  429. * @return void
  430. */
  431. protected static function parseResourceName($resource_name, $default_resource, &$name, &$type)
  432. {
  433. $parts = explode(':', $resource_name, 2);
  434. if (!isset($parts[1]) || !isset($parts[0][1])) {
  435. // no resource given, use default
  436. // or single character before the colon is not a resource type, but part of the filepath
  437. $type = $default_resource;
  438. $name = $resource_name;
  439. } else {
  440. $type = $parts[0];
  441. $name = $parts[1];
  442. }
  443. }
  444. /**
  445. * modify resource_name according to resource handlers specifications
  446. *
  447. * @param Smarty $smarty Smarty instance
  448. * @param string $resource_name resource_name to make unique
  449. *
  450. * @return string unique resource name
  451. */
  452. /**
  453. * modify template_resource according to resource handlers specifications
  454. *
  455. * @param Smarty_Internal_template $template Smarty instance
  456. * @param string $template_resource template_resource to extract resource handler and name of
  457. *
  458. * @return string unique resource name
  459. */
  460. public static function getUniqueTemplateName($template, $template_resource)
  461. {
  462. self::parseResourceName($template_resource, $template->smarty->default_resource_type, $name, $type);
  463. // TODO: optimize for Smarty's internal resource types
  464. $resource = Smarty_Resource::load($template->smarty, $type);
  465. // go relative to a given template?
  466. $_file_is_dotted = $name[0] == '.' && ($name[1] == '.' || $name[1] == '/' || $name[1] == "\\");
  467. if ($template instanceof Smarty_Internal_Template && $_file_is_dotted && ($template->source->type == 'file' || $template->parent->source->type == 'extends')) {
  468. $name = dirname($template->source->filepath) . DS . $name;
  469. }
  470. return $resource->buildUniqueResourceName($template->smarty, $name);
  471. }
  472. /**
  473. * initialize Source Object for given resource
  474. * Either [$_template] or [$smarty, $template_resource] must be specified
  475. *
  476. * @param Smarty_Internal_Template $_template template object
  477. * @param Smarty $smarty smarty object
  478. * @param string $template_resource resource identifier
  479. *
  480. * @return Smarty_Template_Source Source Object
  481. */
  482. public static function source(Smarty_Internal_Template $_template = null, Smarty $smarty = null, $template_resource = null)
  483. {
  484. if ($_template) {
  485. $smarty = $_template->smarty;
  486. $template_resource = $_template->template_resource;
  487. }
  488. // parse resource_name, load resource handler, identify unique resource name
  489. self::parseResourceName($template_resource, $smarty->default_resource_type, $name, $type);
  490. $resource = Smarty_Resource::load($smarty, $type);
  491. // go relative to a given template?
  492. $_file_is_dotted = isset($name[0]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/' || $name[1] == "\\");
  493. if ($_file_is_dotted && isset($_template) && $_template->parent instanceof Smarty_Internal_Template && ($_template->parent->source->type == 'file' || $_template->parent->source->type == 'extends')) {
  494. $name2 = dirname($_template->parent->source->filepath) . DS . $name;
  495. } else {
  496. $name2 = $name;
  497. }
  498. $unique_resource_name = $resource->buildUniqueResourceName($smarty, $name2);
  499. // check runtime cache
  500. $_cache_key = 'template|' . $unique_resource_name;
  501. if ($smarty->compile_id) {
  502. $_cache_key .= '|' . $smarty->compile_id;
  503. }
  504. if (isset(self::$sources[$_cache_key])) {
  505. return self::$sources[$_cache_key];
  506. }
  507. // create source
  508. $source = new Smarty_Template_Source($resource, $smarty, $template_resource, $type, $name, $unique_resource_name);
  509. $resource->populate($source, $_template);
  510. // runtime cache
  511. self::$sources[$_cache_key] = $source;
  512. return $source;
  513. }
  514. /**
  515. * initialize Config Source Object for given resource
  516. *
  517. * @param Smarty_Internal_Config $_config config object
  518. *
  519. * @throws SmartyException
  520. * @return Smarty_Config_Source Source Object
  521. */
  522. public static function config(Smarty_Internal_Config $_config)
  523. {
  524. static $_incompatible_resources = array('eval' => true, 'string' => true, 'extends' => true, 'php' => true);
  525. $config_resource = $_config->config_resource;
  526. $smarty = $_config->smarty;
  527. // parse resource_name
  528. self::parseResourceName($config_resource, $smarty->default_config_type, $name, $type);
  529. // make sure configs are not loaded via anything smarty can't handle
  530. if (isset($_incompatible_resources[$type])) {
  531. throw new SmartyException ("Unable to use resource '{$type}' for config");
  532. }
  533. // load resource handler, identify unique resource name
  534. $resource = Smarty_Resource::load($smarty, $type);
  535. $unique_resource_name = $resource->buildUniqueResourceName($smarty, $name, true);
  536. // check runtime cache
  537. $_cache_key = 'config|' . $unique_resource_name;
  538. if (isset(self::$sources[$_cache_key])) {
  539. return self::$sources[$_cache_key];
  540. }
  541. // create source
  542. $source = new Smarty_Config_Source($resource, $smarty, $config_resource, $type, $name, $unique_resource_name);
  543. $resource->populate($source, null);
  544. // runtime cache
  545. self::$sources[$_cache_key] = $source;
  546. return $source;
  547. }
  548. }
  549. /**
  550. * Smarty Resource Data Object
  551. * Meta Data Container for Template Files
  552. *
  553. * @package Smarty
  554. * @subpackage TemplateResources
  555. * @author Rodney Rehm
  556. * @property integer $timestamp Source Timestamp
  557. * @property boolean $exists Source Existence
  558. * @property boolean $template Extended Template reference
  559. * @property string $content Source Content
  560. */
  561. class Smarty_Template_Source
  562. {
  563. /**
  564. * Name of the Class to compile this resource's contents with
  565. *
  566. * @var string
  567. */
  568. public $compiler_class = null;
  569. /**
  570. * Name of the Class to tokenize this resource's contents with
  571. *
  572. * @var string
  573. */
  574. public $template_lexer_class = null;
  575. /**
  576. * Name of the Class to parse this resource's contents with
  577. *
  578. * @var string
  579. */
  580. public $template_parser_class = null;
  581. /**
  582. * Unique Template ID
  583. *
  584. * @var string
  585. */
  586. public $uid = null;
  587. /**
  588. * Template Resource (Smarty_Internal_Template::$template_resource)
  589. *
  590. * @var string
  591. */
  592. public $resource = null;
  593. /**
  594. * Resource Type
  595. *
  596. * @var string
  597. */
  598. public $type = null;
  599. /**
  600. * Resource Name
  601. *
  602. * @var string
  603. */
  604. public $name = null;
  605. /**
  606. * Unique Resource Name
  607. *
  608. * @var string
  609. */
  610. public $unique_resource = null;
  611. /**
  612. * Source Filepath
  613. *
  614. * @var string
  615. */
  616. public $filepath = null;
  617. /**
  618. * Source is bypassing compiler
  619. *
  620. * @var boolean
  621. */
  622. public $uncompiled = null;
  623. /**
  624. * Source must be recompiled on every occasion
  625. *
  626. * @var boolean
  627. */
  628. public $recompiled = null;
  629. /**
  630. * The Components an extended template is made of
  631. *
  632. * @var array
  633. */
  634. public $components = null;
  635. /**
  636. * Resource Handler
  637. *
  638. * @var Smarty_Resource
  639. */
  640. public $handler = null;
  641. /**
  642. * Smarty instance
  643. *
  644. * @var Smarty
  645. */
  646. public $smarty = null;
  647. /**
  648. * create Source Object container
  649. *
  650. * @param Smarty_Resource $handler Resource Handler this source object communicates with
  651. * @param Smarty $smarty Smarty instance this source object belongs to
  652. * @param string $resource full template_resource
  653. * @param string $type type of resource
  654. * @param string $name resource name
  655. * @param string $unique_resource unique resource name
  656. */
  657. public function __construct(Smarty_Resource $handler, Smarty $smarty, $resource, $type, $name, $unique_resource)
  658. {
  659. $this->handler = $handler; // Note: prone to circular references
  660. $this->compiler_class = $handler->compiler_class;
  661. $this->template_lexer_class = $handler->template_lexer_class;
  662. $this->template_parser_class = $handler->template_parser_class;
  663. $this->uncompiled = $this->handler instanceof Smarty_Resource_Uncompiled;
  664. $this->recompiled = $this->handler instanceof Smarty_Resource_Recompiled;
  665. $this->smarty = $smarty;
  666. $this->resource = $resource;
  667. $this->type = $type;
  668. $this->name = $name;
  669. $this->unique_resource = $unique_resource;
  670. }
  671. /**
  672. * get a Compiled Object of this source
  673. *
  674. * @param Smarty_Internal_Template|Smarty_Internal_Config $_template template object
  675. *
  676. * @return Smarty_Template_Compiled compiled object
  677. */
  678. public function getCompiled($_template)
  679. {
  680. // check runtime cache
  681. $_cache_key = $this->unique_resource . '#' . $_template->compile_id;
  682. if (isset(Smarty_Resource::$compileds[$_cache_key])) {
  683. return Smarty_Resource::$compileds[$_cache_key];
  684. }
  685. $compiled = new Smarty_Template_Compiled($this);
  686. $this->handler->populateCompiledFilepath($compiled, $_template);
  687. if(file_exists($compiled->filepath))
  688. $compiled->timestamp = @filemtime($compiled->filepath);
  689. $compiled->exists = !!$compiled->timestamp;
  690. // runtime cache
  691. Smarty_Resource::$compileds[$_cache_key] = $compiled;
  692. return $compiled;
  693. }
  694. /**
  695. * render the uncompiled source
  696. *
  697. * @param Smarty_Internal_Template $_template template object
  698. */
  699. public function renderUncompiled(Smarty_Internal_Template $_template)
  700. {
  701. return $this->handler->renderUncompiled($this, $_template);
  702. }
  703. /**
  704. * <<magic>> Generic Setter.
  705. *
  706. * @param string $property_name valid: timestamp, exists, content, template
  707. * @param mixed $value new value (is not checked)
  708. *
  709. * @throws SmartyException if $property_name is not valid
  710. */
  711. public function __set($property_name, $value)
  712. {
  713. switch ($property_name) {
  714. // regular attributes
  715. case 'timestamp':
  716. case 'exists':
  717. case 'content':
  718. // required for extends: only
  719. case 'template':
  720. $this->$property_name = $value;
  721. break;
  722. default:
  723. throw new SmartyException("invalid source property '$property_name'.");
  724. }
  725. }
  726. /**
  727. * <<magic>> Generic getter.
  728. *
  729. * @param string $property_name valid: timestamp, exists, content
  730. *
  731. * @return mixed
  732. * @throws SmartyException if $property_name is not valid
  733. */
  734. public function __get($property_name)
  735. {
  736. switch ($property_name) {
  737. case 'timestamp':
  738. case 'exists':
  739. $this->handler->populateTimestamp($this);
  740. return $this->$property_name;
  741. case 'content':
  742. return $this->content = $this->handler->getContent($this);
  743. default:
  744. throw new SmartyException("source property '$property_name' does not exist.");
  745. }
  746. }
  747. }
  748. /**
  749. * Smarty Resource Data Object
  750. * Meta Data Container for Template Files
  751. *
  752. * @package Smarty
  753. * @subpackage TemplateResources
  754. * @author Rodney Rehm
  755. * @property string $content compiled content
  756. */
  757. class Smarty_Template_Compiled
  758. {
  759. /**
  760. * Compiled Filepath
  761. *
  762. * @var string
  763. */
  764. public $filepath = null;
  765. /**
  766. * Compiled Timestamp
  767. *
  768. * @var integer
  769. */
  770. public $timestamp = null;
  771. /**
  772. * Compiled Existence
  773. *
  774. * @var boolean
  775. */
  776. public $exists = false;
  777. /**
  778. * Compiled Content Loaded
  779. *
  780. * @var boolean
  781. */
  782. public $loaded = false;
  783. /**
  784. * Template was compiled
  785. *
  786. * @var boolean
  787. */
  788. public $isCompiled = false;
  789. /**
  790. * Source Object
  791. *
  792. * @var Smarty_Template_Source
  793. */
  794. public $source = null;
  795. /**
  796. * Metadata properties
  797. * populated by Smarty_Internal_Template::decodeProperties()
  798. *
  799. * @var array
  800. */
  801. public $_properties = null;
  802. /**
  803. * create Compiled Object container
  804. *
  805. * @param Smarty_Template_Source $source source object this compiled object belongs to
  806. */
  807. public function __construct(Smarty_Template_Source $source)
  808. {
  809. $this->source = $source;
  810. }
  811. }