You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_resource_custom.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * Wrapper Implementation for custom resource plugins
  12. *
  13. * @package Smarty
  14. * @subpackage TemplateResources
  15. */
  16. abstract class Smarty_Resource_Custom extends Smarty_Resource
  17. {
  18. /**
  19. * fetch template and its modification time from data source
  20. *
  21. * @param string $name template name
  22. * @param string &$source template source
  23. * @param integer &$mtime template modification timestamp (epoch)
  24. */
  25. abstract protected function fetch($name, &$source, &$mtime);
  26. /**
  27. * Fetch template's modification timestamp from data source
  28. * {@internal implementing this method is optional.
  29. * Only implement it if modification times can be accessed faster than loading the complete template source.}}
  30. *
  31. * @param string $name template name
  32. *
  33. * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
  34. */
  35. protected function fetchTimestamp($name)
  36. {
  37. return null;
  38. }
  39. /**
  40. * populate Source Object with meta data from Resource
  41. *
  42. * @param Smarty_Template_Source $source source object
  43. * @param Smarty_Internal_Template $_template template object
  44. */
  45. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
  46. {
  47. $source->filepath = $source->type . ':' . $source->name;
  48. $source->uid = sha1($source->type . ':' . $source->name);
  49. $mtime = $this->fetchTimestamp($source->name);
  50. if ($mtime !== null) {
  51. $source->timestamp = $mtime;
  52. } else {
  53. $this->fetch($source->name, $content, $timestamp);
  54. $source->timestamp = isset($timestamp) ? $timestamp : false;
  55. if (isset($content)) {
  56. $source->content = $content;
  57. }
  58. }
  59. $source->exists = !!$source->timestamp;
  60. }
  61. /**
  62. * Load template's source into current template object
  63. *
  64. * @param Smarty_Template_Source $source source object
  65. *
  66. * @return string template source
  67. * @throws SmartyException if source cannot be loaded
  68. */
  69. public function getContent(Smarty_Template_Source $source)
  70. {
  71. $this->fetch($source->name, $content, $timestamp);
  72. if (isset($content)) {
  73. return $content;
  74. }
  75. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  76. }
  77. /**
  78. * Determine basename for compiled filename
  79. *
  80. * @param Smarty_Template_Source $source source object
  81. *
  82. * @return string resource's basename
  83. */
  84. public function getBasename(Smarty_Template_Source $source)
  85. {
  86. return basename($source->name);
  87. }
  88. }