summaryrefslogtreecommitdiff
path: root/lib/smarty3/sysplugins/smarty_internal_extension_handler.php
blob: b07615526da26cfac78b2a9a56687bd33ff32dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

/**
 * Smarty Extension handler
 *
 * Load extensions dynamically
 *
 * @package    Smarty
 * @subpackage PluginsInternal
 * @author     Uwe Tews
 *
 * Runtime extensions
 * @property   Smarty_Internal_Runtime_CacheModify       $_cacheModify
 * @property   Smarty_Internal_Runtime_CacheResourceFile $_cacheResourceFile
 * @property   Smarty_Internal_Runtime_Capture           $_capture
 * @property   Smarty_Internal_Runtime_CodeFrame         $_codeFrame
 * @property   Smarty_Internal_Runtime_FilterHandler     $_filterHandler
 * @property   Smarty_Internal_Runtime_Foreach           $_foreach
 * @property   Smarty_Internal_Runtime_GetIncludePath    $_getIncludePath
 * @property   Smarty_Internal_Runtime_Make_Nocache      $_make_nocache
 * @property   Smarty_Internal_Runtime_UpdateCache       $_updateCache
 * @property   Smarty_Internal_Runtime_UpdateScope       $_updateScope
 * @property   Smarty_Internal_Runtime_TplFunction       $_tplFunction
 * @property   Smarty_Internal_Runtime_WriteFile         $_writeFile
 *
 * Method extensions
 * @property   Smarty_Internal_Method_GetTemplateVars    $getTemplateVars
 * @property   Smarty_Internal_Method_Append             $append
 * @property   Smarty_Internal_Method_AppendByRef        $appendByRef
 * @property   Smarty_Internal_Method_AssignGlobal       $assignGlobal
 * @property   Smarty_Internal_Method_AssignByRef        $assignByRef
 * @property   Smarty_Internal_Method_LoadFilter         $loadFilter
 * @property   Smarty_Internal_Method_LoadPlugin         $loadPlugin
 * @property   Smarty_Internal_Method_RegisterFilter     $registerFilter
 * @property   Smarty_Internal_Method_RegisterObject     $registerObject
 * @property   Smarty_Internal_Method_RegisterPlugin     $registerPlugin
 * @property   mixed|\Smarty_Template_Cached             configLoad
 */
class Smarty_Internal_Extension_Handler
{
    public $objType = null;

    /**
     * Cache for property information from generic getter/setter
     * Preloaded with names which should not use with generic getter/setter
     *
     * @var array
     */
    private $_property_info     = array(
        'AutoloadFilters' => 0, 'DefaultModifiers' => 0, 'ConfigVars' => 0,
        'DebugTemplate'   => 0, 'RegisteredObject' => 0, 'StreamVariable' => 0,
        'TemplateVars'    => 0, 'Literals' => 'Literals',
    );//

    private $resolvedProperties = array();

    /**
     * Call external Method
     *
     * @param \Smarty_Internal_Data $data
     * @param string                $name external method names
     * @param array                 $args argument array
     *
     * @return mixed
     */
    public function _callExternalMethod(Smarty_Internal_Data $data, $name, $args)
    {
        /* @var Smarty $data ->smarty */
        $smarty = isset($data->smarty) ? $data->smarty : $data;
        if (!isset($smarty->ext->$name)) {
            if (preg_match('/^((set|get)|(.*?))([A-Z].*)$/', $name, $match)) {
                $basename = $this->upperCase($match[ 4 ]);
                if (!isset($smarty->ext->$basename) && isset($this->_property_info[ $basename ])
                    && is_string($this->_property_info[ $basename ])
                ) {
                    $class = 'Smarty_Internal_Method_' . $this->_property_info[ $basename ];
                    if (class_exists($class)) {
                        $classObj = new $class();
                        $methodes = get_class_methods($classObj);
                        foreach ($methodes as $method) {
                            $smarty->ext->$method = $classObj;
                        }
                    }
                }
                if (!empty($match[ 2 ]) && !isset($smarty->ext->$name)) {
                    $class = 'Smarty_Internal_Method_' . $this->upperCase($name);
                    if (!class_exists($class)) {
                        $objType = $data->_objType;
                        $propertyType = false;
                        if (!isset($this->resolvedProperties[ $match[ 0 ] ][ $objType ])) {
                            $property = isset($this->resolvedProperties[ 'property' ][ $basename ]) ?
                                $this->resolvedProperties[ 'property' ][ $basename ] :
                                $property = $this->resolvedProperties[ 'property' ][ $basename ] = strtolower(
                                    join(
                                        '_',
                                        preg_split(
                                            '/([A-Z][^A-Z]*)/',
                                            $basename,
                                            -1,
                                            PREG_SPLIT_NO_EMPTY |
                                            PREG_SPLIT_DELIM_CAPTURE
                                        )
                                    )
                                );
                            if ($property !== false) {
                                if (property_exists($data, $property)) {
                                    $propertyType = $this->resolvedProperties[ $match[ 0 ] ][ $objType ] = 1;
                                } elseif (property_exists($smarty, $property)) {
                                    $propertyType = $this->resolvedProperties[ $match[ 0 ] ][ $objType ] = 2;
                                } else {
                                    $this->resolvedProperties[ 'property' ][ $basename ] = $property = false;
                                }
                            }
                        } else {
                            $propertyType = $this->resolvedProperties[ $match[ 0 ] ][ $objType ];
                            $property = $this->resolvedProperties[ 'property' ][ $basename ];
                        }
                        if ($propertyType) {
                            $obj = $propertyType === 1 ? $data : $smarty;
                            if ($match[ 2 ] === 'get') {
                                return $obj->$property;
                            } elseif ($match[ 2 ] === 'set') {
                                return $obj->$property = $args[ 0 ];
                            }
                        }
                    }
                }
            }
        }
        $callback = array($smarty->ext->$name, $name);
        array_unshift($args, $data);
        if (isset($callback) && $callback[ 0 ]->objMap | $data->_objType) {
            return call_user_func_array($callback, $args);
        }
        return call_user_func_array(array(new Smarty_Internal_Undefined(), $name), $args);
    }

    /**
     * Make first character of name parts upper case
     *
     * @param string $name
     *
     * @return string
     */
    public function upperCase($name)
    {
        $_name = explode('_', $name);
        $_name = array_map('ucfirst', $_name);
        return implode('_', $_name);
    }

    /**
     * get extension object
     *
     * @param string $property_name property name
     *
     * @return mixed|Smarty_Template_Cached
     */
    public function __get($property_name)
    {
        // object properties of runtime template extensions will start with '_'
        if ($property_name[ 0 ] === '_') {
            $class = 'Smarty_Internal_Runtime' . $this->upperCase($property_name);
        } else {
            $class = 'Smarty_Internal_Method_' . $this->upperCase($property_name);
        }
        if (!class_exists($class)) {
            return $this->$property_name = new Smarty_Internal_Undefined($class);
        }
        return $this->$property_name = new $class();
    }

    /**
     * set extension property
     *
     * @param string $property_name property name
     * @param mixed  $value         value
     *
     */
    public function __set($property_name, $value)
    {
        $this->$property_name = $value;
    }

    /**
     * Call error handler for undefined method
     *
     * @param string $name unknown method-name
     * @param array  $args argument array
     *
     * @return mixed
     */
    public function __call($name, $args)
    {
        return call_user_func_array(array(new Smarty_Internal_Undefined(), $name), array($this));
    }
}