smarty_internal_resource_string.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Resource String
  4. *
  5. * @package Smarty
  6. * @subpackage TemplateResources
  7. * @author Uwe Tews
  8. * @author Rodney Rehm
  9. */
  10. /**
  11. * Smarty Internal Plugin Resource String
  12. *
  13. * Implements the strings as resource for Smarty template
  14. *
  15. * {@internal unlike eval-resources the compiled state of string-resources is saved for subsequent access}}
  16. *
  17. * @package Smarty
  18. * @subpackage TemplateResources
  19. */
  20. class Smarty_Internal_Resource_String extends Smarty_Resource {
  21. /**
  22. * populate Source Object with meta data from Resource
  23. *
  24. * @param Smarty_Template_Source $source source object
  25. * @param Smarty_Internal_Template $_template template object
  26. * @return void
  27. */
  28. public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
  29. {
  30. $source->uid = $source->filepath = sha1($source->name);
  31. $source->timestamp = 0;
  32. $source->exists = true;
  33. }
  34. /**
  35. * Load template's source from $resource_name into current template object
  36. *
  37. * @uses decode() to decode base64 and urlencoded template_resources
  38. * @param Smarty_Template_Source $source source object
  39. * @return string template source
  40. */
  41. public function getContent(Smarty_Template_Source $source)
  42. {
  43. return $this->decode($source->name);
  44. }
  45. /**
  46. * decode base64 and urlencode
  47. *
  48. * @param string $string template_resource to decode
  49. * @return string decoded template_resource
  50. */
  51. protected function decode($string)
  52. {
  53. // decode if specified
  54. if (($pos = strpos($string, ':')) !== false) {
  55. if (!strncmp($string, 'base64', 6)) {
  56. return base64_decode(substr($string, 7));
  57. } elseif (!strncmp($string, 'urlencode', 9)) {
  58. return urldecode(substr($string, 10));
  59. }
  60. }
  61. return $string;
  62. }
  63. /**
  64. * modify resource_name according to resource handlers specifications
  65. *
  66. * @param Smarty $smarty Smarty instance
  67. * @param string $resource_name resource_name to make unique
  68. * @return string unique resource name
  69. */
  70. protected function buildUniqueResourceName(Smarty $smarty, $resource_name)
  71. {
  72. return get_class($this) . '#' .$this->decode($resource_name);
  73. }
  74. /**
  75. * Determine basename for compiled filename
  76. *
  77. * Always returns an empty string.
  78. *
  79. * @param Smarty_Template_Source $source source object
  80. * @return string resource's basename
  81. */
  82. protected function getBasename(Smarty_Template_Source $source)
  83. {
  84. return '';
  85. }
  86. }
  87. ?>