Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
     * resource types provided by the core
25
     *
26
     * @var array
27
     */
28
    public static $sysplugins = array(
29
        'file'    => 'smarty_internal_resource_file.php',
30
        'string'  => 'smarty_internal_resource_string.php',
31
        'extends' => 'smarty_internal_resource_extends.php',
32
        'stream'  => 'smarty_internal_resource_stream.php',
33
        'eval'    => 'smarty_internal_resource_eval.php',
34
        'php'     => 'smarty_internal_resource_php.php'
35
    );
36
 
37
    /**
38
     * Source is bypassing compiler
39
     *
40
     * @var boolean
41
     */
42
    public $uncompiled = false;
43
 
44
    /**
45
     * Source must be recompiled on every occasion
46
     *
47
     * @var boolean
48
     */
49
    public $recompiled = false;
50
 
51
    /**
52
     * Flag if resource does implement populateCompiledFilepath() method
53
     *
54
     * @var bool
55
     */
56
    public $hasCompiledHandler = false;
57
 
58
    /**
59
     * Load Resource Handler
60
     *
61
     * @param Smarty $smarty smarty object
62
     * @param string $type   name of the resource
63
     *
64
     * @throws SmartyException
65
     * @return Smarty_Resource Resource Handler
66
     */
67
    public static function load(Smarty $smarty, $type)
68
    {
69
        // try smarty's cache
70
        if (isset($smarty->_cache[ 'resource_handlers' ][ $type ])) {
71
            return $smarty->_cache[ 'resource_handlers' ][ $type ];
72
        }
73
        // try registered resource
74
        if (isset($smarty->registered_resources[ $type ])) {
75
            return $smarty->_cache[ 'resource_handlers' ][ $type ] = $smarty->registered_resources[ $type ];
76
        }
77
        // try sysplugins dir
78
        if (isset(self::$sysplugins[ $type ])) {
79
            $_resource_class = 'Smarty_Internal_Resource_' . smarty_ucfirst_ascii($type);
80
            return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
81
        }
82
        // try plugins dir
83
        $_resource_class = 'Smarty_Resource_' . smarty_ucfirst_ascii($type);
84
        if ($smarty->loadPlugin($_resource_class)) {
85
            if (class_exists($_resource_class, false)) {
86
                return $smarty->_cache[ 'resource_handlers' ][ $type ] = new $_resource_class();
87
            } else {
88
                $smarty->registerResource(
89
                    $type,
90
                    array(
91
                        "smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp",
92
                        "smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted"
93
                    )
94
                );
95
                // give it another try, now that the resource is registered properly
96
                return self::load($smarty, $type);
97
            }
98
        }
99
        // try streams
100
        $_known_stream = stream_get_wrappers();
101
        if (in_array($type, $_known_stream)) {
102
            // is known stream
103
            if (is_object($smarty->security_policy)) {
104
                $smarty->security_policy->isTrustedStream($type);
105
            }
106
            return $smarty->_cache[ 'resource_handlers' ][ $type ] = new Smarty_Internal_Resource_Stream();
107
        }
108
        // TODO: try default_(template|config)_handler
109
        // give up
110
        throw new SmartyException("Unknown resource type '{$type}'");
111
    }
112
 
113
    /**
114
     * extract resource_type and resource_name from template_resource and config_resource
115
     *
116
     * @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
117
     *
118
     * @param string $resource_name    template_resource or config_resource to parse
119
     * @param string $default_resource the default resource_type defined in $smarty
120
     *
121
     * @return array with parsed resource name and type
122
     */
123
    public static function parseResourceName($resource_name, $default_resource)
124
    {
125
        if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
126
            $type = $match[ 1 ];
127
            $name = substr($resource_name, strlen($match[ 0 ]));
128
        } else {
129
            // no resource given, use default
130
            // or single character before the colon is not a resource type, but part of the filepath
131
            $type = $default_resource;
132
            $name = $resource_name;
133
        }
134
        return array($name, $type);
135
    }
136
 
137
    /**
138
     * modify template_resource according to resource handlers specifications
139
     *
140
     * @param \Smarty_Internal_Template|\Smarty $obj               Smarty instance
141
     * @param string                            $template_resource template_resource to extract resource handler and
142
     *                                                             name of
143
     *
144
     * @return string unique resource name
145
     * @throws \SmartyException
146
     */
147
    public static function getUniqueTemplateName($obj, $template_resource)
148
    {
149
        $smarty = $obj->_getSmartyObj();
150
        list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type);
151
        // TODO: optimize for Smarty's internal resource types
152
        $resource = Smarty_Resource::load($smarty, $type);
153
        // go relative to a given template?
154
        $_file_is_dotted = $name[ 0 ] === '.' && ($name[ 1 ] === '.' || $name[ 1 ] === '/');
155
        if ($obj->_isTplObj() && $_file_is_dotted
156
            && ($obj->source->type === 'file' || $obj->parent->source->type === 'extends')
157
        ) {
158
            $name = $smarty->_realpath(dirname($obj->parent->source->filepath) . DIRECTORY_SEPARATOR . $name);
159
        }
160
        return $resource->buildUniqueResourceName($smarty, $name);
161
    }
162
 
163
    /**
164
     * initialize Source Object for given resource
165
     * wrapper for backward compatibility to versions < 3.1.22
166
     * Either [$_template] or [$smarty, $template_resource] must be specified
167
     *
168
     * @param Smarty_Internal_Template $_template         template object
169
     * @param Smarty                   $smarty            smarty object
170
     * @param string                   $template_resource resource identifier
171
     *
172
     * @return \Smarty_Template_Source Source Object
173
     * @throws \SmartyException
174
     */
175
    public static function source(
176
        Smarty_Internal_Template $_template = null,
177
        Smarty $smarty = null,
178
        $template_resource = null
179
    ) {
180
        return Smarty_Template_Source::load($_template, $smarty, $template_resource);
181
    }
182
 
183
    /**
184
     * Load template's source into current template object
185
     *
186
     * @param Smarty_Template_Source $source source object
187
     *
188
     * @return string                 template source
189
     * @throws SmartyException        if source cannot be loaded
190
     */
191
    abstract public function getContent(Smarty_Template_Source $source);
192
 
193
    /**
194
     * populate Source Object with meta data from Resource
195
     *
196
     * @param Smarty_Template_Source   $source    source object
197
     * @param Smarty_Internal_Template $_template template object
198
     */
199
    abstract public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null);
200
 
201
    /**
202
     * populate Source Object with timestamp and exists from Resource
203
     *
204
     * @param Smarty_Template_Source $source source object
205
     */
206
    public function populateTimestamp(Smarty_Template_Source $source)
207
    {
208
        // intentionally left blank
209
    }
210
 
211
    /**
212
     * modify resource_name according to resource handlers specifications
213
     *
214
     * @param Smarty  $smarty        Smarty instance
215
     * @param string  $resource_name resource_name to make unique
216
     * @param boolean $isConfig      flag for config resource
217
     *
218
     * @return string unique resource name
219
     */
220
    public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false)
221
    {
222
        if ($isConfig) {
223
            if (!isset($smarty->_joined_config_dir)) {
224
                $smarty->getTemplateDir(null, true);
225
            }
226
            return get_class($this) . '#' . $smarty->_joined_config_dir . '#' . $resource_name;
227
        } else {
228
            if (!isset($smarty->_joined_template_dir)) {
229
                $smarty->getTemplateDir();
230
            }
231
            return get_class($this) . '#' . $smarty->_joined_template_dir . '#' . $resource_name;
232
        }
233
    }
234
 
235
    /*
236
     * Check if resource must check time stamps when when loading complied or cached templates.
237
     * Resources like 'extends' which use source components my disable timestamp checks on own resource.
238
     *
239
     * @return bool
240
     */
241
    /**
242
     * Determine basename for compiled filename
243
     *
244
     * @param Smarty_Template_Source $source source object
245
     *
246
     * @return string                 resource's basename
247
     */
248
    public function getBasename(Smarty_Template_Source $source)
249
    {
250
        return basename(preg_replace('![^\w]+!', '_', $source->name));
251
    }
252
 
253
    /**
254
     * @return bool
255
     */
256
    public function checkTimestamps()
257
    {
258
        return true;
259
    }
260
}