@@ -24,241 +24,241 @@ |
||
24 | 24 | */ |
25 | 25 | class Data implements IDataProvider |
26 | 26 | { |
27 | - /** |
|
28 | - * Data array. |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $data = array(); |
|
27 | + /** |
|
28 | + * Data array. |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $data = array(); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Returns the data array. |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public function getData() |
|
40 | - { |
|
41 | - return $this->data; |
|
42 | - } |
|
34 | + /** |
|
35 | + * Returns the data array. |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public function getData() |
|
40 | + { |
|
41 | + return $this->data; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Clears a the entire data or only the given key. |
|
46 | - * |
|
47 | - * @param array|string $name clears only one value if you give a name, multiple values if |
|
48 | - * you give an array of names, or the entire data if left null |
|
49 | - */ |
|
50 | - public function clear($name = null) |
|
51 | - { |
|
52 | - if ($name === null) { |
|
53 | - $this->data = array(); |
|
54 | - } elseif (is_array($name)) { |
|
55 | - foreach ($name as $index) { |
|
56 | - unset($this->data[$index]); |
|
57 | - } |
|
58 | - } else { |
|
59 | - unset($this->data[$name]); |
|
60 | - } |
|
61 | - } |
|
44 | + /** |
|
45 | + * Clears a the entire data or only the given key. |
|
46 | + * |
|
47 | + * @param array|string $name clears only one value if you give a name, multiple values if |
|
48 | + * you give an array of names, or the entire data if left null |
|
49 | + */ |
|
50 | + public function clear($name = null) |
|
51 | + { |
|
52 | + if ($name === null) { |
|
53 | + $this->data = array(); |
|
54 | + } elseif (is_array($name)) { |
|
55 | + foreach ($name as $index) { |
|
56 | + unset($this->data[$index]); |
|
57 | + } |
|
58 | + } else { |
|
59 | + unset($this->data[$name]); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Overwrites the entire data with the given array. |
|
65 | - * |
|
66 | - * @param array $data the new data array to use |
|
67 | - */ |
|
68 | - public function setData(array $data) |
|
69 | - { |
|
70 | - $this->data = $data; |
|
71 | - } |
|
63 | + /** |
|
64 | + * Overwrites the entire data with the given array. |
|
65 | + * |
|
66 | + * @param array $data the new data array to use |
|
67 | + */ |
|
68 | + public function setData(array $data) |
|
69 | + { |
|
70 | + $this->data = $data; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * merges the given array(s) with the current data with array_merge. |
|
75 | - * |
|
76 | - * @param array $data the array to merge |
|
77 | - */ |
|
78 | - public function mergeData(array $data) |
|
79 | - { |
|
80 | - $args = func_get_args(); |
|
81 | - foreach ($args as $key => $v) { |
|
82 | - if (is_array($v)) { |
|
83 | - $this->data = array_merge($this->data, $v); |
|
84 | - } |
|
85 | - } |
|
86 | - } |
|
73 | + /** |
|
74 | + * merges the given array(s) with the current data with array_merge. |
|
75 | + * |
|
76 | + * @param array $data the array to merge |
|
77 | + */ |
|
78 | + public function mergeData(array $data) |
|
79 | + { |
|
80 | + $args = func_get_args(); |
|
81 | + foreach ($args as $key => $v) { |
|
82 | + if (is_array($v)) { |
|
83 | + $this->data = array_merge($this->data, $v); |
|
84 | + } |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Assigns a value or an array of values to the data object. |
|
90 | - * |
|
91 | - * @param array|string $name an associative array of multiple (index=>value) or a string |
|
92 | - * that is the index to use, i.e. a value assigned to "foo" will be |
|
93 | - * accessible in the template through {$foo} |
|
94 | - * @param mixed $val the value to assign, or null if $name was an array |
|
95 | - */ |
|
96 | - public function assign($name, $val = null) |
|
97 | - { |
|
98 | - if (is_array($name)) { |
|
99 | - reset($name); |
|
100 | - foreach ($name as $k => $v){ |
|
101 | - $this->data[$k] = $v; |
|
102 | - } |
|
103 | - } else { |
|
104 | - $this->data[$name] = $val; |
|
105 | - } |
|
106 | - } |
|
88 | + /** |
|
89 | + * Assigns a value or an array of values to the data object. |
|
90 | + * |
|
91 | + * @param array|string $name an associative array of multiple (index=>value) or a string |
|
92 | + * that is the index to use, i.e. a value assigned to "foo" will be |
|
93 | + * accessible in the template through {$foo} |
|
94 | + * @param mixed $val the value to assign, or null if $name was an array |
|
95 | + */ |
|
96 | + public function assign($name, $val = null) |
|
97 | + { |
|
98 | + if (is_array($name)) { |
|
99 | + reset($name); |
|
100 | + foreach ($name as $k => $v){ |
|
101 | + $this->data[$k] = $v; |
|
102 | + } |
|
103 | + } else { |
|
104 | + $this->data[$name] = $val; |
|
105 | + } |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Allows to assign variables using the object syntax. |
|
110 | - * |
|
111 | - * @param string $name the variable name |
|
112 | - * @param string $value the value to assign to it |
|
113 | - */ |
|
114 | - public function __set($name, $value) |
|
115 | - { |
|
116 | - $this->assign($name, $value); |
|
117 | - } |
|
108 | + /** |
|
109 | + * Allows to assign variables using the object syntax. |
|
110 | + * |
|
111 | + * @param string $name the variable name |
|
112 | + * @param string $value the value to assign to it |
|
113 | + */ |
|
114 | + public function __set($name, $value) |
|
115 | + { |
|
116 | + $this->assign($name, $value); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Assigns a value by reference to the data object. |
|
121 | - * |
|
122 | - * @param string $name the index to use, i.e. a value assigned to "foo" will be |
|
123 | - * accessible in the template through {$foo} |
|
124 | - * @param mixed $val the value to assign by reference |
|
125 | - */ |
|
126 | - public function assignByRef($name, &$val) |
|
127 | - { |
|
128 | - $this->data[$name] = &$val; |
|
129 | - } |
|
119 | + /** |
|
120 | + * Assigns a value by reference to the data object. |
|
121 | + * |
|
122 | + * @param string $name the index to use, i.e. a value assigned to "foo" will be |
|
123 | + * accessible in the template through {$foo} |
|
124 | + * @param mixed $val the value to assign by reference |
|
125 | + */ |
|
126 | + public function assignByRef($name, &$val) |
|
127 | + { |
|
128 | + $this->data[$name] = &$val; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Appends values or an array of values to the data object. |
|
133 | - * |
|
134 | - * @param array|string $name an associative array of multiple (index=>value) or a string |
|
135 | - * that is the index to use, i.e. a value assigned to "foo" will be |
|
136 | - * accessible in the template through {$foo} |
|
137 | - * @param mixed $val the value to assign, or null if $name was an array |
|
138 | - * @param bool $merge true to merge data or false to append, defaults to false |
|
139 | - */ |
|
140 | - public function append($name, $val = null, $merge = false) |
|
141 | - { |
|
142 | - if (is_array($name)) { |
|
143 | - foreach ($name as $key => $val) { |
|
144 | - if (isset($this->data[$key]) && !is_array($this->data[$key])) { |
|
145 | - settype($this->data[$key], 'array'); |
|
146 | - } |
|
131 | + /** |
|
132 | + * Appends values or an array of values to the data object. |
|
133 | + * |
|
134 | + * @param array|string $name an associative array of multiple (index=>value) or a string |
|
135 | + * that is the index to use, i.e. a value assigned to "foo" will be |
|
136 | + * accessible in the template through {$foo} |
|
137 | + * @param mixed $val the value to assign, or null if $name was an array |
|
138 | + * @param bool $merge true to merge data or false to append, defaults to false |
|
139 | + */ |
|
140 | + public function append($name, $val = null, $merge = false) |
|
141 | + { |
|
142 | + if (is_array($name)) { |
|
143 | + foreach ($name as $key => $val) { |
|
144 | + if (isset($this->data[$key]) && !is_array($this->data[$key])) { |
|
145 | + settype($this->data[$key], 'array'); |
|
146 | + } |
|
147 | 147 | |
148 | - if ($merge === true && is_array($val)) { |
|
149 | - $this->data[$key] = $val + $this->data[$key]; |
|
150 | - } else { |
|
151 | - $this->data[$key][] = $val; |
|
152 | - } |
|
153 | - } |
|
154 | - } elseif ($val !== null) { |
|
155 | - if (isset($this->data[$name]) && !is_array($this->data[$name])) { |
|
156 | - settype($this->data[$name], 'array'); |
|
157 | - } elseif (!isset($this->data[$name])) { |
|
158 | - $this->data[$name] = array(); |
|
159 | - } |
|
148 | + if ($merge === true && is_array($val)) { |
|
149 | + $this->data[$key] = $val + $this->data[$key]; |
|
150 | + } else { |
|
151 | + $this->data[$key][] = $val; |
|
152 | + } |
|
153 | + } |
|
154 | + } elseif ($val !== null) { |
|
155 | + if (isset($this->data[$name]) && !is_array($this->data[$name])) { |
|
156 | + settype($this->data[$name], 'array'); |
|
157 | + } elseif (!isset($this->data[$name])) { |
|
158 | + $this->data[$name] = array(); |
|
159 | + } |
|
160 | 160 | |
161 | - if ($merge === true && is_array($val)) { |
|
162 | - $this->data[$name] = $val + $this->data[$name]; |
|
163 | - } else { |
|
164 | - $this->data[$name][] = $val; |
|
165 | - } |
|
166 | - } |
|
167 | - } |
|
161 | + if ($merge === true && is_array($val)) { |
|
162 | + $this->data[$name] = $val + $this->data[$name]; |
|
163 | + } else { |
|
164 | + $this->data[$name][] = $val; |
|
165 | + } |
|
166 | + } |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Appends a value by reference to the data object. |
|
171 | - * |
|
172 | - * @param string $name the index to use, i.e. a value assigned to "foo" will be |
|
173 | - * accessible in the template through {$foo} |
|
174 | - * @param mixed $val the value to append by reference |
|
175 | - * @param bool $merge true to merge data or false to append, defaults to false |
|
176 | - */ |
|
177 | - public function appendByRef($name, &$val, $merge = false) |
|
178 | - { |
|
179 | - if (isset($this->data[$name]) && !is_array($this->data[$name])) { |
|
180 | - settype($this->data[$name], 'array'); |
|
181 | - } |
|
169 | + /** |
|
170 | + * Appends a value by reference to the data object. |
|
171 | + * |
|
172 | + * @param string $name the index to use, i.e. a value assigned to "foo" will be |
|
173 | + * accessible in the template through {$foo} |
|
174 | + * @param mixed $val the value to append by reference |
|
175 | + * @param bool $merge true to merge data or false to append, defaults to false |
|
176 | + */ |
|
177 | + public function appendByRef($name, &$val, $merge = false) |
|
178 | + { |
|
179 | + if (isset($this->data[$name]) && !is_array($this->data[$name])) { |
|
180 | + settype($this->data[$name], 'array'); |
|
181 | + } |
|
182 | 182 | |
183 | - if ($merge === true && is_array($val)) { |
|
184 | - foreach ($val as $key => &$value) { |
|
185 | - $this->data[$name][$key] = &$value; |
|
186 | - } |
|
187 | - } else { |
|
188 | - $this->data[$name][] = &$val; |
|
189 | - } |
|
190 | - } |
|
183 | + if ($merge === true && is_array($val)) { |
|
184 | + foreach ($val as $key => &$value) { |
|
185 | + $this->data[$name][$key] = &$value; |
|
186 | + } |
|
187 | + } else { |
|
188 | + $this->data[$name][] = &$val; |
|
189 | + } |
|
190 | + } |
|
191 | 191 | |
192 | - /** |
|
193 | - * Returns true if the variable has been assigned already, false otherwise. |
|
194 | - * |
|
195 | - * @param string $name the variable name |
|
196 | - * |
|
197 | - * @return bool |
|
198 | - */ |
|
199 | - public function isAssigned($name) |
|
200 | - { |
|
201 | - return isset($this->data[$name]); |
|
202 | - } |
|
192 | + /** |
|
193 | + * Returns true if the variable has been assigned already, false otherwise. |
|
194 | + * |
|
195 | + * @param string $name the variable name |
|
196 | + * |
|
197 | + * @return bool |
|
198 | + */ |
|
199 | + public function isAssigned($name) |
|
200 | + { |
|
201 | + return isset($this->data[$name]); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * Supports calls to isset($dwoo->var). |
|
206 | - * |
|
207 | - * @param string $name the variable name |
|
208 | - * |
|
209 | - * @return bool |
|
210 | - */ |
|
211 | - public function __isset($name) |
|
212 | - { |
|
213 | - return isset($this->data[$name]); |
|
214 | - } |
|
204 | + /** |
|
205 | + * Supports calls to isset($dwoo->var). |
|
206 | + * |
|
207 | + * @param string $name the variable name |
|
208 | + * |
|
209 | + * @return bool |
|
210 | + */ |
|
211 | + public function __isset($name) |
|
212 | + { |
|
213 | + return isset($this->data[$name]); |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * Unassigns/removes a variable. |
|
218 | - * |
|
219 | - * @param string $name the variable name |
|
220 | - */ |
|
221 | - public function unassign($name) |
|
222 | - { |
|
223 | - unset($this->data[$name]); |
|
224 | - } |
|
216 | + /** |
|
217 | + * Unassigns/removes a variable. |
|
218 | + * |
|
219 | + * @param string $name the variable name |
|
220 | + */ |
|
221 | + public function unassign($name) |
|
222 | + { |
|
223 | + unset($this->data[$name]); |
|
224 | + } |
|
225 | 225 | |
226 | - /** |
|
227 | - * Supports unsetting variables using the object syntax. |
|
228 | - * |
|
229 | - * @param string $name the variable name |
|
230 | - */ |
|
231 | - public function __unset($name) |
|
232 | - { |
|
233 | - unset($this->data[$name]); |
|
234 | - } |
|
226 | + /** |
|
227 | + * Supports unsetting variables using the object syntax. |
|
228 | + * |
|
229 | + * @param string $name the variable name |
|
230 | + */ |
|
231 | + public function __unset($name) |
|
232 | + { |
|
233 | + unset($this->data[$name]); |
|
234 | + } |
|
235 | 235 | |
236 | - /** |
|
237 | - * Returns a variable if it was assigned. |
|
238 | - * |
|
239 | - * @param string $name the variable name |
|
240 | - * |
|
241 | - * @return mixed |
|
242 | - */ |
|
243 | - public function get($name) |
|
244 | - { |
|
245 | - return $this->__get($name); |
|
246 | - } |
|
236 | + /** |
|
237 | + * Returns a variable if it was assigned. |
|
238 | + * |
|
239 | + * @param string $name the variable name |
|
240 | + * |
|
241 | + * @return mixed |
|
242 | + */ |
|
243 | + public function get($name) |
|
244 | + { |
|
245 | + return $this->__get($name); |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * Allows to read variables using the object syntax. |
|
250 | - * |
|
251 | - * @param string $name the variable name |
|
252 | - * |
|
253 | - * @return mixed |
|
254 | - * @throws Exception |
|
255 | - */ |
|
256 | - public function __get($name) |
|
257 | - { |
|
258 | - if (isset($this->data[$name])) { |
|
259 | - return $this->data[$name]; |
|
260 | - } else { |
|
261 | - throw new Exception('Tried to read a value that was not assigned yet : "' . $name . '"'); |
|
262 | - } |
|
263 | - } |
|
248 | + /** |
|
249 | + * Allows to read variables using the object syntax. |
|
250 | + * |
|
251 | + * @param string $name the variable name |
|
252 | + * |
|
253 | + * @return mixed |
|
254 | + * @throws Exception |
|
255 | + */ |
|
256 | + public function __get($name) |
|
257 | + { |
|
258 | + if (isset($this->data[$name])) { |
|
259 | + return $this->data[$name]; |
|
260 | + } else { |
|
261 | + throw new Exception('Tried to read a value that was not assigned yet : "' . $name . '"'); |
|
262 | + } |
|
263 | + } |
|
264 | 264 | } |
@@ -23,139 +23,139 @@ |
||
23 | 23 | */ |
24 | 24 | interface ITemplate |
25 | 25 | { |
26 | - /** |
|
27 | - * Returns the cache duration for this template. |
|
28 | - * defaults to null if it was not provided |
|
29 | - * |
|
30 | - * @return int|null |
|
31 | - */ |
|
32 | - public function getCacheTime(); |
|
26 | + /** |
|
27 | + * Returns the cache duration for this template. |
|
28 | + * defaults to null if it was not provided |
|
29 | + * |
|
30 | + * @return int|null |
|
31 | + */ |
|
32 | + public function getCacheTime(); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Sets the cache duration for this template. |
|
36 | - * can be used to set it after the object is created if you did not provide |
|
37 | - * it in the constructor |
|
38 | - * |
|
39 | - * @param int $seconds duration of the cache validity for this template, if |
|
40 | - * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
41 | - * -1 = infinite cache |
|
42 | - */ |
|
43 | - public function setCacheTime($seconds = null); |
|
34 | + /** |
|
35 | + * Sets the cache duration for this template. |
|
36 | + * can be used to set it after the object is created if you did not provide |
|
37 | + * it in the constructor |
|
38 | + * |
|
39 | + * @param int $seconds duration of the cache validity for this template, if |
|
40 | + * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
41 | + * -1 = infinite cache |
|
42 | + */ |
|
43 | + public function setCacheTime($seconds = null); |
|
44 | 44 | |
45 | - /** |
|
46 | - * Returns the cached template output file name, true if it's cache-able but not cached |
|
47 | - * or false if it's not cached. |
|
48 | - * |
|
49 | - * @param Core $core the dwoo instance that requests it |
|
50 | - * |
|
51 | - * @return string|bool |
|
52 | - */ |
|
53 | - public function getCachedTemplate(Core $core); |
|
45 | + /** |
|
46 | + * Returns the cached template output file name, true if it's cache-able but not cached |
|
47 | + * or false if it's not cached. |
|
48 | + * |
|
49 | + * @param Core $core the dwoo instance that requests it |
|
50 | + * |
|
51 | + * @return string|bool |
|
52 | + */ |
|
53 | + public function getCachedTemplate(Core $core); |
|
54 | 54 | |
55 | - /** |
|
56 | - * Caches the provided output into the cache file. |
|
57 | - * |
|
58 | - * @param Core $core the dwoo instance that requests it |
|
59 | - * @param string $output the template output |
|
60 | - * |
|
61 | - * @return mixed full path of the cached file or false upon failure |
|
62 | - */ |
|
63 | - public function cache(Core $core, $output); |
|
55 | + /** |
|
56 | + * Caches the provided output into the cache file. |
|
57 | + * |
|
58 | + * @param Core $core the dwoo instance that requests it |
|
59 | + * @param string $output the template output |
|
60 | + * |
|
61 | + * @return mixed full path of the cached file or false upon failure |
|
62 | + */ |
|
63 | + public function cache(Core $core, $output); |
|
64 | 64 | |
65 | - /** |
|
66 | - * Clears the cached template if it's older than the given time. |
|
67 | - * |
|
68 | - * @param Core $core the dwoo instance that was used to cache that template |
|
69 | - * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
70 | - * |
|
71 | - * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
72 | - */ |
|
73 | - public function clearCache(Core $core, $olderThan = - 1); |
|
65 | + /** |
|
66 | + * Clears the cached template if it's older than the given time. |
|
67 | + * |
|
68 | + * @param Core $core the dwoo instance that was used to cache that template |
|
69 | + * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
70 | + * |
|
71 | + * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
72 | + */ |
|
73 | + public function clearCache(Core $core, $olderThan = - 1); |
|
74 | 74 | |
75 | - /** |
|
76 | - * Returns the compiled template file name. |
|
77 | - * |
|
78 | - * @param Core $core the dwoo instance that requests it |
|
79 | - * @param ICompiler $compiler the compiler that must be used |
|
80 | - * |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public function getCompiledTemplate(Core $core, ICompiler $compiler = null); |
|
75 | + /** |
|
76 | + * Returns the compiled template file name. |
|
77 | + * |
|
78 | + * @param Core $core the dwoo instance that requests it |
|
79 | + * @param ICompiler $compiler the compiler that must be used |
|
80 | + * |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public function getCompiledTemplate(Core $core, ICompiler $compiler = null); |
|
84 | 84 | |
85 | - /** |
|
86 | - * Returns the template name. |
|
87 | - * |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function getName(); |
|
85 | + /** |
|
86 | + * Returns the template name. |
|
87 | + * |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function getName(); |
|
91 | 91 | |
92 | - /** |
|
93 | - * Returns the resource name for this template class. |
|
94 | - * |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - public function getResourceName(); |
|
92 | + /** |
|
93 | + * Returns the resource name for this template class. |
|
94 | + * |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + public function getResourceName(); |
|
98 | 98 | |
99 | - /** |
|
100 | - * Returns the resource identifier for this template or false if it has no identifier. |
|
101 | - * |
|
102 | - * @return string|false |
|
103 | - */ |
|
104 | - public function getResourceIdentifier(); |
|
99 | + /** |
|
100 | + * Returns the resource identifier for this template or false if it has no identifier. |
|
101 | + * |
|
102 | + * @return string|false |
|
103 | + */ |
|
104 | + public function getResourceIdentifier(); |
|
105 | 105 | |
106 | - /** |
|
107 | - * Returns the template source of this template. |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function getSource(); |
|
106 | + /** |
|
107 | + * Returns the template source of this template. |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function getSource(); |
|
112 | 112 | |
113 | - /** |
|
114 | - * Returns an unique string identifying the current version of this template, |
|
115 | - * for example a timestamp of the last modified date or a hash of the template source. |
|
116 | - * |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public function getUid(); |
|
113 | + /** |
|
114 | + * Returns an unique string identifying the current version of this template, |
|
115 | + * for example a timestamp of the last modified date or a hash of the template source. |
|
116 | + * |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public function getUid(); |
|
120 | 120 | |
121 | - /** |
|
122 | - * Returns the compiler used by this template, if it was just compiled, or null. |
|
123 | - * |
|
124 | - * @return ICompiler |
|
125 | - */ |
|
126 | - public function getCompiler(); |
|
121 | + /** |
|
122 | + * Returns the compiler used by this template, if it was just compiled, or null. |
|
123 | + * |
|
124 | + * @return ICompiler |
|
125 | + */ |
|
126 | + public function getCompiler(); |
|
127 | 127 | |
128 | - /** |
|
129 | - * Returns some php code that will check if this template has been modified or not. |
|
130 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
131 | - * |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function getIsModifiedCode(); |
|
128 | + /** |
|
129 | + * Returns some php code that will check if this template has been modified or not. |
|
130 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
131 | + * |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function getIsModifiedCode(); |
|
135 | 135 | |
136 | - /** |
|
137 | - * Returns a new template object from the given resource identifier, null if no include is |
|
138 | - * possible (resource not found), or false if include is not permitted by this resource type. |
|
139 | - * this method should also check if $dwoo->getSecurityPolicy() is null or not and do the |
|
140 | - * necessary permission checks if required, if the security policy prevents the template |
|
141 | - * generation it should throw a new Security\Exception with a relevant message |
|
142 | - * |
|
143 | - * @param Core $core |
|
144 | - * @param mixed $resourceId the resource identifier |
|
145 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
146 | - * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
147 | - * instance that will render this template |
|
148 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
149 | - * template's content unique, if null it defaults to the current url makes this |
|
150 | - * template's content unique, if null it defaults to the current url |
|
151 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
152 | - * others, if null it defaults to the filename+bits of the path template from |
|
153 | - * others, if null it defaults to the filename+bits of the path |
|
154 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
155 | - * extends or any other plugin) an include, extends or any other plugin) |
|
156 | - * |
|
157 | - * @return ITemplate|false|null |
|
158 | - */ |
|
159 | - public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
160 | - $compileId = null, ITemplate $parentTemplate = null); |
|
136 | + /** |
|
137 | + * Returns a new template object from the given resource identifier, null if no include is |
|
138 | + * possible (resource not found), or false if include is not permitted by this resource type. |
|
139 | + * this method should also check if $dwoo->getSecurityPolicy() is null or not and do the |
|
140 | + * necessary permission checks if required, if the security policy prevents the template |
|
141 | + * generation it should throw a new Security\Exception with a relevant message |
|
142 | + * |
|
143 | + * @param Core $core |
|
144 | + * @param mixed $resourceId the resource identifier |
|
145 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
146 | + * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
147 | + * instance that will render this template |
|
148 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
149 | + * template's content unique, if null it defaults to the current url makes this |
|
150 | + * template's content unique, if null it defaults to the current url |
|
151 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
152 | + * others, if null it defaults to the filename+bits of the path template from |
|
153 | + * others, if null it defaults to the filename+bits of the path |
|
154 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
155 | + * extends or any other plugin) an include, extends or any other plugin) |
|
156 | + * |
|
157 | + * @return ITemplate|false|null |
|
158 | + */ |
|
159 | + public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
160 | + $compileId = null, ITemplate $parentTemplate = null); |
|
161 | 161 | } |
@@ -29,28 +29,28 @@ |
||
29 | 29 | */ |
30 | 30 | class PluginCapitalize extends Plugin |
31 | 31 | { |
32 | - /** |
|
33 | - * @param string $value |
|
34 | - * @param bool $numwords |
|
35 | - * |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function process($value, $numwords = false) |
|
39 | - { |
|
40 | - if ($numwords || preg_match('#^[^0-9]+$#', $value)) { |
|
41 | - return mb_convert_case((string)$value, MB_CASE_TITLE, $this->core->getCharset()); |
|
42 | - } else { |
|
43 | - $bits = explode(' ', (string)$value); |
|
44 | - $out = ''; |
|
45 | - foreach ($bits as $k => $v){ |
|
46 | - if (preg_match('#^[^0-9]+$#', $v)) { |
|
47 | - $out .= ' ' . mb_convert_case($v, MB_CASE_TITLE, $this->core->getCharset()); |
|
48 | - } else { |
|
49 | - $out .= ' ' . $v; |
|
50 | - } |
|
51 | - } |
|
32 | + /** |
|
33 | + * @param string $value |
|
34 | + * @param bool $numwords |
|
35 | + * |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function process($value, $numwords = false) |
|
39 | + { |
|
40 | + if ($numwords || preg_match('#^[^0-9]+$#', $value)) { |
|
41 | + return mb_convert_case((string)$value, MB_CASE_TITLE, $this->core->getCharset()); |
|
42 | + } else { |
|
43 | + $bits = explode(' ', (string)$value); |
|
44 | + $out = ''; |
|
45 | + foreach ($bits as $k => $v){ |
|
46 | + if (preg_match('#^[^0-9]+$#', $v)) { |
|
47 | + $out .= ' ' . mb_convert_case($v, MB_CASE_TITLE, $this->core->getCharset()); |
|
48 | + } else { |
|
49 | + $out .= ' ' . $v; |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - return substr($out, 1); |
|
54 | - } |
|
55 | - } |
|
53 | + return substr($out, 1); |
|
54 | + } |
|
55 | + } |
|
56 | 56 | } |
@@ -29,507 +29,507 @@ |
||
29 | 29 | */ |
30 | 30 | class Str implements ITemplate |
31 | 31 | { |
32 | - /** |
|
33 | - * Template name. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $name; |
|
38 | - |
|
39 | - /** |
|
40 | - * Template compilation id. |
|
41 | - * |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - protected $compileId; |
|
45 | - |
|
46 | - /** |
|
47 | - * Template cache id, if not provided in the constructor, it is set to |
|
48 | - * the md4 hash of the request_uri. it is however highly recommended to |
|
49 | - * provide one that will fit your needs. |
|
50 | - * in all cases, the compilation id is prepended to the cache id to separate |
|
51 | - * templates with similar cache ids from one another |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - protected $cacheId; |
|
56 | - |
|
57 | - /** |
|
58 | - * Validity duration of the generated cache file (in seconds). |
|
59 | - * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
60 | - * |
|
61 | - * @var int |
|
62 | - */ |
|
63 | - protected $cacheTime; |
|
64 | - |
|
65 | - /** |
|
66 | - * Boolean flag that defines whether the compilation should be enforced (once) or |
|
67 | - * not use this if you have issues with the compiled templates not being updated |
|
68 | - * but if you do need this it's most likely that you should file a bug report. |
|
69 | - * |
|
70 | - * @var bool |
|
71 | - */ |
|
72 | - protected $compilationEnforced; |
|
73 | - |
|
74 | - /** |
|
75 | - * Caches the results of the file checks to save some time when the same |
|
76 | - * templates is rendered several times. |
|
77 | - * |
|
78 | - * @var array |
|
79 | - */ |
|
80 | - protected static $cache = array( |
|
81 | - 'cached' => array(), |
|
82 | - 'compiled' => array() |
|
83 | - ); |
|
84 | - |
|
85 | - /** |
|
86 | - * Holds the compiler that built this template. |
|
87 | - * |
|
88 | - * @var ICompiler |
|
89 | - */ |
|
90 | - protected $compiler; |
|
91 | - |
|
92 | - /** |
|
93 | - * Chmod value for all files written (cached or compiled ones). |
|
94 | - * set to null if you don't want any chmod operation to happen |
|
95 | - * |
|
96 | - * @var int |
|
97 | - */ |
|
98 | - protected $chmod = 0777; |
|
99 | - |
|
100 | - /** |
|
101 | - * Containing template string. |
|
102 | - * |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - protected $template; |
|
106 | - |
|
107 | - /** |
|
108 | - * Creates a template from a string. |
|
109 | - * |
|
110 | - * @param string $templateString the template to use |
|
111 | - * @param int $cacheTime duration of the cache validity for this template, |
|
112 | - * if null it defaults to the Dwoo instance that will |
|
113 | - * render this template, set to -1 for infinite cache or 0 to disable |
|
114 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
115 | - * makes this template's content unique, if null it defaults |
|
116 | - * to the current url |
|
117 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
118 | - * template from others, if null it defaults to the md4 hash of the template |
|
119 | - */ |
|
120 | - public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
121 | - { |
|
122 | - $this->template = $templateString; |
|
123 | - $this->name = hash('md4', $templateString); |
|
124 | - $this->cacheTime = $cacheTime; |
|
125 | - |
|
126 | - if ($compileId !== null) { |
|
127 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
128 | - } else { |
|
129 | - $this->compileId = $templateString; |
|
130 | - } |
|
131 | - |
|
132 | - if ($cacheId !== null) { |
|
133 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Returns the cache duration for this template. |
|
139 | - * defaults to null if it was not provided |
|
140 | - * |
|
141 | - * @return int|null |
|
142 | - */ |
|
143 | - public function getCacheTime() |
|
144 | - { |
|
145 | - return $this->cacheTime; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Sets the cache duration for this template. |
|
150 | - * can be used to set it after the object is created if you did not provide |
|
151 | - * it in the constructor |
|
152 | - * |
|
153 | - * @param int $seconds duration of the cache validity for this template, if |
|
154 | - * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
155 | - * -1 = infinite cache |
|
156 | - */ |
|
157 | - public function setCacheTime($seconds = null) |
|
158 | - { |
|
159 | - $this->cacheTime = $seconds; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Returns the chmod value for all files written (cached or compiled ones). |
|
164 | - * defaults to 0777 |
|
165 | - * |
|
166 | - * @return int|null |
|
167 | - */ |
|
168 | - public function getChmod() |
|
169 | - { |
|
170 | - return $this->chmod; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Set the chmod value for all files written (cached or compiled ones). |
|
175 | - * set to null if you don't want to do any chmod() operation |
|
176 | - * |
|
177 | - * @param int $mask new bitmask to use for all files |
|
178 | - */ |
|
179 | - public function setChmod($mask = null) |
|
180 | - { |
|
181 | - $this->chmod = $mask; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Returns the template name. |
|
186 | - * |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public function getName() |
|
190 | - { |
|
191 | - return $this->name; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Returns the resource name for this template class. |
|
196 | - * |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function getResourceName() |
|
200 | - { |
|
201 | - return 'string'; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
206 | - * |
|
207 | - * @return false |
|
208 | - */ |
|
209 | - public function getResourceIdentifier() |
|
210 | - { |
|
211 | - return false; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Returns the template source of this template. |
|
216 | - * |
|
217 | - * @return string |
|
218 | - */ |
|
219 | - public function getSource() |
|
220 | - { |
|
221 | - return $this->template; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Returns an unique value identifying the current version of this template, |
|
226 | - * in this case it's the md4 hash of the content. |
|
227 | - * |
|
228 | - * @return string |
|
229 | - */ |
|
230 | - public function getUid() |
|
231 | - { |
|
232 | - return $this->name; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Returns the compiler used by this template, if it was just compiled, or null. |
|
237 | - * |
|
238 | - * @return ICompiler |
|
239 | - */ |
|
240 | - public function getCompiler() |
|
241 | - { |
|
242 | - return $this->compiler; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Marks this template as compile-forced, which means it will be recompiled even if it |
|
247 | - * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
248 | - * it's only meant to be used in development (and the development of dwoo particularly). |
|
249 | - */ |
|
250 | - public function forceCompilation() |
|
251 | - { |
|
252 | - $this->compilationEnforced = true; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Returns the cached template output file name, true if it's cache-able but not cached |
|
257 | - * or false if it's not cached. |
|
258 | - * |
|
259 | - * @param Core $core the dwoo instance that requests it |
|
260 | - * |
|
261 | - * @return string|bool |
|
262 | - */ |
|
263 | - public function getCachedTemplate(Core $core) |
|
264 | - { |
|
265 | - $cacheLength = $core->getCacheTime(); |
|
266 | - if ($this->cacheTime !== null) { |
|
267 | - $cacheLength = $this->cacheTime; |
|
268 | - } |
|
269 | - |
|
270 | - // file is not cacheable |
|
271 | - if ($cacheLength == 0) { |
|
272 | - return false; |
|
273 | - } |
|
274 | - |
|
275 | - $cachedFile = $this->getCacheFilename($core); |
|
276 | - |
|
277 | - if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
278 | - // already checked, return cache file |
|
279 | - return $cachedFile; |
|
280 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
281 | - // cache is still valid and can be loaded |
|
282 | - self::$cache['cached'][$this->cacheId] = true; |
|
283 | - |
|
284 | - return $cachedFile; |
|
285 | - } |
|
286 | - |
|
287 | - // file is cacheable |
|
288 | - return true; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Caches the provided output into the cache file. |
|
293 | - * |
|
294 | - * @param Core $core the dwoo instance that requests it |
|
295 | - * @param string $output the template output |
|
296 | - * |
|
297 | - * @return mixed full path of the cached file or false upon failure |
|
298 | - */ |
|
299 | - public function cache(Core $core, $output) |
|
300 | - { |
|
301 | - $cacheDir = $core->getCacheDir(); |
|
302 | - $cachedFile = $this->getCacheFilename($core); |
|
303 | - |
|
304 | - // the code below is courtesy of Rasmus Schultz, |
|
305 | - // thanks for his help on avoiding concurency issues |
|
306 | - $temp = tempnam($cacheDir, 'temp'); |
|
307 | - if (!($file = @fopen($temp, 'wb'))) { |
|
308 | - $temp = $cacheDir . uniqid('temp'); |
|
309 | - if (!($file = @fopen($temp, 'wb'))) { |
|
310 | - trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
311 | - |
|
312 | - return false; |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - fwrite($file, $output); |
|
317 | - fclose($file); |
|
318 | - |
|
319 | - $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
320 | - if (!@rename($temp, $cachedFile)) { |
|
321 | - @unlink($cachedFile); |
|
322 | - @rename($temp, $cachedFile); |
|
323 | - } |
|
324 | - |
|
325 | - if ($this->chmod !== null) { |
|
326 | - chmod($cachedFile, $this->chmod); |
|
327 | - } |
|
328 | - |
|
329 | - self::$cache['cached'][$this->cacheId] = true; |
|
330 | - |
|
331 | - return $cachedFile; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Clears the cached template if it's older than the given time. |
|
336 | - * |
|
337 | - * @param Core $core the dwoo instance that was used to cache that template |
|
338 | - * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
339 | - * |
|
340 | - * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
341 | - */ |
|
342 | - public function clearCache(Core $core, $olderThan = - 1) |
|
343 | - { |
|
344 | - $cachedFile = $this->getCacheFilename($core); |
|
345 | - |
|
346 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Returns the compiled template file name. |
|
351 | - * |
|
352 | - * @param Core $core the dwoo instance that requests it |
|
353 | - * @param ICompiler $compiler the compiler that must be used |
|
354 | - * |
|
355 | - * @return string |
|
356 | - */ |
|
357 | - public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
358 | - { |
|
359 | - $compiledFile = $this->getCompiledFilename($core); |
|
360 | - |
|
361 | - if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
362 | - // already checked, return compiled file |
|
363 | - } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
364 | - // template is compiled |
|
365 | - self::$cache['compiled'][$this->compileId] = true; |
|
366 | - } else { |
|
367 | - // compiles the template |
|
368 | - $this->compilationEnforced = false; |
|
369 | - |
|
370 | - if ($compiler === null) { |
|
371 | - $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
372 | - |
|
373 | - if ($compiler === null || $compiler === array('Dwoo\Compiler', 'compilerFactory')) { |
|
374 | - $compiler = Compiler::compilerFactory(); |
|
375 | - } else { |
|
376 | - $compiler = call_user_func($compiler); |
|
377 | - } |
|
378 | - } |
|
379 | - |
|
380 | - $this->compiler = $compiler; |
|
381 | - |
|
382 | - $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
383 | - $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
384 | - $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
385 | - file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
386 | - if ($this->chmod !== null) { |
|
387 | - chmod($compiledFile, $this->chmod); |
|
388 | - } |
|
389 | - |
|
390 | - if (extension_loaded('Zend OPcache')) { |
|
391 | - opcache_invalidate($compiledFile); |
|
392 | - } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
393 | - apc_delete_file($compiledFile); |
|
394 | - } |
|
395 | - |
|
396 | - self::$cache['compiled'][$this->compileId] = true; |
|
397 | - } |
|
398 | - |
|
399 | - return $compiledFile; |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Checks if compiled file is valid (it exists). |
|
404 | - * |
|
405 | - * @param string $file |
|
406 | - * |
|
407 | - * @return bool True cache file existence |
|
408 | - */ |
|
409 | - protected function isValidCompiledFile($file) |
|
410 | - { |
|
411 | - return file_exists($file); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Returns a new template string object with the resource id being the template source code. |
|
416 | - * |
|
417 | - * @param Core $core the dwoo instance requiring it |
|
418 | - * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
419 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
420 | - * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
421 | - * instance that will render this template |
|
422 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
423 | - * template's content unique, if null it defaults to the current url makes this |
|
424 | - * template's content unique, if null it defaults to the current url |
|
425 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
426 | - * others, if null it defaults to the filename+bits of the path template from |
|
427 | - * others, if null it defaults to the filename+bits of the path |
|
428 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
429 | - * extends or any other plugin) an include, extends or any other plugin) |
|
430 | - * |
|
431 | - * @return $this |
|
432 | - */ |
|
433 | - public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
434 | - $compileId = null, ITemplate $parentTemplate = null) |
|
435 | - { |
|
436 | - return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Returns the full compiled file name and assigns a default value to it if |
|
441 | - * required. |
|
442 | - * |
|
443 | - * @param Core $core the Core instance that requests the file name |
|
444 | - * |
|
445 | - * @return string the full path to the compiled file |
|
446 | - */ |
|
447 | - protected function getCompiledFilename(Core $core) |
|
448 | - { |
|
449 | - return $core->getCompileDir() . hash('md4', $this->compileId) . '.d' . Core::RELEASE_TAG . '.php'; |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Returns the full cached file name and assigns a default value to it if |
|
454 | - * required. |
|
455 | - * |
|
456 | - * @param Core $core the dwoo instance that requests the file name |
|
457 | - * |
|
458 | - * @return string the full path to the cached file |
|
459 | - */ |
|
460 | - protected function getCacheFilename(Core $core) |
|
461 | - { |
|
462 | - // no cache id provided, use request_uri as default |
|
463 | - if ($this->cacheId === null) { |
|
464 | - if (isset($_SERVER['REQUEST_URI']) === true) { |
|
465 | - $cacheId = $_SERVER['REQUEST_URI']; |
|
466 | - } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
467 | - $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
468 | - } else { |
|
469 | - $cacheId = ''; |
|
470 | - } |
|
471 | - // force compiled id generation |
|
472 | - $this->getCompiledFilename($core); |
|
473 | - |
|
474 | - $this->cacheId = str_replace('../', '__', |
|
475 | - $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
476 | - } |
|
477 | - |
|
478 | - return $core->getCacheDir() . $this->cacheId . '.html'; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Returns some php code that will check if this template has been modified or not. |
|
483 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
484 | - * |
|
485 | - * @return string |
|
486 | - */ |
|
487 | - public function getIsModifiedCode() |
|
488 | - { |
|
489 | - return null; |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Ensures the given path exists. |
|
494 | - * |
|
495 | - * @param string $path any path |
|
496 | - * @param string $baseDir the base directory where the directory is created |
|
497 | - * ($path must still contain the full path, $baseDir |
|
498 | - * is only used for unix permissions) |
|
499 | - * |
|
500 | - * @throws Exception |
|
501 | - */ |
|
502 | - protected function makeDirectory($path, $baseDir = null) |
|
503 | - { |
|
504 | - if (is_dir($path) === true) { |
|
505 | - return; |
|
506 | - } |
|
507 | - |
|
508 | - if ($this->chmod === null) { |
|
509 | - $chmod = 0777; |
|
510 | - } else { |
|
511 | - $chmod = $this->chmod; |
|
512 | - } |
|
513 | - |
|
514 | - $retries = 3; |
|
515 | - while ($retries --) { |
|
516 | - @mkdir($path, $chmod, true); |
|
517 | - if (is_dir($path)) { |
|
518 | - break; |
|
519 | - } |
|
520 | - usleep(20); |
|
521 | - } |
|
522 | - |
|
523 | - // enforce the correct mode for all directories created |
|
524 | - if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
525 | - $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
526 | - $folders = explode('/', trim($path, '/')); |
|
527 | - foreach ($folders as $folder) { |
|
528 | - $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
529 | - if (!chmod($baseDir, $chmod)) { |
|
530 | - throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
531 | - } |
|
532 | - } |
|
533 | - } |
|
534 | - } |
|
32 | + /** |
|
33 | + * Template name. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $name; |
|
38 | + |
|
39 | + /** |
|
40 | + * Template compilation id. |
|
41 | + * |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + protected $compileId; |
|
45 | + |
|
46 | + /** |
|
47 | + * Template cache id, if not provided in the constructor, it is set to |
|
48 | + * the md4 hash of the request_uri. it is however highly recommended to |
|
49 | + * provide one that will fit your needs. |
|
50 | + * in all cases, the compilation id is prepended to the cache id to separate |
|
51 | + * templates with similar cache ids from one another |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + protected $cacheId; |
|
56 | + |
|
57 | + /** |
|
58 | + * Validity duration of the generated cache file (in seconds). |
|
59 | + * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
60 | + * |
|
61 | + * @var int |
|
62 | + */ |
|
63 | + protected $cacheTime; |
|
64 | + |
|
65 | + /** |
|
66 | + * Boolean flag that defines whether the compilation should be enforced (once) or |
|
67 | + * not use this if you have issues with the compiled templates not being updated |
|
68 | + * but if you do need this it's most likely that you should file a bug report. |
|
69 | + * |
|
70 | + * @var bool |
|
71 | + */ |
|
72 | + protected $compilationEnforced; |
|
73 | + |
|
74 | + /** |
|
75 | + * Caches the results of the file checks to save some time when the same |
|
76 | + * templates is rendered several times. |
|
77 | + * |
|
78 | + * @var array |
|
79 | + */ |
|
80 | + protected static $cache = array( |
|
81 | + 'cached' => array(), |
|
82 | + 'compiled' => array() |
|
83 | + ); |
|
84 | + |
|
85 | + /** |
|
86 | + * Holds the compiler that built this template. |
|
87 | + * |
|
88 | + * @var ICompiler |
|
89 | + */ |
|
90 | + protected $compiler; |
|
91 | + |
|
92 | + /** |
|
93 | + * Chmod value for all files written (cached or compiled ones). |
|
94 | + * set to null if you don't want any chmod operation to happen |
|
95 | + * |
|
96 | + * @var int |
|
97 | + */ |
|
98 | + protected $chmod = 0777; |
|
99 | + |
|
100 | + /** |
|
101 | + * Containing template string. |
|
102 | + * |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + protected $template; |
|
106 | + |
|
107 | + /** |
|
108 | + * Creates a template from a string. |
|
109 | + * |
|
110 | + * @param string $templateString the template to use |
|
111 | + * @param int $cacheTime duration of the cache validity for this template, |
|
112 | + * if null it defaults to the Dwoo instance that will |
|
113 | + * render this template, set to -1 for infinite cache or 0 to disable |
|
114 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
115 | + * makes this template's content unique, if null it defaults |
|
116 | + * to the current url |
|
117 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
118 | + * template from others, if null it defaults to the md4 hash of the template |
|
119 | + */ |
|
120 | + public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
121 | + { |
|
122 | + $this->template = $templateString; |
|
123 | + $this->name = hash('md4', $templateString); |
|
124 | + $this->cacheTime = $cacheTime; |
|
125 | + |
|
126 | + if ($compileId !== null) { |
|
127 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
128 | + } else { |
|
129 | + $this->compileId = $templateString; |
|
130 | + } |
|
131 | + |
|
132 | + if ($cacheId !== null) { |
|
133 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Returns the cache duration for this template. |
|
139 | + * defaults to null if it was not provided |
|
140 | + * |
|
141 | + * @return int|null |
|
142 | + */ |
|
143 | + public function getCacheTime() |
|
144 | + { |
|
145 | + return $this->cacheTime; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Sets the cache duration for this template. |
|
150 | + * can be used to set it after the object is created if you did not provide |
|
151 | + * it in the constructor |
|
152 | + * |
|
153 | + * @param int $seconds duration of the cache validity for this template, if |
|
154 | + * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
155 | + * -1 = infinite cache |
|
156 | + */ |
|
157 | + public function setCacheTime($seconds = null) |
|
158 | + { |
|
159 | + $this->cacheTime = $seconds; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Returns the chmod value for all files written (cached or compiled ones). |
|
164 | + * defaults to 0777 |
|
165 | + * |
|
166 | + * @return int|null |
|
167 | + */ |
|
168 | + public function getChmod() |
|
169 | + { |
|
170 | + return $this->chmod; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Set the chmod value for all files written (cached or compiled ones). |
|
175 | + * set to null if you don't want to do any chmod() operation |
|
176 | + * |
|
177 | + * @param int $mask new bitmask to use for all files |
|
178 | + */ |
|
179 | + public function setChmod($mask = null) |
|
180 | + { |
|
181 | + $this->chmod = $mask; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Returns the template name. |
|
186 | + * |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public function getName() |
|
190 | + { |
|
191 | + return $this->name; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Returns the resource name for this template class. |
|
196 | + * |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function getResourceName() |
|
200 | + { |
|
201 | + return 'string'; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
206 | + * |
|
207 | + * @return false |
|
208 | + */ |
|
209 | + public function getResourceIdentifier() |
|
210 | + { |
|
211 | + return false; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Returns the template source of this template. |
|
216 | + * |
|
217 | + * @return string |
|
218 | + */ |
|
219 | + public function getSource() |
|
220 | + { |
|
221 | + return $this->template; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Returns an unique value identifying the current version of this template, |
|
226 | + * in this case it's the md4 hash of the content. |
|
227 | + * |
|
228 | + * @return string |
|
229 | + */ |
|
230 | + public function getUid() |
|
231 | + { |
|
232 | + return $this->name; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Returns the compiler used by this template, if it was just compiled, or null. |
|
237 | + * |
|
238 | + * @return ICompiler |
|
239 | + */ |
|
240 | + public function getCompiler() |
|
241 | + { |
|
242 | + return $this->compiler; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Marks this template as compile-forced, which means it will be recompiled even if it |
|
247 | + * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
248 | + * it's only meant to be used in development (and the development of dwoo particularly). |
|
249 | + */ |
|
250 | + public function forceCompilation() |
|
251 | + { |
|
252 | + $this->compilationEnforced = true; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Returns the cached template output file name, true if it's cache-able but not cached |
|
257 | + * or false if it's not cached. |
|
258 | + * |
|
259 | + * @param Core $core the dwoo instance that requests it |
|
260 | + * |
|
261 | + * @return string|bool |
|
262 | + */ |
|
263 | + public function getCachedTemplate(Core $core) |
|
264 | + { |
|
265 | + $cacheLength = $core->getCacheTime(); |
|
266 | + if ($this->cacheTime !== null) { |
|
267 | + $cacheLength = $this->cacheTime; |
|
268 | + } |
|
269 | + |
|
270 | + // file is not cacheable |
|
271 | + if ($cacheLength == 0) { |
|
272 | + return false; |
|
273 | + } |
|
274 | + |
|
275 | + $cachedFile = $this->getCacheFilename($core); |
|
276 | + |
|
277 | + if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
278 | + // already checked, return cache file |
|
279 | + return $cachedFile; |
|
280 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
281 | + // cache is still valid and can be loaded |
|
282 | + self::$cache['cached'][$this->cacheId] = true; |
|
283 | + |
|
284 | + return $cachedFile; |
|
285 | + } |
|
286 | + |
|
287 | + // file is cacheable |
|
288 | + return true; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Caches the provided output into the cache file. |
|
293 | + * |
|
294 | + * @param Core $core the dwoo instance that requests it |
|
295 | + * @param string $output the template output |
|
296 | + * |
|
297 | + * @return mixed full path of the cached file or false upon failure |
|
298 | + */ |
|
299 | + public function cache(Core $core, $output) |
|
300 | + { |
|
301 | + $cacheDir = $core->getCacheDir(); |
|
302 | + $cachedFile = $this->getCacheFilename($core); |
|
303 | + |
|
304 | + // the code below is courtesy of Rasmus Schultz, |
|
305 | + // thanks for his help on avoiding concurency issues |
|
306 | + $temp = tempnam($cacheDir, 'temp'); |
|
307 | + if (!($file = @fopen($temp, 'wb'))) { |
|
308 | + $temp = $cacheDir . uniqid('temp'); |
|
309 | + if (!($file = @fopen($temp, 'wb'))) { |
|
310 | + trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
311 | + |
|
312 | + return false; |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + fwrite($file, $output); |
|
317 | + fclose($file); |
|
318 | + |
|
319 | + $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
320 | + if (!@rename($temp, $cachedFile)) { |
|
321 | + @unlink($cachedFile); |
|
322 | + @rename($temp, $cachedFile); |
|
323 | + } |
|
324 | + |
|
325 | + if ($this->chmod !== null) { |
|
326 | + chmod($cachedFile, $this->chmod); |
|
327 | + } |
|
328 | + |
|
329 | + self::$cache['cached'][$this->cacheId] = true; |
|
330 | + |
|
331 | + return $cachedFile; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Clears the cached template if it's older than the given time. |
|
336 | + * |
|
337 | + * @param Core $core the dwoo instance that was used to cache that template |
|
338 | + * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
339 | + * |
|
340 | + * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
341 | + */ |
|
342 | + public function clearCache(Core $core, $olderThan = - 1) |
|
343 | + { |
|
344 | + $cachedFile = $this->getCacheFilename($core); |
|
345 | + |
|
346 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Returns the compiled template file name. |
|
351 | + * |
|
352 | + * @param Core $core the dwoo instance that requests it |
|
353 | + * @param ICompiler $compiler the compiler that must be used |
|
354 | + * |
|
355 | + * @return string |
|
356 | + */ |
|
357 | + public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
358 | + { |
|
359 | + $compiledFile = $this->getCompiledFilename($core); |
|
360 | + |
|
361 | + if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
362 | + // already checked, return compiled file |
|
363 | + } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
364 | + // template is compiled |
|
365 | + self::$cache['compiled'][$this->compileId] = true; |
|
366 | + } else { |
|
367 | + // compiles the template |
|
368 | + $this->compilationEnforced = false; |
|
369 | + |
|
370 | + if ($compiler === null) { |
|
371 | + $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
372 | + |
|
373 | + if ($compiler === null || $compiler === array('Dwoo\Compiler', 'compilerFactory')) { |
|
374 | + $compiler = Compiler::compilerFactory(); |
|
375 | + } else { |
|
376 | + $compiler = call_user_func($compiler); |
|
377 | + } |
|
378 | + } |
|
379 | + |
|
380 | + $this->compiler = $compiler; |
|
381 | + |
|
382 | + $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
383 | + $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
384 | + $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
385 | + file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
386 | + if ($this->chmod !== null) { |
|
387 | + chmod($compiledFile, $this->chmod); |
|
388 | + } |
|
389 | + |
|
390 | + if (extension_loaded('Zend OPcache')) { |
|
391 | + opcache_invalidate($compiledFile); |
|
392 | + } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
393 | + apc_delete_file($compiledFile); |
|
394 | + } |
|
395 | + |
|
396 | + self::$cache['compiled'][$this->compileId] = true; |
|
397 | + } |
|
398 | + |
|
399 | + return $compiledFile; |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Checks if compiled file is valid (it exists). |
|
404 | + * |
|
405 | + * @param string $file |
|
406 | + * |
|
407 | + * @return bool True cache file existence |
|
408 | + */ |
|
409 | + protected function isValidCompiledFile($file) |
|
410 | + { |
|
411 | + return file_exists($file); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Returns a new template string object with the resource id being the template source code. |
|
416 | + * |
|
417 | + * @param Core $core the dwoo instance requiring it |
|
418 | + * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
419 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
420 | + * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
421 | + * instance that will render this template |
|
422 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
423 | + * template's content unique, if null it defaults to the current url makes this |
|
424 | + * template's content unique, if null it defaults to the current url |
|
425 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
426 | + * others, if null it defaults to the filename+bits of the path template from |
|
427 | + * others, if null it defaults to the filename+bits of the path |
|
428 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
429 | + * extends or any other plugin) an include, extends or any other plugin) |
|
430 | + * |
|
431 | + * @return $this |
|
432 | + */ |
|
433 | + public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
434 | + $compileId = null, ITemplate $parentTemplate = null) |
|
435 | + { |
|
436 | + return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Returns the full compiled file name and assigns a default value to it if |
|
441 | + * required. |
|
442 | + * |
|
443 | + * @param Core $core the Core instance that requests the file name |
|
444 | + * |
|
445 | + * @return string the full path to the compiled file |
|
446 | + */ |
|
447 | + protected function getCompiledFilename(Core $core) |
|
448 | + { |
|
449 | + return $core->getCompileDir() . hash('md4', $this->compileId) . '.d' . Core::RELEASE_TAG . '.php'; |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Returns the full cached file name and assigns a default value to it if |
|
454 | + * required. |
|
455 | + * |
|
456 | + * @param Core $core the dwoo instance that requests the file name |
|
457 | + * |
|
458 | + * @return string the full path to the cached file |
|
459 | + */ |
|
460 | + protected function getCacheFilename(Core $core) |
|
461 | + { |
|
462 | + // no cache id provided, use request_uri as default |
|
463 | + if ($this->cacheId === null) { |
|
464 | + if (isset($_SERVER['REQUEST_URI']) === true) { |
|
465 | + $cacheId = $_SERVER['REQUEST_URI']; |
|
466 | + } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
467 | + $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
468 | + } else { |
|
469 | + $cacheId = ''; |
|
470 | + } |
|
471 | + // force compiled id generation |
|
472 | + $this->getCompiledFilename($core); |
|
473 | + |
|
474 | + $this->cacheId = str_replace('../', '__', |
|
475 | + $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
476 | + } |
|
477 | + |
|
478 | + return $core->getCacheDir() . $this->cacheId . '.html'; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Returns some php code that will check if this template has been modified or not. |
|
483 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
484 | + * |
|
485 | + * @return string |
|
486 | + */ |
|
487 | + public function getIsModifiedCode() |
|
488 | + { |
|
489 | + return null; |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Ensures the given path exists. |
|
494 | + * |
|
495 | + * @param string $path any path |
|
496 | + * @param string $baseDir the base directory where the directory is created |
|
497 | + * ($path must still contain the full path, $baseDir |
|
498 | + * is only used for unix permissions) |
|
499 | + * |
|
500 | + * @throws Exception |
|
501 | + */ |
|
502 | + protected function makeDirectory($path, $baseDir = null) |
|
503 | + { |
|
504 | + if (is_dir($path) === true) { |
|
505 | + return; |
|
506 | + } |
|
507 | + |
|
508 | + if ($this->chmod === null) { |
|
509 | + $chmod = 0777; |
|
510 | + } else { |
|
511 | + $chmod = $this->chmod; |
|
512 | + } |
|
513 | + |
|
514 | + $retries = 3; |
|
515 | + while ($retries --) { |
|
516 | + @mkdir($path, $chmod, true); |
|
517 | + if (is_dir($path)) { |
|
518 | + break; |
|
519 | + } |
|
520 | + usleep(20); |
|
521 | + } |
|
522 | + |
|
523 | + // enforce the correct mode for all directories created |
|
524 | + if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
525 | + $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
526 | + $folders = explode('/', trim($path, '/')); |
|
527 | + foreach ($folders as $folder) { |
|
528 | + $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
529 | + if (!chmod($baseDir, $chmod)) { |
|
530 | + throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
531 | + } |
|
532 | + } |
|
533 | + } |
|
534 | + } |
|
535 | 535 | } |
@@ -26,35 +26,35 @@ |
||
26 | 26 | */ |
27 | 27 | class Exception extends DwooException |
28 | 28 | { |
29 | - protected $compiler; |
|
30 | - protected $template; |
|
29 | + protected $compiler; |
|
30 | + protected $template; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Exception constructor. |
|
34 | - * |
|
35 | - * @param DwooCompiler $compiler |
|
36 | - * @param int $message |
|
37 | - */ |
|
38 | - public function __construct(DwooCompiler $compiler, $message) |
|
39 | - { |
|
40 | - $this->compiler = $compiler; |
|
41 | - $this->template = $compiler->getCore()->getTemplate(); |
|
42 | - parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
43 | - } |
|
32 | + /** |
|
33 | + * Exception constructor. |
|
34 | + * |
|
35 | + * @param DwooCompiler $compiler |
|
36 | + * @param int $message |
|
37 | + */ |
|
38 | + public function __construct(DwooCompiler $compiler, $message) |
|
39 | + { |
|
40 | + $this->compiler = $compiler; |
|
41 | + $this->template = $compiler->getCore()->getTemplate(); |
|
42 | + parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return DwooCompiler |
|
47 | - */ |
|
48 | - public function getCompiler() |
|
49 | - { |
|
50 | - return $this->compiler; |
|
51 | - } |
|
45 | + /** |
|
46 | + * @return DwooCompiler |
|
47 | + */ |
|
48 | + public function getCompiler() |
|
49 | + { |
|
50 | + return $this->compiler; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return \Dwoo\ITemplate|null |
|
55 | - */ |
|
56 | - public function getTemplate() |
|
57 | - { |
|
58 | - return $this->template; |
|
59 | - } |
|
53 | + /** |
|
54 | + * @return \Dwoo\ITemplate|null |
|
55 | + */ |
|
56 | + public function getTemplate() |
|
57 | + { |
|
58 | + return $this->template; |
|
59 | + } |
|
60 | 60 | } |
@@ -34,28 +34,28 @@ discard block |
||
34 | 34 | */ |
35 | 35 | class PluginExtends extends Plugin implements ICompilable |
36 | 36 | { |
37 | - protected static $childSource; |
|
38 | - protected static $regex; |
|
39 | - protected static $l; |
|
40 | - protected static $r; |
|
41 | - protected static $lastReplacement; |
|
42 | - |
|
43 | - public function process() |
|
44 | - { |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @param Compiler $compiler |
|
49 | - * @param $file |
|
50 | - * |
|
51 | - * @throws CompilationException |
|
52 | - */ |
|
53 | - public static function compile(Compiler $compiler, $file) |
|
54 | - { |
|
55 | - list($l, $r) = $compiler->getDelimiters(); |
|
56 | - self::$l = preg_quote($l, '/'); |
|
57 | - self::$r = preg_quote($r, '/'); |
|
58 | - self::$regex = '/ |
|
37 | + protected static $childSource; |
|
38 | + protected static $regex; |
|
39 | + protected static $l; |
|
40 | + protected static $r; |
|
41 | + protected static $lastReplacement; |
|
42 | + |
|
43 | + public function process() |
|
44 | + { |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @param Compiler $compiler |
|
49 | + * @param $file |
|
50 | + * |
|
51 | + * @throws CompilationException |
|
52 | + */ |
|
53 | + public static function compile(Compiler $compiler, $file) |
|
54 | + { |
|
55 | + list($l, $r) = $compiler->getDelimiters(); |
|
56 | + self::$l = preg_quote($l, '/'); |
|
57 | + self::$r = preg_quote($r, '/'); |
|
58 | + self::$regex = '/ |
|
59 | 59 | ' . self::$l . 'block\s(["\']?)(.+?)\1' . self::$r . '(?:\r?\n?) |
60 | 60 | ((?: |
61 | 61 | (?R) |
@@ -70,137 +70,137 @@ discard block |
||
70 | 70 | ' . self::$l . '\/block' . self::$r . ' |
71 | 71 | /six'; |
72 | 72 | |
73 | - if ($compiler->getLooseOpeningHandling()) { |
|
74 | - self::$l .= '\s*'; |
|
75 | - self::$r = '\s*' . self::$r; |
|
76 | - } |
|
77 | - $inheritanceTree = array(array('source' => $compiler->getTemplateSource())); |
|
78 | - $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR; |
|
79 | - $curTpl = $compiler->getCore()->getTemplate(); |
|
80 | - |
|
81 | - while (!empty($file)) { |
|
82 | - if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) { |
|
83 | - throw new CompilationException($compiler, 'Extends : The file name must be a non-empty string'); |
|
84 | - } |
|
85 | - |
|
86 | - if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) { |
|
87 | - // resource:identifier given, extract them |
|
88 | - $resource = $m[1]; |
|
89 | - $identifier = $m[2]; |
|
90 | - } else { |
|
91 | - // get the current template's resource |
|
92 | - $resource = $curTpl->getResourceName(); |
|
93 | - $identifier = substr($file, 1, - 1); |
|
94 | - } |
|
95 | - |
|
96 | - try { |
|
97 | - $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
|
98 | - } |
|
99 | - catch (SecurityException $e) { |
|
100 | - throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); |
|
101 | - } |
|
102 | - catch (Exception $e) { |
|
103 | - throw new CompilationException($compiler, 'Extends : ' . $e->getMessage()); |
|
104 | - } |
|
105 | - |
|
106 | - if ($parent === null) { |
|
107 | - throw new CompilationException($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
108 | - } elseif ($parent === false) { |
|
109 | - throw new CompilationException($compiler, 'Extends : Resource "' . $resource . '" does not support extends.'); |
|
110 | - } |
|
111 | - |
|
112 | - $curTpl = $parent; |
|
113 | - $newParent = array( |
|
114 | - 'source' => $parent->getSource(), |
|
115 | - 'resource' => $resource, |
|
116 | - 'identifier' => $parent->getResourceIdentifier(), |
|
117 | - 'uid' => $parent->getUid() |
|
118 | - ); |
|
119 | - if (array_search($newParent, $inheritanceTree, true) !== false) { |
|
120 | - throw new CompilationException($compiler, 'Extends : Recursive template inheritance detected'); |
|
121 | - } |
|
122 | - $inheritanceTree[] = $newParent; |
|
123 | - |
|
124 | - if (preg_match('/^' . self::$l . 'extends(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?' . self::$r . '/i', $parent->getSource(), $match)) { |
|
125 | - $curPath = dirname($identifier) . DIRECTORY_SEPARATOR; |
|
126 | - if (isset($match[2]) && $match[2] == '"') { |
|
127 | - $file = '"' . str_replace('"', '\\"', substr($match[1], 1, - 1)) . '"'; |
|
128 | - } elseif (isset($match[2]) && $match[2] == "'") { |
|
129 | - $file = '"' . substr($match[1], 1, - 1) . '"'; |
|
130 | - } else { |
|
131 | - $file = '"' . $match[1] . '"'; |
|
132 | - } |
|
133 | - } else { |
|
134 | - $file = false; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - while (true) { |
|
139 | - $parent = array_pop($inheritanceTree); |
|
140 | - $child = end($inheritanceTree); |
|
141 | - self::$childSource = $child['source']; |
|
142 | - self::$lastReplacement = count($inheritanceTree) === 1; |
|
143 | - if (!isset($newSource)) { |
|
144 | - $newSource = $parent['source']; |
|
145 | - } |
|
146 | - $newSource = preg_replace_callback(self::$regex, array( |
|
147 | - 'Dwoo\Plugins\Functions\PluginExtends', |
|
148 | - 'replaceBlock' |
|
149 | - ), $newSource); |
|
150 | - $newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource; |
|
151 | - |
|
152 | - if (self::$lastReplacement) { |
|
153 | - break; |
|
154 | - } |
|
155 | - } |
|
156 | - $compiler->setTemplateSource($newSource); |
|
157 | - $compiler->recompile(); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @param array $matches |
|
162 | - * |
|
163 | - * @return mixed|string |
|
164 | - */ |
|
165 | - protected static function replaceBlock(array $matches) |
|
166 | - { |
|
167 | - $matches[3] = self::removeTrailingNewline($matches[3]); |
|
168 | - |
|
169 | - if (preg_match_all(self::$regex, self::$childSource, $override) && in_array($matches[2], $override[2])) { |
|
170 | - $key = array_search($matches[2], $override[2]); |
|
171 | - $override = self::removeTrailingNewline($override[3][$key]); |
|
172 | - |
|
173 | - $l = stripslashes(self::$l); |
|
174 | - $r = stripslashes(self::$r); |
|
175 | - |
|
176 | - if (self::$lastReplacement) { |
|
177 | - return preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override); |
|
178 | - } |
|
179 | - |
|
180 | - return $l . 'block ' . $matches[1] . $matches[2] . $matches[1] . $r . preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override) . $l . '/block' . $r; |
|
181 | - } |
|
182 | - |
|
183 | - if (preg_match(self::$regex, $matches[3])) { |
|
184 | - return preg_replace_callback(self::$regex, array( |
|
185 | - 'Dwoo\Plugins\Functions\PluginExtends', |
|
186 | - 'replaceBlock' |
|
187 | - ), $matches[3]); |
|
188 | - } |
|
189 | - |
|
190 | - if (self::$lastReplacement) { |
|
191 | - return $matches[3]; |
|
192 | - } |
|
193 | - |
|
194 | - return $matches[0]; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param $text |
|
199 | - * |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - protected static function removeTrailingNewline($text) |
|
203 | - { |
|
204 | - return substr($text, - 1) === "\n" ? substr($text, - 2, 1) === "\r" ? substr($text, 0, - 2) : substr($text, 0, - 1) : $text; |
|
205 | - } |
|
73 | + if ($compiler->getLooseOpeningHandling()) { |
|
74 | + self::$l .= '\s*'; |
|
75 | + self::$r = '\s*' . self::$r; |
|
76 | + } |
|
77 | + $inheritanceTree = array(array('source' => $compiler->getTemplateSource())); |
|
78 | + $curPath = dirname($compiler->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR; |
|
79 | + $curTpl = $compiler->getCore()->getTemplate(); |
|
80 | + |
|
81 | + while (!empty($file)) { |
|
82 | + if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) { |
|
83 | + throw new CompilationException($compiler, 'Extends : The file name must be a non-empty string'); |
|
84 | + } |
|
85 | + |
|
86 | + if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) { |
|
87 | + // resource:identifier given, extract them |
|
88 | + $resource = $m[1]; |
|
89 | + $identifier = $m[2]; |
|
90 | + } else { |
|
91 | + // get the current template's resource |
|
92 | + $resource = $curTpl->getResourceName(); |
|
93 | + $identifier = substr($file, 1, - 1); |
|
94 | + } |
|
95 | + |
|
96 | + try { |
|
97 | + $parent = $compiler->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl); |
|
98 | + } |
|
99 | + catch (SecurityException $e) { |
|
100 | + throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); |
|
101 | + } |
|
102 | + catch (Exception $e) { |
|
103 | + throw new CompilationException($compiler, 'Extends : ' . $e->getMessage()); |
|
104 | + } |
|
105 | + |
|
106 | + if ($parent === null) { |
|
107 | + throw new CompilationException($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
108 | + } elseif ($parent === false) { |
|
109 | + throw new CompilationException($compiler, 'Extends : Resource "' . $resource . '" does not support extends.'); |
|
110 | + } |
|
111 | + |
|
112 | + $curTpl = $parent; |
|
113 | + $newParent = array( |
|
114 | + 'source' => $parent->getSource(), |
|
115 | + 'resource' => $resource, |
|
116 | + 'identifier' => $parent->getResourceIdentifier(), |
|
117 | + 'uid' => $parent->getUid() |
|
118 | + ); |
|
119 | + if (array_search($newParent, $inheritanceTree, true) !== false) { |
|
120 | + throw new CompilationException($compiler, 'Extends : Recursive template inheritance detected'); |
|
121 | + } |
|
122 | + $inheritanceTree[] = $newParent; |
|
123 | + |
|
124 | + if (preg_match('/^' . self::$l . 'extends(?:\(?\s*|\s+)(?:file=)?\s*((["\']).+?\2|\S+?)\s*\)?\s*?' . self::$r . '/i', $parent->getSource(), $match)) { |
|
125 | + $curPath = dirname($identifier) . DIRECTORY_SEPARATOR; |
|
126 | + if (isset($match[2]) && $match[2] == '"') { |
|
127 | + $file = '"' . str_replace('"', '\\"', substr($match[1], 1, - 1)) . '"'; |
|
128 | + } elseif (isset($match[2]) && $match[2] == "'") { |
|
129 | + $file = '"' . substr($match[1], 1, - 1) . '"'; |
|
130 | + } else { |
|
131 | + $file = '"' . $match[1] . '"'; |
|
132 | + } |
|
133 | + } else { |
|
134 | + $file = false; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + while (true) { |
|
139 | + $parent = array_pop($inheritanceTree); |
|
140 | + $child = end($inheritanceTree); |
|
141 | + self::$childSource = $child['source']; |
|
142 | + self::$lastReplacement = count($inheritanceTree) === 1; |
|
143 | + if (!isset($newSource)) { |
|
144 | + $newSource = $parent['source']; |
|
145 | + } |
|
146 | + $newSource = preg_replace_callback(self::$regex, array( |
|
147 | + 'Dwoo\Plugins\Functions\PluginExtends', |
|
148 | + 'replaceBlock' |
|
149 | + ), $newSource); |
|
150 | + $newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource; |
|
151 | + |
|
152 | + if (self::$lastReplacement) { |
|
153 | + break; |
|
154 | + } |
|
155 | + } |
|
156 | + $compiler->setTemplateSource($newSource); |
|
157 | + $compiler->recompile(); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @param array $matches |
|
162 | + * |
|
163 | + * @return mixed|string |
|
164 | + */ |
|
165 | + protected static function replaceBlock(array $matches) |
|
166 | + { |
|
167 | + $matches[3] = self::removeTrailingNewline($matches[3]); |
|
168 | + |
|
169 | + if (preg_match_all(self::$regex, self::$childSource, $override) && in_array($matches[2], $override[2])) { |
|
170 | + $key = array_search($matches[2], $override[2]); |
|
171 | + $override = self::removeTrailingNewline($override[3][$key]); |
|
172 | + |
|
173 | + $l = stripslashes(self::$l); |
|
174 | + $r = stripslashes(self::$r); |
|
175 | + |
|
176 | + if (self::$lastReplacement) { |
|
177 | + return preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override); |
|
178 | + } |
|
179 | + |
|
180 | + return $l . 'block ' . $matches[1] . $matches[2] . $matches[1] . $r . preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override) . $l . '/block' . $r; |
|
181 | + } |
|
182 | + |
|
183 | + if (preg_match(self::$regex, $matches[3])) { |
|
184 | + return preg_replace_callback(self::$regex, array( |
|
185 | + 'Dwoo\Plugins\Functions\PluginExtends', |
|
186 | + 'replaceBlock' |
|
187 | + ), $matches[3]); |
|
188 | + } |
|
189 | + |
|
190 | + if (self::$lastReplacement) { |
|
191 | + return $matches[3]; |
|
192 | + } |
|
193 | + |
|
194 | + return $matches[0]; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param $text |
|
199 | + * |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + protected static function removeTrailingNewline($text) |
|
203 | + { |
|
204 | + return substr($text, - 1) === "\n" ? substr($text, - 2, 1) === "\r" ? substr($text, 0, - 2) : substr($text, 0, - 1) : $text; |
|
205 | + } |
|
206 | 206 | } |
@@ -28,37 +28,37 @@ discard block |
||
28 | 28 | */ |
29 | 29 | class PluginExtendsCheck extends Plugin implements ICompilable |
30 | 30 | { |
31 | - /** |
|
32 | - * @param Compiler $compiler |
|
33 | - * @param $file |
|
34 | - * |
|
35 | - * @return string |
|
36 | - * @throws CompilationException |
|
37 | - */ |
|
38 | - public static function compile(Compiler $compiler, $file) |
|
39 | - { |
|
40 | - preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m); |
|
41 | - $resource = $m[1]; |
|
42 | - $identifier = $m[2]; |
|
31 | + /** |
|
32 | + * @param Compiler $compiler |
|
33 | + * @param $file |
|
34 | + * |
|
35 | + * @return string |
|
36 | + * @throws CompilationException |
|
37 | + */ |
|
38 | + public static function compile(Compiler $compiler, $file) |
|
39 | + { |
|
40 | + preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m); |
|
41 | + $resource = $m[1]; |
|
42 | + $identifier = $m[2]; |
|
43 | 43 | |
44 | - $tpl = $compiler->getCore()->templateFactory($resource, $identifier); |
|
44 | + $tpl = $compiler->getCore()->templateFactory($resource, $identifier); |
|
45 | 45 | |
46 | - if ($tpl === null) { |
|
47 | - throw new CompilationException($compiler, |
|
48 | - 'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
49 | - } elseif ($tpl === false) { |
|
50 | - throw new CompilationException($compiler, |
|
51 | - 'Load Templates : Resource "' . $resource . '" does not support includes.'); |
|
52 | - } |
|
46 | + if ($tpl === null) { |
|
47 | + throw new CompilationException($compiler, |
|
48 | + 'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
49 | + } elseif ($tpl === false) { |
|
50 | + throw new CompilationException($compiler, |
|
51 | + 'Load Templates : Resource "' . $resource . '" does not support includes.'); |
|
52 | + } |
|
53 | 53 | |
54 | - $out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; |
|
54 | + $out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; |
|
55 | 55 | |
56 | - $modCheck = $tpl->getIsModifiedCode(); |
|
56 | + $modCheck = $tpl->getIsModifiedCode(); |
|
57 | 57 | |
58 | - if ($modCheck) { |
|
59 | - $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; |
|
60 | - } else { |
|
61 | - $out .= 'try { |
|
58 | + if ($modCheck) { |
|
59 | + $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; |
|
60 | + } else { |
|
61 | + $out .= 'try { |
|
62 | 62 | $tpl = $this->templateFactory("' . $resource . '", "' . $identifier . '"); |
63 | 63 | } catch (Dwoo\Exception $e) { |
64 | 64 | $this->triggerError(\'Load Templates : Resource <em>' . $resource . '</em> was not added to Dwoo, can not extend <em>' . $identifier . '</em>\', E_USER_WARNING); |
@@ -68,8 +68,8 @@ discard block |
||
68 | 68 | elseif ($tpl === false) |
69 | 69 | $this->triggerError(\'Load Templates : Resource "' . $resource . '" does not support extends.\', E_USER_WARNING); |
70 | 70 | if ($tpl->getUid() != "' . $tpl->getUid() . '") { ob_end_clean(); return false; }'; |
71 | - } |
|
71 | + } |
|
72 | 72 | |
73 | - return $out; |
|
74 | - } |
|
73 | + return $out; |
|
74 | + } |
|
75 | 75 | } |
76 | 76 | \ No newline at end of file |
@@ -36,70 +36,70 @@ |
||
36 | 36 | */ |
37 | 37 | class PluginTif extends Plugin implements ICompilable |
38 | 38 | { |
39 | - /** |
|
40 | - * @param Compiler $compiler |
|
41 | - * @param array $rest |
|
42 | - * @param array $tokens |
|
43 | - * |
|
44 | - * @return mixed|string |
|
45 | - * @throws CompilationException |
|
46 | - */ |
|
47 | - public static function compile(Compiler $compiler, array $rest, array $tokens) |
|
48 | - { |
|
49 | - // load if plugin |
|
50 | - if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
|
51 | - try { |
|
52 | - $compiler->getCore()->getLoader()->loadPlugin('if'); |
|
53 | - } |
|
54 | - catch (Exception $e) { |
|
55 | - throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); |
|
56 | - } |
|
57 | - } |
|
39 | + /** |
|
40 | + * @param Compiler $compiler |
|
41 | + * @param array $rest |
|
42 | + * @param array $tokens |
|
43 | + * |
|
44 | + * @return mixed|string |
|
45 | + * @throws CompilationException |
|
46 | + */ |
|
47 | + public static function compile(Compiler $compiler, array $rest, array $tokens) |
|
48 | + { |
|
49 | + // load if plugin |
|
50 | + if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
|
51 | + try { |
|
52 | + $compiler->getCore()->getLoader()->loadPlugin('if'); |
|
53 | + } |
|
54 | + catch (Exception $e) { |
|
55 | + throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); |
|
56 | + } |
|
57 | + } |
|
58 | 58 | |
59 | - if (count($rest) == 1) { |
|
60 | - return $rest[0]; |
|
61 | - } |
|
59 | + if (count($rest) == 1) { |
|
60 | + return $rest[0]; |
|
61 | + } |
|
62 | 62 | |
63 | - // fetch false result and remove the ":" if it was present |
|
64 | - $falseResult = array_pop($rest); |
|
63 | + // fetch false result and remove the ":" if it was present |
|
64 | + $falseResult = array_pop($rest); |
|
65 | 65 | |
66 | - if (trim(end($rest), '"\'') === ':') { |
|
67 | - // remove the ':' if present |
|
68 | - array_pop($rest); |
|
69 | - } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { |
|
70 | - if ($falseResult === '?' || $falseResult === ':') { |
|
71 | - throw new CompilationException($compiler, |
|
72 | - 'Tif: incomplete tif statement, value missing after ' . $falseResult); |
|
73 | - } |
|
74 | - // there was in fact no false result provided, so we move it to be the true result instead |
|
75 | - $trueResult = $falseResult; |
|
76 | - $falseResult = "''"; |
|
77 | - } |
|
66 | + if (trim(end($rest), '"\'') === ':') { |
|
67 | + // remove the ':' if present |
|
68 | + array_pop($rest); |
|
69 | + } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { |
|
70 | + if ($falseResult === '?' || $falseResult === ':') { |
|
71 | + throw new CompilationException($compiler, |
|
72 | + 'Tif: incomplete tif statement, value missing after ' . $falseResult); |
|
73 | + } |
|
74 | + // there was in fact no false result provided, so we move it to be the true result instead |
|
75 | + $trueResult = $falseResult; |
|
76 | + $falseResult = "''"; |
|
77 | + } |
|
78 | 78 | |
79 | - // fetch true result if needed |
|
80 | - if (!isset($trueResult)) { |
|
81 | - $trueResult = array_pop($rest); |
|
82 | - // no true result provided so we use the expression arg |
|
83 | - if ($trueResult === '?') { |
|
84 | - $trueResult = true; |
|
85 | - } |
|
86 | - } |
|
79 | + // fetch true result if needed |
|
80 | + if (!isset($trueResult)) { |
|
81 | + $trueResult = array_pop($rest); |
|
82 | + // no true result provided so we use the expression arg |
|
83 | + if ($trueResult === '?') { |
|
84 | + $trueResult = true; |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - // remove the '?' if present |
|
89 | - if (trim(end($rest), '"\'') === '?') { |
|
90 | - array_pop($rest); |
|
91 | - } |
|
88 | + // remove the '?' if present |
|
89 | + if (trim(end($rest), '"\'') === '?') { |
|
90 | + array_pop($rest); |
|
91 | + } |
|
92 | 92 | |
93 | - // check params were correctly provided |
|
94 | - if (empty($rest) || $trueResult === null || $falseResult === null) { |
|
95 | - throw new CompilationException($compiler, |
|
96 | - 'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>'); |
|
97 | - } |
|
93 | + // check params were correctly provided |
|
94 | + if (empty($rest) || $trueResult === null || $falseResult === null) { |
|
95 | + throw new CompilationException($compiler, |
|
96 | + 'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>'); |
|
97 | + } |
|
98 | 98 | |
99 | - // parse condition |
|
100 | - $condition = PluginIf::replaceKeywords($rest, $tokens, $compiler); |
|
99 | + // parse condition |
|
100 | + $condition = PluginIf::replaceKeywords($rest, $tokens, $compiler); |
|
101 | 101 | |
102 | - return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', |
|
103 | - $condition) : $trueResult) . ' : ' . $falseResult . ')'; |
|
104 | - } |
|
102 | + return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', |
|
103 | + $condition) : $trueResult) . ' : ' . $falseResult . ')'; |
|
104 | + } |
|
105 | 105 | } |
106 | 106 | \ No newline at end of file |
@@ -31,58 +31,58 @@ discard block |
||
31 | 31 | */ |
32 | 32 | class PluginLoadTemplates extends Plugin implements ICompilable |
33 | 33 | { |
34 | - /** |
|
35 | - * @param Compiler $compiler |
|
36 | - * @param string $file |
|
37 | - * |
|
38 | - * @return string |
|
39 | - * @throws CompilationException |
|
40 | - */ |
|
41 | - public static function compile(Compiler $compiler, $file) |
|
42 | - { |
|
43 | - $file = substr($file, 1, - 1); |
|
34 | + /** |
|
35 | + * @param Compiler $compiler |
|
36 | + * @param string $file |
|
37 | + * |
|
38 | + * @return string |
|
39 | + * @throws CompilationException |
|
40 | + */ |
|
41 | + public static function compile(Compiler $compiler, $file) |
|
42 | + { |
|
43 | + $file = substr($file, 1, - 1); |
|
44 | 44 | |
45 | - if ($file === '') { |
|
46 | - return ''; |
|
47 | - } |
|
45 | + if ($file === '') { |
|
46 | + return ''; |
|
47 | + } |
|
48 | 48 | |
49 | - if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) { |
|
50 | - // resource:identifier given, extract them |
|
51 | - $resource = $m[1]; |
|
52 | - $identifier = $m[2]; |
|
53 | - } else { |
|
54 | - // get the current template's resource |
|
55 | - $resource = $compiler->getCore()->getTemplate()->getResourceName(); |
|
56 | - $identifier = $file; |
|
57 | - } |
|
49 | + if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) { |
|
50 | + // resource:identifier given, extract them |
|
51 | + $resource = $m[1]; |
|
52 | + $identifier = $m[2]; |
|
53 | + } else { |
|
54 | + // get the current template's resource |
|
55 | + $resource = $compiler->getCore()->getTemplate()->getResourceName(); |
|
56 | + $identifier = $file; |
|
57 | + } |
|
58 | 58 | |
59 | - $tpl = $compiler->getCore()->templateFactory($resource, $identifier); |
|
59 | + $tpl = $compiler->getCore()->templateFactory($resource, $identifier); |
|
60 | 60 | |
61 | - if ($tpl === null) { |
|
62 | - throw new CompilationException($compiler, |
|
63 | - 'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
64 | - } elseif ($tpl === false) { |
|
65 | - throw new CompilationException($compiler, |
|
66 | - 'Load Templates : Resource "' . $resource . '" does not support includes.'); |
|
67 | - } |
|
61 | + if ($tpl === null) { |
|
62 | + throw new CompilationException($compiler, |
|
63 | + 'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); |
|
64 | + } elseif ($tpl === false) { |
|
65 | + throw new CompilationException($compiler, |
|
66 | + 'Load Templates : Resource "' . $resource . '" does not support includes.'); |
|
67 | + } |
|
68 | 68 | |
69 | - $cmp = clone $compiler; |
|
70 | - $cmp->compile($compiler->getCore(), $tpl); |
|
71 | - foreach ($cmp->getTemplatePlugins() as $template => $args) { |
|
72 | - $compiler->addTemplatePlugin($template, $args['params'], $args['uuid'], $args['body']); |
|
73 | - } |
|
74 | - foreach ($cmp->getUsedPlugins() as $plugin => $type) { |
|
75 | - $compiler->addUsedPlugin($plugin, $type); |
|
76 | - } |
|
69 | + $cmp = clone $compiler; |
|
70 | + $cmp->compile($compiler->getCore(), $tpl); |
|
71 | + foreach ($cmp->getTemplatePlugins() as $template => $args) { |
|
72 | + $compiler->addTemplatePlugin($template, $args['params'], $args['uuid'], $args['body']); |
|
73 | + } |
|
74 | + foreach ($cmp->getUsedPlugins() as $plugin => $type) { |
|
75 | + $compiler->addUsedPlugin($plugin, $type); |
|
76 | + } |
|
77 | 77 | |
78 | - $out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; |
|
78 | + $out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; |
|
79 | 79 | |
80 | - $modCheck = $tpl->getIsModifiedCode(); |
|
80 | + $modCheck = $tpl->getIsModifiedCode(); |
|
81 | 81 | |
82 | - if ($modCheck) { |
|
83 | - $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; |
|
84 | - } else { |
|
85 | - $out .= 'try { |
|
82 | + if ($modCheck) { |
|
83 | + $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; |
|
84 | + } else { |
|
85 | + $out .= 'try { |
|
86 | 86 | $tpl = $this->templateFactory("' . $resource . '", "' . $identifier . '"); |
87 | 87 | } catch (Dwoo\Exception $e) { |
88 | 88 | $this->triggerError(\'Load Templates : Resource <em>' . $resource . '</em> was not added to Dwoo, can not extend <em>' . $identifier . '</em>\', E_USER_WARNING); |
@@ -92,8 +92,8 @@ discard block |
||
92 | 92 | elseif ($tpl === false) |
93 | 93 | $this->triggerError(\'Load Templates : Resource "' . $resource . '" does not support extends.\', E_USER_WARNING); |
94 | 94 | if ($tpl->getUid() != "' . $tpl->getUid() . '") { ob_end_clean(); return false; }'; |
95 | - } |
|
95 | + } |
|
96 | 96 | |
97 | - return $out; |
|
98 | - } |
|
97 | + return $out; |
|
98 | + } |
|
99 | 99 | } |
100 | 100 | \ No newline at end of file |