/
sfCachetaggableDoctrineRecord.class.php
291 lines (246 loc) · 7.11 KB
/
sfCachetaggableDoctrineRecord.class.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/*
* This file is part of the sfCacheTaggingPlugin package.
* (c) 2009-2013 Ilya Sabelnikov <fruit.dev@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package sfCacheTaggingPlugin
* @subpackage doctrine
* @author Ilya Sabelnikov <fruit.dev@gmail.com>
*/
abstract class sfCachetaggableDoctrineRecord extends sfDoctrineRecord
{
/**
* Passed through the save/replace/delete user current method
* connection instance.
*
* Used to detect transaction state while writing tags into cache backend.
*
* @var Doctrine_Connection
*/
protected $userConnection = null;
/**
* @var string $templateName Template (behavior) name
* @return Doctrine_Template
*/
protected function getTempleteWithInvoker ($templateName, $method)
{
$table = $this->getTable();
if ( ! $table->hasTemplate($templateName))
{
throw new InvalidArgumentException(
sprintf('Template "%s" is not registered', $templateName)
);
}
$template = $table->getTemplate($templateName);
$template->setInvoker($this);
$table->setMethodOwner($method, $template);
return $template;
}
/**
* @return Doctrine_Template_Cachetaggable
*/
protected function getCachetaggable ($method)
{
return $this->getTempleteWithInvoker(
sfCacheTaggingToolkit::TEMPLATE_NAME, $method
);
}
/**
* @return Doctrine_Record
*/
public function updateObjectVersion ()
{
return $this->getCachetaggable('updateObjectVersion')->updateObjectVersion();
}
/**
* @see Doctrine_Template_Cachetaggable::getCacheTags()
* @var boolean $deep (whether to fetch tags recursively from all joined tables)
* @return array Object tags
*/
public function getCacheTags ($deep = true)
{
return $this
->getCachetaggable('getCacheTags')
->getCacheTags($deep)
;
}
/**
* @see Doctrine_Template_Cachetaggable::obtainTagName()
* @return string
*/
public function obtainTagName ()
{
return $this->getCachetaggable('obtainTagName')->obtainTagName();
}
/**
* @see Doctrine_Template_Cachetaggable::obtainObjectVersion()
* @return string
*/
public function obtainObjectVersion ()
{
return $this->getCachetaggable('obtainObjectVersion')->obtainObjectVersion();
}
/**
* @see Doctrine_Template_Cachetaggable::getCollectionTags()
* @return array
*/
public function getCollectionTags ()
{
return $this->getCachetaggable('getCollectionTags')->getCollectionTags();
}
/**
* @see Doctrine_Template_Cachetaggable::obtainCollectionName()
* @return string
*/
public function obtainCollectionName ()
{
return $this->getCachetaggable('obtainCollectionName')->obtainCollectionName();
}
/**
* @see Doctrine_Template_Cachetaggable::obtainCollectionVersion()
* @return string
*/
public function obtainCollectionVersion ()
{
return $this->getCachetaggable('obtainCollectionVersion')->obtainCollectionVersion();
}
/**
* Generates tags for refTable
*
* @param string $alias
* @param array $ids
* @return array
*/
protected function getTagNamesByAlias ($alias, array $ids)
{
$relation = $this->getTable()->getRelation($alias);
if (! $relation instanceof Doctrine_Relation_Association)
{
return array();
}
/* @var $refTable Doctrine_Table */
$refTable = $relation->getAssociationTable();
if (! $refTable->hasTemplate(sfCacheTaggingToolkit::TEMPLATE_NAME))
{
return array();
}
$template = $refTable->getTemplate(sfCacheTaggingToolkit::TEMPLATE_NAME);
$values = array();
foreach ($ids as $id)
{
foreach ($refTable->getIdentifierColumnNames() as $columnName)
{
if ($relation->getLocal() == $columnName)
{
$values[$id][$columnName] = $this->getPrimaryKey();
}
else
{
$values[$id][$columnName] = $id;
}
}
}
$uniqueColumns = $template->getOptionUniqueColumns();
$keyFormat = $template->getOptionKeyFormat($uniqueColumns);
$tagNames = array();
foreach ($values as $columnValues)
{
$tagName = sfCacheTaggingToolkit::buildTagKey(
$template, $keyFormat, array_values($columnValues)
);
$tagNames[$tagName] = true;
}
return $tagNames;
}
/**
* After linking ID's, invalidates object tags
*
* {@inheritdoc}
*/
public function link ($alias, $ids, $now = false)
{
$self = parent::link($alias, $ids, $now);
$ids = (array) $ids;
if (! sfConfig::get('sf_cache') || ! count($ids)) return $self;
$taggingCache = sfCacheTaggingToolkit::getTaggingCache();
$taggingCache->invalidateTags($this->getTagNamesByAlias($alias, $ids));
return $self;
}
/**
* After unlinking ID's, invalidates object tags
*
* {@inheritdoc}
*/
public function unlink ($alias, $ids = array(), $now = false)
{
$self = parent::unlink($alias, $ids, $now);
$ids = (array) $ids;
if (! sfConfig::get('sf_cache') || ! count($ids)) return $self;
$taggingCache = sfCacheTaggingToolkit::getTaggingCache();
$taggingCache->deleteTags($this->getTagNamesByAlias($alias, $ids));
return $self;
}
/**
* Saves user custom connection instance, to correctly detect Doctrine
* transaction state in Doctrine_Template_Listener_Cachetaggable.
*
* @param Doctrine_Connection $conn
*/
protected function setUserConnection (Doctrine_Connection $conn = null)
{
$this->userConnection = $conn;
}
/**
* Returns real connection, when user executes one of the actions:
* - save
* - delete
* - replace
*
* Used in Doctrine_Template_Listener_Cachetaggable to have right connection
* for checking Doctrine transaction state.
*
* @return Doctrine_Connection
*/
public function getCurrentConnection ()
{
return $this->userConnection
? $this->userConnection
: $this->getTable()->getConnection();
}
/**
* {@inheritdoc}
*/
public function save (Doctrine_Connection $conn = null)
{
$this->setUserConnection($conn);
return parent::save($conn);
}
/**
* {@inheritdoc}
*/
public function delete (Doctrine_Connection $conn = null)
{
$this->setUserConnection($conn);
return parent::delete($conn);
}
/**
* {@inheritdoc}
*/
public function replace (Doctrine_Connection $conn = null)
{
$this->setUserConnection($conn);
return parent::replace($conn);
}
/**
* {@inheritdoc}
*/
public function free ($deep = false)
{
unset($this->userConnection);
return parent::free($deep);
}
}