smarty_resource_custom.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. *
  12. * Wrapper Implementation for custom resource plugins
  13. *
  14. * @package Smarty
  15. * @subpackage TemplateResources
  16. */
  17. abstract class Smarty_Resource_Custom extends Smarty_Resource {
  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. protected abstract function fetch($name, &$source, &$mtime);
  26. /**
  27. * Fetch template's modification timestamp from data source
  28. *
  29. * {@internal implementing this method is optional.
  30. * Only implement it if modification times can be accessed faster than loading the complete template source.}}
  31. *
  32. * @param string $name template name
  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 = strtolower($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. $source->exists = !!$source->timestamp;
  59. }
  60. /**
  61. * Load template's source into current template object
  62. *
  63. * @param Smarty_Template_Source $source source object
  64. * @return string template source
  65. * @throws SmartyException if source cannot be loaded
  66. */
  67. public function getContent(Smarty_Template_Source $source)
  68. {
  69. $this->fetch($source->name, $content, $timestamp);
  70. if (isset($content)) {
  71. return $content;
  72. }
  73. throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
  74. }
  75. /**
  76. * Determine basename for compiled filename
  77. *
  78. * @param Smarty_Template_Source $source source object
  79. * @return string resource's basename
  80. */
  81. protected function getBasename(Smarty_Template_Source $source)
  82. {
  83. return basename($source->name);
  84. }
  85. }
  86. ?>