]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_resource.php
Merge pull request #4233 from MrPetovan/task/4116-move-smarty-to-composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_resource.php
1 <?php
2 /**
3  * Smarty Resource Plugin
4  *
5  * @package    Smarty
6  * @subpackage TemplateResources
7  * @author     Rodney Rehm
8  */
9
10 /**
11  * Smarty Resource Plugin
12  * Base implementation for resource plugins
13  *
14  * @package    Smarty
15  * @subpackage TemplateResources
16  *
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)
20  */
21 abstract class Smarty_Resource
22 {
23     /**
24      * Source is bypassing compiler
25      *
26      * @var boolean
27      */
28     public $uncompiled = false;
29
30     /**
31      * Source must be recompiled on every occasion
32      *
33      * @var boolean
34      */
35     public $recompiled = false;
36
37     /**
38      * resource types provided by the core
39      *
40      * @var array
41      */
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');
48
49     /**
50      * Flag if resource does implement populateCompiledFilepath() method
51      *
52      * @var bool
53      */
54     public $hasCompiledHandler = false;
55
56     /**
57      * Load template's source into current template object
58      *
59      * @param  Smarty_Template_Source $source source object
60      *
61      * @return string                 template source
62      * @throws SmartyException        if source cannot be loaded
63      */
64     abstract public function getContent(Smarty_Template_Source $source);
65
66     /**
67      * populate Source Object with meta data from Resource
68      *
69      * @param Smarty_Template_Source   $source    source object
70      * @param Smarty_Internal_Template $_template template object
71      */
72     abstract public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null);
73
74     /**
75      * populate Source Object with timestamp and exists from Resource
76      *
77      * @param Smarty_Template_Source $source source object
78      */
79     public function populateTimestamp(Smarty_Template_Source $source)
80     {
81         // intentionally left blank
82     }
83
84     /**
85      * modify resource_name according to resource handlers specifications
86      *
87      * @param  Smarty  $smarty        Smarty instance
88      * @param  string  $resource_name resource_name to make unique
89      * @param  boolean $isConfig      flag for config resource
90      *
91      * @return string unique resource name
92      */
93     public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false)
94     {
95         if ($isConfig) {
96             if (!isset($smarty->_joined_config_dir)) {
97                 $smarty->getTemplateDir(null, true);
98             }
99             return get_class($this) . '#' . $smarty->_joined_config_dir . '#' . $resource_name;
100         } else {
101             if (!isset($smarty->_joined_template_dir)) {
102                 $smarty->getTemplateDir();
103             }
104             return get_class($this) . '#' . $smarty->_joined_template_dir . '#' . $resource_name;
105         }
106     }
107
108     /**
109      * Determine basename for compiled filename
110      *
111      * @param  Smarty_Template_Source $source source object
112      *
113      * @return string                 resource's basename
114      */
115     public function getBasename(Smarty_Template_Source $source)
116     {
117         return basename(preg_replace('![^\w]+!', '_', $source->name));
118     }
119
120     /**
121      * Load Resource Handler
122      *
123      * @param  Smarty $smarty smarty object
124      * @param  string $type   name of the resource
125      *
126      * @throws SmartyException
127      * @return Smarty_Resource Resource Handler
128      */
129     public static function load(Smarty $smarty, $type)
130     {
131         // try smarty's cache
132         if (isset($smarty->_cache[ 'resource_handlers' ][ $type ])) {
133             return $smarty->_cache[ 'resource_handlers' ][ $type ];
134         }
135
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();
141         }
142
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();
147         }
148
149         // try plugins dir
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();
154             } else {
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);
160             }
161         }
162
163         // try streams
164         $_known_stream = stream_get_wrappers();
165         if (in_array($type, $_known_stream)) {
166             // is known stream
167             if (is_object($smarty->security_policy)) {
168                 $smarty->security_policy->isTrustedStream($type);
169             }
170             return $smarty->_cache[ 'resource_handlers' ][ $type ] = new Smarty_Internal_Resource_Stream();
171         }
172
173         // TODO: try default_(template|config)_handler
174
175         // give up
176         throw new SmartyException("Unknown resource type '{$type}'");
177     }
178
179     /**
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).
182      *
183      * @param  string $resource_name    template_resource or config_resource to parse
184      * @param  string $default_resource the default resource_type defined in $smarty
185      *
186      * @return array with parsed resource name and type
187      */
188     public static function parseResourceName($resource_name, $default_resource)
189     {
190         if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
191             $type = $match[ 1 ];
192             $name = substr($resource_name, strlen($match[ 0 ]));
193         } else {
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;
198         }
199         return array($name, $type);
200     }
201
202     /**
203      * modify template_resource according to resource handlers specifications
204      *
205      * @param  \Smarty_Internal_Template|\Smarty $obj               Smarty instance
206      * @param  string                            $template_resource template_resource to extract resource handler and name of
207      *
208      * @return string unique resource name
209      */
210     public static function getUniqueTemplateName($obj, $template_resource)
211     {
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')
220         ) {
221              $name = $smarty->_realpath(dirname($obj->parent->source->filepath) . $smarty->ds . $name);
222         }
223         return $resource->buildUniqueResourceName($smarty, $name);
224     }
225
226     /*
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.
229      *
230      * @return bool
231      */
232     public function checkTimestamps()
233     {
234         return true;
235     }
236
237     /**
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
241      *
242      * @param  Smarty_Internal_Template $_template         template object
243      * @param  Smarty                   $smarty            smarty object
244      * @param  string                   $template_resource resource identifier
245      *
246      * @return Smarty_Template_Source   Source Object
247      */
248     public static function source(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
249                                   $template_resource = null)
250     {
251         return Smarty_Template_Source::load($_template, $smarty, $template_resource);
252     }
253 }
254