|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\components; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Component; |
|
7
|
|
|
|
|
8
|
|
|
class ViewElementsGathener extends Component |
|
9
|
|
|
{ |
|
10
|
|
|
public $elements = []; |
|
11
|
|
|
public $cacheStack = [null,]; |
|
12
|
|
|
public $cacheStackDependencies = [null,]; |
|
13
|
|
|
public $currentStackId = null; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string|\yii\caching\Cache */ |
|
16
|
|
|
public $cache = 'cache'; |
|
17
|
|
|
|
|
18
|
|
|
public $cacheLifetime = 86400; |
|
19
|
|
|
|
|
20
|
|
|
public function init() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::init(); |
|
23
|
|
|
if (is_string($this->cache)) { |
|
24
|
|
|
$this->cache = Yii::$app->get($this->cache); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function gatherToStack($cacheStackId, $elementType, $arguments) |
|
29
|
|
|
{ |
|
30
|
|
|
if (isset($this->elements[$cacheStackId]) === false) { |
|
31
|
|
|
$this->elements[$cacheStackId] = []; |
|
32
|
|
|
} |
|
33
|
|
|
if (isset($this->elements[$cacheStackId][$elementType]) === false) { |
|
34
|
|
|
$this->elements[$cacheStackId][$elementType] = []; |
|
35
|
|
|
} |
|
36
|
|
|
$this->elements[$cacheStackId][$elementType][] = $arguments; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function gather($elementType, $arguments) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->currentStackId===null) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
Yii::trace("Gather: ".$this->currentStackId." -- ".$elementType); |
|
|
|
|
|
|
45
|
|
|
$this->gatherToStack($this->currentStackId, $elementType, $arguments); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function startGathering($cacheStackId, $dependency = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->currentStackId = $cacheStackId; |
|
51
|
|
|
$this->elements[$cacheStackId] = []; |
|
52
|
|
|
$this->cacheStack[] = $cacheStackId; |
|
53
|
|
|
$this->cacheStackDependencies[] = $dependency; |
|
54
|
|
|
Yii::trace('Start gathering:' . $cacheStackId); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function endGathering() |
|
58
|
|
|
{ |
|
59
|
|
|
array_pop($this->cacheStack); |
|
60
|
|
|
$elements = $this->elements[$this->currentStackId]; |
|
61
|
|
|
Yii::trace('End gathering:' . $this->currentStackId); |
|
|
|
|
|
|
62
|
|
|
$dependencies = array_pop($this->cacheStackDependencies); |
|
63
|
|
|
|
|
64
|
|
|
$this->cache->set( |
|
65
|
|
|
$this->getCacheKey(), |
|
66
|
|
|
$elements, |
|
67
|
|
|
$this->cacheLifetime, |
|
68
|
|
|
$dependencies |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
unset($this->elements[$this->currentStackId]); |
|
72
|
|
|
|
|
73
|
|
|
$stack = $this->cacheStack; |
|
74
|
|
|
$this->currentStackId = end($stack); |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
return $elements; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
public function repeatGatheredData($view, $cachedData = []) |
|
82
|
|
|
{ |
|
83
|
|
|
Yii::trace('Repeat gathered data!'); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
foreach ($cachedData as $function => $cached) { |
|
86
|
|
|
foreach ($cached as $arguments) { |
|
87
|
|
|
call_user_func_array([$view, $function], $arguments); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getCachedData($cacheStackId) |
|
93
|
|
|
{ |
|
94
|
|
|
Yii::trace('Get cached data: ' . $cacheStackId); |
|
|
|
|
|
|
95
|
|
|
$data = Yii::$app->cache->get('ViewElementsGathener:'.$cacheStackId); |
|
96
|
|
|
|
|
97
|
|
|
return $data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function getCacheKey() |
|
101
|
|
|
{ |
|
102
|
|
|
return 'ViewElementsGathener:'.$this->currentStackId; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.