Хранилища Subversion ant

Редакция

Содержимое файла | Последнее изменение | Открыть журнал | RSS

Редакция Автор № строки Строка
304 alex-w 1
<?php
2
/**
3
* Smarty Internal Plugin Config
4
*
5
* Main class for config variables
6
*
7
* @ignore
8
* @package Smarty
9
* @subpackage Config
10
* @author Uwe Tews
11
*/
12
class Smarty_Internal_Config {
13
    static $config_objects = array();
14
 
15
    public function __construct($config_resource, $smarty)
16
    {
17
        $this->smarty = $smarty;
18
        $this->config_resource = $config_resource;
19
        $this->config_resource_type = null;
20
        $this->config_resource_name = null;
21
        $this->config_filepath = null;
22
        $this->config_timestamp = null;
23
        $this->config_source = null;
24
        $this->compiled_config = null;
25
        $this->compiled_filepath = null;
26
        $this->compiled_timestamp = null;
27
        $this->mustCompile = null;
28
        $this->compiler_object = null;
29
        // parse config resource name
30
        if (!$this->parseConfigResourceName ($config_resource)) {
31
            throw new Exception ("Unable to parse config resource '{$config_resource}'");
32
        }
33
    }
34
 
35
    public function getConfigFilepath ()
36
    {
37
        return $this->config_filepath === null ?
38
        $this->config_filepath = $this->buildConfigFilepath() :
39
        $this->config_filepath;
40
    }
41
 
42
    public function getTimestamp ()
43
    {
44
        return $this->config_timestamp === null ?
45
        $this->config_timestamp = filemtime($this->getConfigFilepath()) :
46
        $this->config_timestamp;
47
    }
48
 
49
    private function parseConfigResourceName($config_resource)
50
    {
51
        if (empty($config_resource))
52
            return false;
53
        if (strpos($config_resource, ':') === false) {
54
            // no resource given, use default
55
            $this->config_resource_type = $this->smarty->default_config_type;
56
            $this->config_resource_name = $config_resource;
57
        } else {
58
            // get type and name from path
59
            list($this->config_resource_type, $this->config_resource_name) = explode(':', $config_resource, 2);
60
            if (strlen($this->config_resource_type) == 1) {
61
                // 1 char is not resource type, but part of filepath
62
                $this->config_resource_type = $this->smarty->default_config_type;
63
                $this->config_resource_name = $config_resource;
64
            } else {
65
                $this->config_resource_type = strtolower($this->config_resource_type);
66
            }
67
        }
68
        return true;
69
    }
70
 
71
    /*
72
     * get system filepath to config
73
     */
74
    public function buildConfigFilepath ()
75
    {
76
        foreach((array)$this->smarty->config_dir as $_config_dir) {
77
            if (strpos('/\\', substr($_config_dir, -1)) === false) {
78
                $_config_dir .= DS;
79
            }
80
 
81
            $_filepath = $_config_dir . $this->config_resource_name;
82
            if (file_exists($_filepath))
83
                return $_filepath;
84
        }
85
        // no tpl file found
86
        throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
87
        return false;
88
    }
89
    /**
90
    * Read config file source
91
    *
92
    * @return string content of source file
93
    */
94
    /**
95
    * Returns the template source code
96
    *
97
    * The template source is being read by the actual resource handler
98
    *
99
    * @return string the template source
100
    */
101
    public function getConfigSource ()
102
    {
103
        if ($this->config_source === null) {
104
            if ($this->readConfigSource($this) === false) {
105
                throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
106
            }
107
        }
108
        return $this->config_source;
109
    }
110
    public function readConfigSource()
111
    {
112
        // read source file
113
        if (file_exists($this->getConfigFilepath())) {
114
            $this->config_source = file_get_contents($this->getConfigFilepath());
115
            return true;
116
        } else {
117
            return false;
118
        }
119
    }
120
 
121
    /**
122
    * Returns the compiled  filepath
123
    *
124
    * @return string the compiled filepath
125
    */
126
    public function getCompiledFilepath ()
127
    {
128
        return $this->compiled_filepath === null ?
129
        ($this->compiled_filepath = $this->buildCompiledFilepath()) :
130
        $this->compiled_filepath;
131
    }
132
    public function buildCompiledFilepath()
133
    {
134
        $_filepath = (string)abs(crc32($this->config_resource_name));
135
        // if use_sub_dirs, break file into directories
136
        if ($this->smarty->use_sub_dirs) {
137
            $_filepath = substr($_filepath, 0, 3) . DS
138
             . substr($_filepath, 0, 2) . DS
139
             . substr($_filepath, 0, 1) . DS
140
             . $_filepath;
141
        }
142
        $_compile_dir = $this->smarty->compile_dir;
143
        if (substr($_compile_dir, -1) != DS) {
144
            $_compile_dir .= DS;
145
        }
146
        return $_compile_dir . $_filepath . '.' . basename($this->config_resource_name) . '.config' . $this->smarty->php_ext;
147
    }
148
    /**
149
    * Returns the timpestamp of the compiled file
150
    *
151
    * @return integer the file timestamp
152
    */
153
    public function getCompiledTimestamp ()
154
    {
155
        return $this->compiled_timestamp === null ?
156
        ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false) :
157
        $this->compiled_timestamp;
158
    }
159
    /**
160
    * Returns if the current config file must be compiled
161
    *
162
    * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
163
    *
164
    * @return boolean true if the file must be compiled
165
    */
166
    public function mustCompile ()
167
    {
168
        return $this->mustCompile === null ?
169
        $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () !== $this->getTimestamp ()):
170
        $this->mustCompile;
171
    }
172
    /**
173
    * Returns the compiled config file
174
    *
175
    * It checks if the config file must be compiled or just read the compiled version
176
    *
177
    * @return string the compiled config file
178
    */
179
    public function getCompiledConfig ()
180
    {
181
        if ($this->compiled_config === null) {
182
            // see if template needs compiling.
183
            if ($this->mustCompile()) {
184
                $this->compileConfigSource();
185
            } else {
186
                $this->compiled_config = file_get_contents($this->getCompiledFilepath());
187
            }
188
        }
189
        return $this->compiled_config;
190
    }
191
 
192
    /**
193
    * Compiles the config files
194
    */
195
    public function compileConfigSource ()
196
    {
197
        // compile template
198
        if (!is_object($this->compiler_object)) {
199
            // load compiler
200
            $this->smarty->loadPlugin('Smarty_Internal_Config_File_Compiler');
201
            $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
202
        }
203
        if (!is_object($this->smarty->write_file_object)) {
204
            $this->smarty->loadPlugin("Smarty_Internal_Write_File");
205
            $this->smarty->write_file_object = new Smarty_Internal_Write_File;
206
        }
207
        // call compiler
208
        if ($this->compiler_object->compileSource($this)) {
209
            // compiling succeded
210
            // write compiled template
211
            $this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->getCompiledConfig());
212
            // make template and compiled file timestamp match
213
            touch($this->getCompiledFilepath(), $this->getTimestamp());
214
        } else {
215
            // error compiling template
216
            throw new Exception("Error compiling template {$this->getConfigFilepath ()}");
217
            return false;
218
        }
219
    }
220
 
221
    /*
222
     * load config variables
223
    *
224
    * @param mixed $sections array of section names, single section or null
225
    * @param object $scope global,parent or local
226
    */
227
    public function loadConfigVars ($sections = null, $scope)
228
    {
229
        $config_data = unserialize($this->getCompiledConfig());
230
        // var_dump($config_data);
231
        // copy global config vars
232
        foreach ($config_data['vars'] as $variable => $value) {
233
            $scope->config_vars[$variable] = $value;
234
        }
235
        // scan sections
236
        foreach ($config_data['sections'] as $this_section => $dummy) {
237
            if ($sections == null || in_array($this_section, (array)$sections)) {
238
                foreach ($config_data['sections'][$this_section]['vars'] as $variable => $value) {
239
                    $scope->config_vars[$variable] = $value;
240
                }
241
            }
242
        }
243
    }
244
}
245
 
246
?>