1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DevGroup\TagDependencyHelper; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\caching\TagDependency; |
7
|
|
|
use yii\db\ActiveRecord; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* TagDependencyTrait features: |
11
|
|
|
* - retrieving common and object tags |
12
|
|
|
* - configuring cache component(through overriding getTagDependencyCacheComponent) |
13
|
|
|
* - configuring composite tags(through overriding cacheCompositeTagFields) |
14
|
|
|
* - Identity Map pattern support |
15
|
|
|
*/ |
16
|
|
|
trait TagDependencyTrait |
17
|
|
|
{ |
18
|
|
|
/** @var array IdentityMap pattern support */ |
19
|
|
|
public static $identityMap = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return \yii\caching\Cache |
23
|
|
|
*/ |
24
|
1 |
|
public function getTagDependencyCacheComponent() |
25
|
|
|
{ |
26
|
1 |
|
return Yii::$app->cache; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns common tag name for model instance |
31
|
|
|
* @return string tag name |
32
|
|
|
*/ |
33
|
1 |
|
public static function commonTag() |
34
|
|
|
{ |
35
|
|
|
/** @var \yii\db\ActiveRecord $this */ |
36
|
1 |
|
return NamingHelper::getCommonTag(static::className()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns object tag name including it's id |
41
|
|
|
* @param array Changed fields from Update Event |
42
|
|
|
* @return string tag name |
43
|
|
|
*/ |
44
|
1 |
|
public function objectTag($oldfields = []) |
45
|
|
|
{ |
46
|
|
|
/** @var \yii\db\ActiveRecord $this */ |
47
|
|
|
$primary_key; |
|
|
|
|
48
|
1 |
|
if (count($this->primaryKey()) == 1) |
49
|
1 |
|
{ |
50
|
1 |
|
$key = $this->primaryKey()[0]; |
51
|
1 |
|
$primary_key = isset($oldfields[$key]) ? $oldfields[$key] : $this->$key; |
52
|
1 |
|
} else { |
53
|
|
|
$primary_key = []; |
54
|
|
|
foreach ($this->primaryKey() as $key) |
55
|
|
|
{ |
56
|
|
|
$primary_key[$key] = isset($oldfields[$key]) ? $oldfields[$key] : $this->$key; |
57
|
|
|
} |
58
|
|
|
} |
59
|
1 |
|
return NamingHelper::getObjectTag($this->className(), $primary_key); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns composite tags name including fields |
64
|
|
|
* @param array Changed fields from Update Event |
65
|
|
|
* @return array tag names |
66
|
|
|
*/ |
67
|
1 |
|
public function objectCompositeTag($oldfields = []) |
68
|
|
|
{ |
69
|
|
|
/** @var \yii\db\ActiveRecord|TagDependencyTrait $this */ |
70
|
1 |
|
$cacheFields = $this->cacheCompositeTagFields(); |
71
|
|
|
|
72
|
1 |
|
if(empty($cacheFields)) { |
73
|
1 |
|
return []; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$tags = []; |
77
|
|
|
|
78
|
1 |
|
foreach ($cacheFields as $tagFields) { |
79
|
1 |
|
$tag = []; |
80
|
1 |
|
$changed = false; |
81
|
|
|
|
82
|
1 |
View Code Duplication |
foreach ($tagFields as $tagField) { |
|
|
|
|
83
|
|
|
$tag[$tagField] = $this->$tagField; |
84
|
|
|
$changed |= isset($oldfields[$tagField]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$tags[] = NamingHelper::getCompositeTag($this->className(), $tag); |
88
|
|
|
|
89
|
|
|
if ($changed) { |
90
|
|
|
$tag = []; |
91
|
|
View Code Duplication |
foreach ($tagFields as $tagField) { |
|
|
|
|
92
|
|
|
$tag[$tagField] = isset($oldfields[$tagField]) ? $oldfields[$tagField] : $this->$tagField; |
93
|
|
|
} |
94
|
|
|
$tags[] = NamingHelper::getCompositeTag($this->className(), $tag); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $tags; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Specific fields from model for build composite tags for invalidate |
103
|
|
|
* Example: |
104
|
|
|
* return [ |
105
|
|
|
* ['field1', 'field2'], |
106
|
|
|
* ['field1', 'field2', 'field3'], |
107
|
|
|
* ]; |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
2 |
|
protected function cacheCompositeTagFields() |
111
|
|
|
{ |
112
|
2 |
|
return []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Finds or creates new model using or not using cache(objectTag is applied) |
117
|
|
|
* @param string|int $id ID of model to find |
118
|
|
|
* @param bool $createIfEmptyId Create new model instance(record) if id is empty |
119
|
|
|
* @param bool $useCache Use cache |
120
|
|
|
* @param int $cacheLifetime Cache lifetime in seconds |
121
|
|
|
* @param bool|\Exception $throwException False or exception instance to throw if model not found or (empty id AND createIfEmptyId==false) |
|
|
|
|
122
|
|
|
* @param bool $useIdentityMap True if we want to use identity map |
123
|
|
|
* @return \yii\db\ActiveRecord|null|self|TagDependencyTrait |
124
|
|
|
* @throws \Exception |
125
|
|
|
*/ |
126
|
1 |
|
public static function loadModel( |
127
|
|
|
$id, |
128
|
|
|
$createIfEmptyId = false, |
129
|
|
|
$useCache = true, |
130
|
|
|
$cacheLifetime = 86400, |
131
|
|
|
$throwException = false, |
132
|
|
|
$useIdentityMap = false |
133
|
|
|
) { |
134
|
|
|
/** @var \yii\db\ActiveRecord|TagDependencyTrait $model */ |
135
|
1 |
|
$model = null; |
136
|
1 |
|
if (empty($id)) { |
137
|
1 |
|
if ($createIfEmptyId === true) { |
138
|
1 |
|
$model = new static; |
139
|
1 |
|
} else { |
140
|
1 |
|
if ($throwException !== false) { |
141
|
1 |
|
throw $throwException; |
142
|
|
|
} else { |
143
|
1 |
|
return null; |
144
|
|
|
} |
145
|
|
|
} |
146
|
1 |
|
} elseif ($useIdentityMap === true) { |
147
|
|
|
if (isset(static::$identityMap[$id])) { |
148
|
|
|
return static::$identityMap[$id]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
if ($useCache === true && $model===null) { |
153
|
1 |
|
$model = Yii::$app->cache->get(static::className() . ":" . $id); |
154
|
1 |
|
} |
155
|
1 |
|
if (!is_object($model)) { |
156
|
1 |
|
$model = static::findOne($id); |
157
|
|
|
|
158
|
1 |
|
if ($model !== null) { |
159
|
1 |
|
if ($useIdentityMap === true) { |
160
|
|
|
static::$identityMap[$model->id] = &$model; |
161
|
|
|
} |
162
|
1 |
|
if ($useCache === true) { |
163
|
1 |
|
Yii::$app->cache->set( |
164
|
1 |
|
static::className() . ":" . $id, |
165
|
1 |
|
$model, |
166
|
1 |
|
$cacheLifetime, |
167
|
1 |
|
new TagDependency([ |
168
|
1 |
|
'tags' => $model->objectTag(), |
169
|
1 |
|
]) |
170
|
1 |
|
); |
171
|
1 |
|
} |
172
|
1 |
|
} |
173
|
1 |
|
} |
174
|
1 |
|
if (!is_object($model)) { |
175
|
1 |
|
if ($throwException) { |
176
|
1 |
|
throw $throwException; |
177
|
|
|
} else { |
178
|
1 |
|
return null; |
179
|
|
|
} |
180
|
|
|
} |
181
|
1 |
|
return $model; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Invalidate model tags. |
186
|
|
|
* @param yii\db\AfterSaveEvent when called as an event handler. |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
1 |
|
public function invalidateTags($event = null) |
190
|
|
|
{ |
191
|
|
|
/** @var TagDependencyTrait $this */ |
192
|
1 |
|
\yii\caching\TagDependency::invalidate( |
193
|
1 |
|
$this->getTagDependencyCacheComponent(), |
194
|
|
|
[ |
195
|
1 |
|
static::commonTag(), |
196
|
1 |
|
$this->objectTag($event != null && $event->name == ActiveRecord::EVENT_AFTER_UPDATE ? $event->changedAttributes : []) |
|
|
|
|
197
|
1 |
|
] |
198
|
1 |
|
); |
199
|
|
|
|
200
|
1 |
|
if (!empty($this->cacheCompositeTagFields())) { |
201
|
|
|
\yii\caching\TagDependency::invalidate( |
202
|
|
|
$this->getTagDependencyCacheComponent(), |
203
|
|
|
$this->objectCompositeTag($event != null && $event->name == ActiveRecord::EVENT_AFTER_UPDATE ? $event->changedAttributes : []) |
|
|
|
|
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
return true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.