-
Notifications
You must be signed in to change notification settings - Fork 4
/
OnceWidget.php
56 lines (45 loc) · 1.16 KB
/
OnceWidget.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
<?php
/**
* Created by PhpStorm.
* User: execut
* Date: 9/26/17
* Time: 3:59 PM
*/
namespace execut\yii;
use yii\base\Exception;
use yii\base\Widget;
class OnceWidget extends Widget
{
public $widget = [];
protected static $alreadyRenderedWidgets = [];
public function run()
{
$widgets = $this->widget;
if (!is_array($widgets)) {
$class = $widgets;
$widgets = [
[
'class' => $class,
],
];
} else if (isset($widgets['class'])) {
$widgets = [$widgets];
}
$result = '';
foreach ($widgets as $widget) {
if (!isset($widget['class'])) {
throw new Exception('Widget class required in widget config');
}
$class = $widget['class'];
if (isset(self::$alreadyRenderedWidgets[$class])) {
return;
}
self::$alreadyRenderedWidgets[$class] = true;
$result .= $class::widget($widget);
}
return $result;
}
public static function reset() {
self::$alreadyRenderedWidgets = [];
}
}