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
|
2 |
|
*/ |
24
|
|
|
public function getTagDependencyCacheComponent() |
25
|
2 |
|
{ |
26
|
|
|
return Yii::$app->cache; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns common tag name for model instance |
31
|
|
|
* @return string tag name |
32
|
2 |
|
*/ |
33
|
|
|
public static function commonTag() |
34
|
|
|
{ |
35
|
2 |
|
/** @var \yii\db\ActiveRecord $this */ |
36
|
|
|
return NamingHelper::getCommonTag(static::className()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns object tag name including it's id |
41
|
|
|
* @return string tag name |
42
|
2 |
|
*/ |
43
|
|
|
public function objectTag($oldfields) |
44
|
|
|
{ |
45
|
2 |
|
/** @var \yii\db\ActiveRecord $this */ |
46
|
|
|
$primary_key; |
|
|
|
|
47
|
|
|
if (count($this->primaryKey()) == 1) |
48
|
|
|
{ |
49
|
|
|
$key = $this->primaryKey()[0]; |
50
|
|
|
$primary_key = isset($oldfields[$key]) ? $oldfields[$key] : $this->$key; |
51
|
|
|
} else { |
52
|
1 |
|
$primary_key = []; |
53
|
|
|
foreach ($this->primaryKey() as $key) |
54
|
|
|
{ |
55
|
1 |
|
$primary_key[$key] = isset($oldfields[$key]) ? $oldfields[$key] : $this->$key; |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
1 |
|
return NamingHelper::getObjectTag($this->className(), $primary_key); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
/** |
62
|
1 |
|
* Returns composite tags name including fields |
63
|
|
|
* @return array tag names |
64
|
1 |
|
*/ |
65
|
1 |
|
public function objectCompositeTag($oldfields) |
66
|
|
|
{ |
67
|
1 |
|
/** @var \yii\db\ActiveRecord|TagDependencyTrait $this */ |
68
|
1 |
|
$cacheFields = $this->cacheCompositeTagFields(); |
69
|
1 |
|
|
70
|
|
|
if(empty($cacheFields)) { |
71
|
1 |
|
return []; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
$tags = []; |
75
|
|
|
|
76
|
|
|
foreach ($cacheFields as $tagFields) { |
77
|
|
|
$tag = []; |
78
|
|
|
$changed = false; |
79
|
|
|
|
80
|
|
View Code Duplication |
foreach ($tagFields as $tagField) { |
|
|
|
|
81
|
|
|
$tag[$tagField] = $this->$tagField; |
82
|
|
|
$changed |= isset($oldfields[$tagField]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$tags[] = NamingHelper::getCompositeTag($this->className(), $tag); |
86
|
2 |
|
|
87
|
|
|
if ($changed) { |
88
|
2 |
|
$tag = []; |
89
|
|
View Code Duplication |
foreach ($tagFields as $tagField) { |
|
|
|
|
90
|
|
|
$tag[$tagField] = isset($oldfields[$tagField]) ? $oldfields[$tagField] : $this->$tagField; |
91
|
|
|
} |
92
|
|
|
$tags[] = NamingHelper::getCompositeTag($this->className(), $tag); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $tags; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Specific fields from model for build composite tags for invalidate |
101
|
|
|
* Example: |
102
|
1 |
|
* return [ |
103
|
|
|
* ['field1', 'field2'], |
104
|
|
|
* ['field1', 'field2', 'field3'], |
105
|
|
|
* ]; |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
protected function cacheCompositeTagFields() |
109
|
|
|
{ |
110
|
|
|
return []; |
111
|
1 |
|
} |
112
|
1 |
|
|
113
|
1 |
|
/** |
114
|
1 |
|
* Finds or creates new model using or not using cache(objectTag is applied) |
115
|
1 |
|
* @param string|int $id ID of model to find |
116
|
1 |
|
* @param bool $createIfEmptyId Create new model instance(record) if id is empty |
117
|
1 |
|
* @param bool $useCache Use cache |
118
|
|
|
* @param int $cacheLifetime Cache lifetime in seconds |
119
|
1 |
|
* @param bool|\Exception $throwException False or exception instance to throw if model not found or (empty id AND createIfEmptyId==false) |
|
|
|
|
120
|
|
|
* @param bool $useIdentityMap True if we want to use identity map |
121
|
|
|
* @return \yii\db\ActiveRecord|null|self|TagDependencyTrait |
122
|
1 |
|
* @throws \Exception |
123
|
|
|
*/ |
124
|
|
|
public static function loadModel( |
125
|
|
|
$id, |
126
|
|
|
$createIfEmptyId = false, |
127
|
|
|
$useCache = true, |
128
|
1 |
|
$cacheLifetime = 86400, |
129
|
1 |
|
$throwException = false, |
130
|
1 |
|
$useIdentityMap = false |
131
|
1 |
|
) { |
132
|
1 |
|
/** @var \yii\db\ActiveRecord|TagDependencyTrait $model */ |
133
|
|
|
$model = null; |
134
|
1 |
|
if (empty($id)) { |
135
|
1 |
|
if ($createIfEmptyId === true) { |
136
|
|
|
$model = new static; |
137
|
|
|
} else { |
138
|
1 |
|
if ($throwException !== false) { |
139
|
1 |
|
throw $throwException; |
140
|
1 |
|
} else { |
141
|
1 |
|
return null; |
142
|
1 |
|
} |
143
|
1 |
|
} |
144
|
1 |
|
} elseif ($useIdentityMap === true) { |
145
|
1 |
|
if (isset(static::$identityMap[$id])) { |
146
|
1 |
|
return static::$identityMap[$id]; |
147
|
1 |
|
} |
148
|
1 |
|
} |
149
|
1 |
|
|
150
|
1 |
|
if ($useCache === true && $model===null) { |
151
|
1 |
|
$model = Yii::$app->cache->get(static::className() . ":" . $id); |
152
|
1 |
|
} |
153
|
|
|
if (!is_object($model)) { |
154
|
1 |
|
$model = static::findOne($id); |
155
|
|
|
|
156
|
|
|
if ($model !== null) { |
157
|
1 |
|
if ($useIdentityMap === true) { |
158
|
|
|
static::$identityMap[$model->id] = &$model; |
159
|
|
|
} |
160
|
|
|
if ($useCache === true) { |
161
|
|
|
Yii::$app->cache->set( |
162
|
|
|
static::className() . ":" . $id, |
163
|
|
|
$model, |
164
|
2 |
|
$cacheLifetime, |
165
|
|
|
new TagDependency([ |
166
|
|
|
'tags' => $model->objectTag(), |
167
|
|
|
]) |
168
|
2 |
|
); |
169
|
2 |
|
} |
170
|
|
|
} |
171
|
2 |
|
} |
172
|
2 |
|
if (!is_object($model)) { |
173
|
2 |
|
if ($throwException) { |
174
|
2 |
|
throw $throwException; |
175
|
|
|
} else { |
176
|
2 |
|
return null; |
177
|
1 |
|
} |
178
|
1 |
|
} |
179
|
1 |
|
return $model; |
180
|
1 |
|
} |
181
|
1 |
|
|
182
|
|
|
/** |
183
|
2 |
|
* Invalidate model tags. |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function invalidateTags($event = null) |
187
|
|
|
{ |
188
|
|
|
/** @var TagDependencyTrait $this */ |
189
|
|
|
\yii\caching\TagDependency::invalidate( |
190
|
|
|
$this->getTagDependencyCacheComponent(), |
191
|
|
|
[ |
192
|
|
|
static::commonTag(), |
193
|
|
|
$this->objectTag($event != null && $event->name == ActiveRecord::EVENT_AFTER_UPDATE ? $event->changedAttributes : []) |
|
|
|
|
194
|
|
|
] |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
if (!empty($this->cacheCompositeTagFields())) { |
198
|
|
|
\yii\caching\TagDependency::invalidate( |
199
|
|
|
$this->getTagDependencyCacheComponent(), |
200
|
|
|
$this->objectCompositeTag($event != null && $event->name == ActiveRecord::EVENT_AFTER_UPDATE ? $event->changedAttributes : []) |
|
|
|
|
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return true; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
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.