smarty_template_source.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Smarty Resource Data Object
  4. * Meta Data Container for Template Files
  5. *
  6. * @package Smarty
  7. * @subpackage TemplateResources
  8. * @author Rodney Rehm
  9. *
  10. */
  11. class Smarty_Template_Source
  12. {
  13. /**
  14. * Unique Template ID
  15. *
  16. * @var string
  17. */
  18. public $uid = null;
  19. /**
  20. * Template Resource (Smarty_Internal_Template::$template_resource)
  21. *
  22. * @var string
  23. */
  24. public $resource = null;
  25. /**
  26. * Resource Type
  27. *
  28. * @var string
  29. */
  30. public $type = null;
  31. /**
  32. * Resource Name
  33. *
  34. * @var string
  35. */
  36. public $name = null;
  37. /**
  38. * Source Filepath
  39. *
  40. * @var string
  41. */
  42. public $filepath = null;
  43. /**
  44. * Source Timestamp
  45. *
  46. * @var integer
  47. */
  48. public $timestamp = null;
  49. /**
  50. * Source Existence
  51. *
  52. * @var boolean
  53. */
  54. public $exists = false;
  55. /**
  56. * Source File Base name
  57. *
  58. * @var string
  59. */
  60. public $basename = null;
  61. /**
  62. * The Components an extended template is made of
  63. *
  64. * @var \Smarty_Template_Source[]
  65. */
  66. public $components = null;
  67. /**
  68. * Resource Handler
  69. *
  70. * @var \Smarty_Resource
  71. */
  72. public $handler = null;
  73. /**
  74. * Smarty instance
  75. *
  76. * @var Smarty
  77. */
  78. public $smarty = null;
  79. /**
  80. * Resource is source
  81. *
  82. * @var bool
  83. */
  84. public $isConfig = false;
  85. /**
  86. * Template source content eventually set by default handler
  87. *
  88. * @var string
  89. */
  90. public $content = null;
  91. /**
  92. * Name of the Class to compile this resource's contents with
  93. *
  94. * @var string
  95. */
  96. public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
  97. /**
  98. * Name of the Class to tokenize this resource's contents with
  99. *
  100. * @var string
  101. */
  102. public $template_lexer_class = 'Smarty_Internal_Templatelexer';
  103. /**
  104. * Name of the Class to parse this resource's contents with
  105. *
  106. * @var string
  107. */
  108. public $template_parser_class = 'Smarty_Internal_Templateparser';
  109. /**
  110. * create Source Object container
  111. *
  112. * @param Smarty_Resource $handler Resource Handler this source object communicates with
  113. * @param Smarty $smarty Smarty instance this source object belongs to
  114. * @param string $resource full template_resource
  115. * @param string $type type of resource
  116. * @param string $name resource name
  117. *
  118. */
  119. public function __construct(Smarty $smarty, $resource, $type, $name)
  120. {
  121. $this->handler =
  122. isset($smarty->_cache[ 'resource_handlers' ][ $type ]) ? $smarty->_cache[ 'resource_handlers' ][ $type ] :
  123. Smarty_Resource::load($smarty, $type);
  124. $this->smarty = $smarty;
  125. $this->resource = $resource;
  126. $this->type = $type;
  127. $this->name = $name;
  128. }
  129. /**
  130. * initialize Source Object for given resource
  131. * Either [$_template] or [$smarty, $template_resource] must be specified
  132. *
  133. * @param Smarty_Internal_Template $_template template object
  134. * @param Smarty $smarty smarty object
  135. * @param string $template_resource resource identifier
  136. *
  137. * @return Smarty_Template_Source Source Object
  138. * @throws SmartyException
  139. */
  140. public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
  141. $template_resource = null)
  142. {
  143. if ($_template) {
  144. $smarty = $_template->smarty;
  145. $template_resource = $_template->template_resource;
  146. }
  147. if (empty($template_resource)) {
  148. throw new SmartyException('Source: Missing name');
  149. }
  150. // parse resource_name, load resource handler, identify unique resource name
  151. if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]([\s\S]*)$/', $template_resource, $match)) {
  152. $type = $match[ 1 ];
  153. $name = $match[ 2 ];
  154. } else {
  155. // no resource given, use default
  156. // or single character before the colon is not a resource type, but part of the filepath
  157. $type = $smarty->default_resource_type;
  158. $name = $template_resource;
  159. }
  160. // create new source object
  161. $source = new Smarty_Template_Source($smarty, $template_resource, $type, $name);
  162. $source->handler->populate($source, $_template);
  163. if (!$source->exists && isset($_template->smarty->default_template_handler_func)) {
  164. Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
  165. $source->handler->populate($source, $_template);
  166. }
  167. return $source;
  168. }
  169. /**
  170. * Get source time stamp
  171. *
  172. * @return int
  173. */
  174. public function getTimeStamp()
  175. {
  176. if (!isset($this->timestamp)) {
  177. $this->handler->populateTimestamp($this);
  178. }
  179. return $this->timestamp;
  180. }
  181. /**
  182. * Get source content
  183. *
  184. * @return string
  185. */
  186. public function getContent()
  187. {
  188. return isset($this->content) ? $this->content : $this->handler->getContent($this);
  189. }
  190. }