3 * Smarty Resource Plugin
6 * @subpackage TemplateResources
11 * Smarty Resource Plugin
12 * Base implementation for resource plugins
15 * @subpackage TemplateResources
17 * @method renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
18 * @method populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
19 * @method process(Smarty_Internal_Template $_smarty_tpl)
21 abstract class Smarty_Resource
24 * Source is bypassing compiler
28 public $uncompiled = false;
31 * Source must be recompiled on every occasion
35 public $recompiled = false;
38 * resource types provided by the core
42 public static $sysplugins = array('file' => 'smarty_internal_resource_file.php',
43 'string' => 'smarty_internal_resource_string.php',
44 'extends' => 'smarty_internal_resource_extends.php',
45 'stream' => 'smarty_internal_resource_stream.php',
46 'eval' => 'smarty_internal_resource_eval.php',
47 'php' => 'smarty_internal_resource_php.php');
50 * Flag if resource does implement populateCompiledFilepath() method
54 public $hasCompiledHandler = false;
57 * Load template's source into current template object
59 * @param Smarty_Template_Source $source source object
61 * @return string template source
62 * @throws SmartyException if source cannot be loaded
64 abstract public function getContent(Smarty_Template_Source $source);
67 * populate Source Object with meta data from Resource
69 * @param Smarty_Template_Source $source source object
70 * @param Smarty_Internal_Template $_template template object
72 abstract public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null);
75 * populate Source Object with timestamp and exists from Resource
77 * @param Smarty_Template_Source $source source object
79 public function populateTimestamp(Smarty_Template_Source $source)
81 // intentionally left blank
85 * modify resource_name according to resource handlers specifications
87 * @param Smarty $smarty Smarty instance
88 * @param string $resource_name resource_name to make unique
89 * @param boolean $isConfig flag for config resource
91 * @return string unique resource name
93 public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false)
96 if (!isset($smarty->_joined_config_dir)) {
97 $smarty->getTemplateDir(null, true);
99 return get_class($this) . '#' . $smarty->_joined_config_dir . '#' . $resource_name;
101 if (!isset($smarty->_joined_template_dir)) {
102 $smarty->getTemplateDir();
104 return get_class($this) . '#' . $smarty->_joined_template_dir . '#' . $resource_name;
109 * Determine basename for compiled filename
111 * @param Smarty_Template_Source $source source object
113 * @return string resource's basename
115 public function getBasename(Smarty_Template_Source $source)
117 return basename(preg_replace('![^\w]+!', '_', $source->name));
121 * Load Resource Handler
123 * @param Smarty $smarty smarty object
124 * @param string $type name of the resource
126 * @throws SmartyException
127 * @return Smarty_Resource Resource Handler
129 public static function load(Smarty $smarty, $type)
131 // try smarty's cache
132 if (isset($smarty->_cache[ 'resource_handlers' ][ $type ])) {
133 return $smarty->_cache[ 'resource_handlers' ][ $type ];
136 // try registered resource
137 if (isset($smarty->registered_resources[ $type ])) {
138 return $smarty->_cache[ 'resource_handlers' ][ $type ] =
139 $smarty->registered_resources[ $type ] instanceof Smarty_Resource ?
140 $smarty->registered_resources[ $type ] : new Smarty_Internal_Resource_Registered();
143 // try sysplugins dir
144 if (isset(self::$sysplugins[ $type ])) {
145 $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
146 return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
150 $_resource_class = 'Smarty_Resource_' . ucfirst($type);
151 if ($smarty->loadPlugin($_resource_class)) {
152 if (class_exists($_resource_class, false)) {
153 return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
155 $smarty->registerResource($type,
156 array("smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp",
157 "smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted"));
158 // give it another try, now that the resource is registered properly
159 return self::load($smarty, $type);
164 $_known_stream = stream_get_wrappers();
165 if (in_array($type, $_known_stream)) {
167 if (is_object($smarty->security_policy)) {
168 $smarty->security_policy->isTrustedStream($type);
170 return $smarty->_cache[ 'resource_handlers' ][ $type ] = new Smarty_Internal_Resource_Stream();
173 // TODO: try default_(template|config)_handler
176 throw new SmartyException("Unknown resource type '{$type}'");
180 * extract resource_type and resource_name from template_resource and config_resource
181 * @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
183 * @param string $resource_name template_resource or config_resource to parse
184 * @param string $default_resource the default resource_type defined in $smarty
186 * @return array with parsed resource name and type
188 public static function parseResourceName($resource_name, $default_resource)
190 if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
192 $name = substr($resource_name, strlen($match[ 0 ]));
194 // no resource given, use default
195 // or single character before the colon is not a resource type, but part of the filepath
196 $type = $default_resource;
197 $name = $resource_name;
199 return array($name, $type);
203 * modify template_resource according to resource handlers specifications
205 * @param \Smarty_Internal_Template|\Smarty $obj Smarty instance
206 * @param string $template_resource template_resource to extract resource handler and name of
208 * @return string unique resource name
210 public static function getUniqueTemplateName($obj, $template_resource)
212 $smarty = $obj->_getSmartyObj();
213 list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type);
214 // TODO: optimize for Smarty's internal resource types
215 $resource = Smarty_Resource::load($smarty, $type);
216 // go relative to a given template?
217 $_file_is_dotted = $name[ 0 ] == '.' && ($name[ 1 ] == '.' || $name[ 1 ] == '/');
218 if ($obj->_isTplObj() && $_file_is_dotted &&
219 ($obj->source->type == 'file' || $obj->parent->source->type == 'extends')
221 $name = $smarty->_realpath(dirname($obj->parent->source->filepath) . $smarty->ds . $name);
223 return $resource->buildUniqueResourceName($smarty, $name);
227 * Check if resource must check time stamps when when loading complied or cached templates.
228 * Resources like 'extends' which use source components my disable timestamp checks on own resource.
232 public function checkTimestamps()
238 * initialize Source Object for given resource
239 * wrapper for backward compatibility to versions < 3.1.22
240 * Either [$_template] or [$smarty, $template_resource] must be specified
242 * @param Smarty_Internal_Template $_template template object
243 * @param Smarty $smarty smarty object
244 * @param string $template_resource resource identifier
246 * @return Smarty_Template_Source Source Object
248 public static function source(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
249 $template_resource = null)
251 return Smarty_Template_Source::load($_template, $smarty, $template_resource);