|
1
|
|
|
<?php |
|
2
|
|
|
namespace app\modules\core\helpers; |
|
3
|
|
|
|
|
4
|
|
|
use app\modules\core\models\ContentBlock; |
|
5
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
6
|
|
|
use yii\caching\TagDependency; |
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
use yii\helpers\Url; |
|
9
|
|
|
use yii; |
|
10
|
|
|
use app; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ContentBlockHelper |
|
14
|
|
|
* Main public static method compileContentString() uses submethods to extract chunk calls from model content field, |
|
15
|
|
|
* fetch chunks from data base table, then compile it and replace chunk calls with compiled chunks data |
|
16
|
|
|
* Example chunk call in model content field should be like: [[$chunk param='value'|'default value' param2=42]]. |
|
17
|
|
|
* Chunk declaration should be like : <p>String: [[+param]]</p> <p>Float: [[+param2:format, param1, param2]]</p> |
|
18
|
|
|
* All supported formats you can find at Yii::$app->formatter |
|
19
|
|
|
* |
|
20
|
|
|
* @package app\modules\core\helpers |
|
21
|
|
|
*/ |
|
22
|
|
|
class ContentBlockHelper |
|
23
|
|
|
{ |
|
24
|
|
|
private static $chunksByKey = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Compiles content string by injecting chunks into content |
|
28
|
|
|
* Preloads chunks which have preload = 1 |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $content Original content with chunk calls |
|
31
|
|
|
* @param string $content_key Key for caching compiled content version |
|
32
|
|
|
* @param yii\caching\Dependency $dependency Cache dependency |
|
33
|
|
|
* @return string Compiled content with injected chunks |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function compileContentString($content, $content_key, $dependency) |
|
36
|
|
|
{ |
|
37
|
|
|
self::preloadChunks(); |
|
38
|
|
|
$output = self::processData($content, $content_key, $dependency); |
|
39
|
|
|
$output = self::processData($output, $content_key, $dependency, '', false); |
|
40
|
|
|
return $output; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Finding chunk calls with regexp |
|
45
|
|
|
* Iterate matches |
|
46
|
|
|
* While iterating: |
|
47
|
|
|
* Extracts single chunk data with sanitizeChunk() method |
|
48
|
|
|
* Fetches chunk by key using fetchChunkByKey(), who returns chunk value by key from static array if exists, otherwise from db |
|
|
|
|
|
|
49
|
|
|
* Compiles single chunk using compileChunk() method |
|
50
|
|
|
* Replaces single chunk call with compiled chunk data in the model content |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $content_key Key for caching compiled content version |
|
53
|
|
|
* @param yii\caching\Dependency $dependency Cache dependency |
|
54
|
|
|
* @param bool $preprocess flag to separate rendering non cacheable chunks such as Form |
|
55
|
|
|
* @param string $content |
|
56
|
|
|
* @param string $chunk_key ContentBlock key string to prevent endless recursion |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
private static function processData($content, $content_key, $dependency, $chunk_key = '', $preprocess = true) |
|
60
|
|
|
{ |
|
61
|
|
|
$matches = []; |
|
62
|
|
|
$replacement = ''; |
|
63
|
|
|
preg_match_all('%\[\[([^\]\[]+)\]\]%ui', $content, $matches); |
|
64
|
|
|
if (!empty($matches[0])) { |
|
65
|
|
|
foreach ($matches[0] as $k => $rawChunk) { |
|
66
|
|
|
$chunkData = self::sanitizeChunk($rawChunk); |
|
67
|
|
|
if ($chunkData['key'] == $chunk_key) { |
|
68
|
|
|
$content = str_replace($matches[0][$k], '', $content); |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
$cacheKey = $content_key . $chunkData['key'] . serialize($chunkData); |
|
72
|
|
|
switch ($chunkData['token']) { |
|
73
|
|
|
case '$': |
|
74
|
|
|
if ($preprocess === false) break; |
|
75
|
|
|
$chunk = self::fetchChunkByKey($chunkData['key']); |
|
76
|
|
|
$replacement = Yii::$app->cache->get($cacheKey); |
|
77
|
|
|
if ($replacement === false) { |
|
78
|
|
|
$replacement = static::compileChunk($chunk, $chunkData, $chunkData['key'], $content_key, $dependency); |
|
|
|
|
|
|
79
|
|
|
Yii::$app->cache->set( |
|
80
|
|
|
$content_key, |
|
81
|
|
|
$replacement, |
|
82
|
|
|
84600, |
|
83
|
|
|
$dependency |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
break; |
|
87
|
|
|
case '%': |
|
88
|
|
|
if ($preprocess === true) { |
|
89
|
|
|
$replacement = $rawChunk; |
|
90
|
|
|
break; |
|
91
|
|
|
} |
|
92
|
|
|
$replacement = static::replaceForms($chunkData); |
|
|
|
|
|
|
93
|
|
|
break; |
|
94
|
|
|
case '~' : |
|
|
|
|
|
|
95
|
|
|
if ($preprocess === false) break; |
|
96
|
|
|
$replacement = Yii::$app->cache->get($cacheKey); |
|
97
|
|
|
if ($replacement === false) { |
|
98
|
|
|
$replacement = static::renderUrl($chunkData); |
|
|
|
|
|
|
99
|
|
|
Yii::$app->cache->set( |
|
100
|
|
|
$content_key, |
|
101
|
|
|
$replacement, |
|
102
|
|
|
84600, |
|
103
|
|
|
$dependency |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
$content = str_replace($matches[0][$k], $replacement, $content); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
return $content; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $chunkData |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
private static function replaceForms($chunkData) |
|
119
|
|
|
{ |
|
120
|
|
|
$regexp = '/^(?P<formId>\d+)(#(?P<id>[\w\d\-_]+))?(;(?P<isModal>isModal))?$/Usi'; |
|
121
|
|
|
return preg_replace_callback( |
|
122
|
|
|
$regexp, |
|
123
|
|
|
function ($matches) { |
|
124
|
|
|
if (isset($matches['formId'])) { |
|
125
|
|
|
$params = ['formId' => intval($matches['formId'])]; |
|
126
|
|
|
if (isset($matches['id'])) { |
|
127
|
|
|
$params['id'] = $matches['id']; |
|
128
|
|
|
} |
|
129
|
|
|
if (isset($matches['isModal'])) { |
|
130
|
|
|
$params['isModal'] = true; |
|
131
|
|
|
} |
|
132
|
|
|
return app\widgets\form\Form::widget($params); |
|
133
|
|
|
} |
|
134
|
|
|
return ''; |
|
135
|
|
|
}, |
|
136
|
|
|
$chunkData['key'] |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* renders url according to given data |
|
142
|
|
|
* @param $chunkData |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
private static function renderUrl($chunkData) |
|
146
|
|
|
{ |
|
147
|
|
|
$expression = '%(?P<objectName>[^#]+?)#(?P<objectId>[\d]+?)$%'; |
|
148
|
|
|
$output = ''; |
|
149
|
|
|
preg_match($expression, $chunkData['key'], $m); |
|
150
|
|
|
if (true === isset($m['objectName'], $m['objectId'])) { |
|
151
|
|
|
$id = (int)$m['objectId']; |
|
152
|
|
|
switch (strtolower($m['objectName'])) { |
|
153
|
|
View Code Duplication |
case "page" : |
|
|
|
|
|
|
154
|
|
|
if (null !== $model = app\modules\page\models\Page::findById($id)) { |
|
155
|
|
|
$output = Url::to(['@article', 'id' => $id]); |
|
156
|
|
|
} |
|
157
|
|
|
break; |
|
158
|
|
|
case "category" : |
|
|
|
|
|
|
159
|
|
|
if (null !== $model = app\modules\shop\models\Category::findById($id)) { |
|
160
|
|
|
$output = Url::to(['@category', 'last_category_id' => $id]); |
|
161
|
|
|
} |
|
162
|
|
|
break; |
|
163
|
|
View Code Duplication |
case "product" : |
|
|
|
|
|
|
164
|
|
|
if (null !== $model = app\modules\shop\models\Product::findById($id)) { |
|
165
|
|
|
$output = Url::to(['@product', 'model' => $model]); |
|
166
|
|
|
} |
|
167
|
|
|
break; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
return $output; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Extracts chunk data from chunk call |
|
175
|
|
|
* uses regexp to extract param data from placeholder |
|
176
|
|
|
* [[$chunk <paramName>='<escapedValue>'|'<escapedDefault>' <paramName>=<unescapedValue>|<unescapedDefault>]] |
|
177
|
|
|
* iterate matches. |
|
178
|
|
|
* While iterating converts escapedValue and escapedDefault into string, unescapedValue and unescapedDefault - into float |
|
|
|
|
|
|
179
|
|
|
* Returns chunk data array like: |
|
180
|
|
|
* [ |
|
181
|
|
|
* 'key' => 'chunkKey', |
|
182
|
|
|
* 'firstParam'=> 'string value', |
|
183
|
|
|
* 'firstParam-default'=> 'default string value', |
|
184
|
|
|
* 'secondParam'=> float value, |
|
185
|
|
|
* 'secondParam-default'=> default float value, |
|
186
|
|
|
* ] |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $rawChunk |
|
189
|
|
|
* @return array |
|
|
|
|
|
|
190
|
|
|
*/ |
|
191
|
|
|
private static function sanitizeChunk($rawChunk) |
|
192
|
|
|
{ |
|
193
|
|
|
$chunk = []; |
|
194
|
|
|
preg_match('%(?P<chunkToken>[^\w\[]?)([^\s\]\[]+)[\s\]]%', $rawChunk, $keyMatches); |
|
195
|
|
|
$chunk['token'] = $keyMatches['chunkToken']; |
|
196
|
|
|
$chunk['key'] = $keyMatches[2]; |
|
197
|
|
|
$expression = "#\s*(?P<paramName>[\\w\\d]*)=(('(?P<escapedValue>.*[^\\\\])')|(?P<unescapedValue>.*))(\\|(('(?P<escapedDefault>.*[^\\\\])')|(?P<unescapedDefault>.*)))?[\\]\\s]#uUi"; |
|
|
|
|
|
|
198
|
|
|
preg_match_all($expression, $rawChunk, $matches); |
|
199
|
|
|
foreach ($matches['paramName'] as $key => $paramName) { |
|
200
|
|
|
if (!empty($matches['escapedValue'][$key])) { |
|
201
|
|
|
$chunk[$paramName] = strval($matches['escapedValue'][$key]); |
|
202
|
|
|
} |
|
203
|
|
|
if (!empty($matches['unescapedValue'][$key])) { |
|
204
|
|
|
$chunk[$paramName] = floatval($matches['unescapedValue'][$key]); |
|
205
|
|
|
} |
|
206
|
|
View Code Duplication |
if (!empty($matches['escapedDefault'][$key])) { |
|
207
|
|
|
$chunk[$paramName . '-default'] = strval($matches['escapedDefault'][$key]); |
|
208
|
|
|
} |
|
209
|
|
View Code Duplication |
if (!empty($matches['unescapedDefault'][$key])) { |
|
210
|
|
|
$chunk[$paramName . '-default'] = floatval($matches['unescapedDefault'][$key]); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
return $chunk; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Compiles single chunk |
|
218
|
|
|
* uses regexp to find placeholders and extract it's data from chunk value field |
|
219
|
|
|
* [[<token><paramName>:<format><params>]] |
|
220
|
|
|
* token switch is for future functionality increase |
|
221
|
|
|
* now method only recognizes + token and replaces following param with according $arguments array data |
|
222
|
|
|
* applies formatter according previously defined param values type if needed |
|
223
|
|
|
* if param name from placeholder was not found in arguments array, placeholder in the compiled chunk will be replaced with empty string |
|
|
|
|
|
|
224
|
|
|
* returns compiled chunk |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $content_key Key for caching compiled content version |
|
227
|
|
|
* @param yii\caching\Dependency $dependency Cache dependency |
|
228
|
|
|
* @param string $chunk ContentBlock instance |
|
229
|
|
|
* @param array $arguments Arguments for this chunk from original content |
|
230
|
|
|
* @param string $key ContentBlock key string to prevent endless recursion |
|
231
|
|
|
* @return string Result string ready for replacing |
|
232
|
|
|
*/ |
|
233
|
|
|
private static function compileChunk($chunk, $arguments, $key, $content_key, $dependency) |
|
234
|
|
|
{ |
|
235
|
|
|
$matches = []; |
|
236
|
|
|
preg_match_all('%\[\[(?P<token>[\+\*])(?P<paramName>[^\s\:\]]+)\:?(?P<format>[^\,\]]+)?\,?(?P<params>[^\]]+)?\]\]%ui', $chunk, $matches); |
|
|
|
|
|
|
237
|
|
|
foreach ($matches[0] as $k => $rawParam) { |
|
238
|
|
|
$token = $matches['token'][$k]; |
|
239
|
|
|
$paramName = trim($matches['paramName'][$k]); |
|
240
|
|
|
$format = trim($matches['format'][$k]); |
|
241
|
|
|
$params = preg_replace('%[\s]%', '', $matches['params'][$k]); |
|
242
|
|
|
$params = explode(',', $params); |
|
243
|
|
|
switch ($token) { |
|
244
|
|
|
case '+': |
|
245
|
|
|
if (array_key_exists($paramName, $arguments)) { |
|
246
|
|
|
$replacement = static::applyFormatter($arguments[$paramName], $format, $params); |
|
|
|
|
|
|
247
|
|
|
$chunk = str_replace($matches[0][$k], $replacement, $chunk); |
|
248
|
|
|
} else if (array_key_exists($paramName . '-default', $arguments)) { |
|
249
|
|
|
$replacement = static::applyFormatter($arguments[$paramName . '-default'], $format, $params); |
|
|
|
|
|
|
250
|
|
|
$chunk = str_replace($matches[0][$k], $replacement, $chunk); |
|
251
|
|
|
} else { |
|
252
|
|
|
$chunk = str_replace($matches[0][$k], '', $chunk); |
|
253
|
|
|
} |
|
254
|
|
|
break; |
|
255
|
|
|
default: |
|
256
|
|
|
$chunk = str_replace($matches[0][$k], '', $chunk); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
return self::processData($chunk, $content_key, $dependency, $key); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Find formatter declarations in chunk placeholders. if find trying to apply |
|
264
|
|
|
* yii\i18n\Formatter formats see yii\i18n\Formatter for details |
|
265
|
|
|
* |
|
266
|
|
|
* @param string $value single placeholder declaration from chunk |
|
267
|
|
|
* @param string $format |
|
268
|
|
|
* @param array $params |
|
269
|
|
|
* @return string|array |
|
270
|
|
|
*/ |
|
271
|
|
|
private static function applyFormatter($value, $format, $params) |
|
272
|
|
|
{ |
|
273
|
|
|
if (false === method_exists(Yii::$app->formatter, $format) || empty($format)) { |
|
274
|
|
|
return $value; |
|
275
|
|
|
} |
|
276
|
|
|
array_unshift($params, $value); |
|
277
|
|
|
try { |
|
278
|
|
|
$formattedValue = call_user_func_array([Yii::$app->formatter, $format], $params); |
|
279
|
|
|
} catch (\Exception $e) { |
|
280
|
|
|
$formattedValue = $value; |
|
281
|
|
|
} |
|
282
|
|
|
return $formattedValue; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Fetches single chunk by key from static var |
|
287
|
|
|
* if is no there - get it from db and push to static array |
|
288
|
|
|
* |
|
289
|
|
|
* @param $key string Chunk key field |
|
290
|
|
|
* @return string Chunk value field |
|
291
|
|
|
*/ |
|
292
|
|
|
private static function fetchChunkByKey($key) |
|
293
|
|
|
{ |
|
294
|
|
|
if (!array_key_exists($key, static::$chunksByKey)) { |
|
295
|
|
|
$dependency = new TagDependency([ |
|
296
|
|
|
'tags' => [ |
|
297
|
|
|
ActiveRecordHelper::getCommonTag(ContentBlock::className()), |
|
298
|
|
|
] |
|
299
|
|
|
]); |
|
300
|
|
|
static::$chunksByKey[$key] = ContentBlock::getDb()->cache(function ($db) use ($key) { |
|
|
|
|
|
|
301
|
|
|
$chunk = ContentBlock::find() |
|
302
|
|
|
->where(['key' => $key]) |
|
303
|
|
|
->asArray() |
|
304
|
|
|
->one(); |
|
305
|
|
|
return static::$chunksByKey[$key] = $chunk['value']; |
|
306
|
|
|
}, 86400, $dependency); |
|
307
|
|
|
} |
|
308
|
|
|
return static::$chunksByKey[$key]; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* preloads chunks with option preload = 1 |
|
313
|
|
|
* and push it to static array |
|
314
|
|
|
* |
|
315
|
|
|
* @return array|void |
|
316
|
|
|
*/ |
|
317
|
|
|
private static function preloadChunks() |
|
318
|
|
|
{ |
|
319
|
|
|
if (is_null(static::$chunksByKey)) { |
|
320
|
|
|
$dependency = new TagDependency([ |
|
321
|
|
|
'tags' => [ |
|
322
|
|
|
ActiveRecordHelper::getCommonTag(ContentBlock::className()), |
|
323
|
|
|
] |
|
324
|
|
|
]); |
|
325
|
|
|
static::$chunksByKey = ContentBlock::getDb()->cache(function ($db) { |
|
|
|
|
|
|
326
|
|
|
$chunks = ContentBlock::find() |
|
327
|
|
|
->where(['preload' => 1]) |
|
328
|
|
|
->asArray() |
|
329
|
|
|
->all(); |
|
330
|
|
|
return ArrayHelper::map($chunks, 'key', 'value'); |
|
331
|
|
|
}, 86400, $dependency); |
|
332
|
|
|
} |
|
333
|
|
|
return static::$chunksByKey; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* renders chunks in template files |
|
338
|
|
|
* @param string $key ContentBlock key |
|
339
|
|
|
* @param array $params . Array of params to be replaced while render |
|
340
|
|
|
* @param yii\base\Model $model . Caller model instance to use in caching |
|
|
|
|
|
|
341
|
|
|
* @return mixed |
|
|
|
|
|
|
342
|
|
|
*/ |
|
343
|
|
|
public static function getChunk($key, $params = [], yii\base\Model $model = null) |
|
344
|
|
|
{ |
|
345
|
|
|
if (null === $rawChunk = self::fetchChunkByKey($key)) { |
|
346
|
|
|
return ''; |
|
347
|
|
|
} |
|
348
|
|
|
$tags = [ |
|
349
|
|
|
ActiveRecordHelper::getCommonTag(app\modules\core\models\ContentBlock::className()), |
|
350
|
|
|
]; |
|
351
|
|
|
$content_key = 'templateChunkRender' . $key; |
|
352
|
|
|
if (null !== $model) { |
|
353
|
|
|
$content_key .= $model->id; |
|
354
|
|
|
$tags[] = ActiveRecordHelper::getObjectTag(get_class($model), $model->id); |
|
355
|
|
|
} |
|
356
|
|
|
$dependency = new TagDependency(['tags' => $tags]); |
|
357
|
|
|
if (false === empty($params)) { |
|
358
|
|
|
$rawChunk = self::compileChunk($rawChunk, $params, $key, $content_key, $dependency); |
|
359
|
|
|
} |
|
360
|
|
|
return self::compileContentString($rawChunk, $content_key, $dependency); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.