-
Notifications
You must be signed in to change notification settings - Fork 2
/
InlineWidgetsBehavior.php
executable file
·252 lines (204 loc) · 5.77 KB
/
InlineWidgetsBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace sadovojav\iwb;
use Yii;
use yii\base\Behavior;
/**
* Class InlineWidgetsBehavior
* @package sadovojav\iwb
*/
class InlineWidgetsBehavior extends Behavior
{
/**
* @var string marker of block begin
*/
public $startBlock = '[*';
/**
* @var string marker of block end
*/
public $endBlock = '*]';
/**
* @var string of widgets like 'common\components\widgets'
*/
public $namespace = '';
/**
* @var string global classname suffix like 'Widget'
*/
public $classSuffix = '';
/**
* @var int default cache duration
*/
public $cacheDuration = 0;
/**
* @var array of allowed widgets
*/
public $widgets = [];
/**
* @var string widget token
*/
private $widgetToken;
public function init()
{
$this->initToken();
}
/**
* Content parser
* Use $this->view->decodeWidgets($model->text) in view
* @param $text
* @return mixed
*/
public function decodeWidgets($text)
{
$text = $this->clearAutoParagraphs($text);
$text = $this->replaceBlocks($text);
$text = $this->processWidgets($text);
return $text;
}
/**
* Content cleaner
* Use $this->view->clearWidgets($model->text) in view
* @param $text
* @return mixed
*/
public function clearWidgets($text)
{
$text = $this->clearAutoParagraphs($text);
$text = $this->replaceBlocks($text);
$text = $this->removeWidgets($text);
return $text;
}
/**
* Processing widget
* @param $text
* @return mixed
*/
private function processWidgets($text)
{
if (preg_match('|\{' . $this->widgetToken . ':.+?' . $this->widgetToken . '\}|is', $text)) {
foreach ($this->widgets as $alias) {
$widget = $this->getClassByAlias($alias);
while (preg_match('/\{' . $this->widgetToken . ':' . $widget . '(\|([^}]*)?)?' . $this->widgetToken . '\}/is', $text, $p)) {
$text = str_replace($p[0], $this->loadWidget($alias, isset($p[2]) ? $p[2] : ''), $text);
}
}
return $text;
}
return $text;
}
/**
* Remove widget from the content
* @param $text
* @return mixed
*/
private function removeWidgets($text)
{
return preg_replace('|\{' . $this->widgetToken . ':.+?' . $this->widgetToken . '\}|is', '', $text);
}
/**
* Token initialisation
*/
private function initToken()
{
$this->widgetToken = md5(microtime());
}
/**
* Replace blocks
* @param $text
* @return mixed
*/
private function replaceBlocks($text)
{
$text = str_replace($this->startBlock, '{' . $this->widgetToken . ':', $text);
$text = str_replace($this->endBlock, $this->widgetToken . '}', $text);
return $text;
}
/**
* Clear auto paragraphs
* @param $output
* @return mixed
*/
private function clearAutoParagraphs($output)
{
$output = str_replace('<p>' . $this->startBlock, $this->startBlock, $output);
$output = str_replace($this->endBlock . '</p>', $this->endBlock, $output);
return $output;
}
/**
* Load widget
* @param $name
* @param string $attributes
* @return mixed
* @throws \yii\base\InvalidConfigException
*/
private function loadWidget($name, $attributes = '')
{
$attrs = $this->parseAttributes($attributes);
$cache = $this->extractCacheDurationTime($attrs);
$index = 'widget_' . $name . '_' . serialize($attrs);
if ($cache && $cachedHtml = Yii::$app->cache->get($index)) {
$html = $cachedHtml;
} else {
$widgetClass = $this->getFullClassName($name);
$config['class'] = $widgetClass;
$config = array_merge($config, $attrs);
$html = Yii::createObject($config)->run();
Yii::$app->cache->set($index, $html, $cache);
}
return $html;
}
/**
* Parse attributes
* @param $attributesString
* @return array
*/
private function parseAttributes($attributesString)
{
$params = explode(';', $attributesString);
$attrs = [];
foreach ($params as $param) {
if ($param) {
list($attribute, $value) = explode('=', $param);
if ($value) {
$attrs[$attribute] = trim($value);
}
}
}
ksort($attrs);
return $attrs;
}
/**
* Extrat cache duration time
* @param $attrs
* @return int
*/
private function extractCacheDurationTime(&$attrs)
{
if (isset($attrs['cache'])) {
$this->cacheDuration = (int)$attrs['cache'];
unset($attrs['cache']);
}
return $this->cacheDuration;
}
/**
* Get full Class name
* @param $name
* @return string
*/
private function getFullClassName($name)
{
$widgetClass = $name . $this->classSuffix;
if ($this->getClassByAlias($widgetClass) == $widgetClass && $this->namespace) {
$widgetClass = $this->namespace . '\\' . $widgetClass;
}
return $widgetClass;
}
/**
* Get Class by alias
* @param $alias
* @return mixed
*/
private function getClassByAlias($alias)
{
$paths = explode('\\', $alias);
return array_pop($paths);
}
}