1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\Model; |
15
|
|
|
use Phalcon\Mvc\ModelInterface; |
16
|
|
|
use Zemit\Config\ConfigInterface; |
17
|
|
|
use Zemit\Models\Audit; |
18
|
|
|
use Zemit\Models\AuditDetail; |
19
|
|
|
use Zemit\Models\Session; |
20
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior; |
21
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractModelsCache; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Flush Cache on changes |
25
|
|
|
* |
26
|
|
|
* @todo set cache keys |
27
|
|
|
* @todo improve to delete only necessary keys |
28
|
|
|
* @todo improve whiteList system |
29
|
|
|
* @todo precache system |
30
|
|
|
*/ |
31
|
|
|
trait Cache |
32
|
|
|
{ |
33
|
|
|
use AbstractModelsCache; |
34
|
|
|
use AbstractBehavior; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set true to avoid flushing cache for the current instance |
38
|
|
|
*/ |
39
|
|
|
public bool $preventFlushCache = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Whitelisted classes to not force global cache flush on change |
43
|
|
|
*/ |
44
|
|
|
public array $flushModelsCacheBlackList = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initializing Cache |
48
|
|
|
*/ |
49
|
2 |
|
public function initializeCache(): void |
50
|
|
|
{ |
51
|
2 |
|
$config = $this->getDI()->get('config'); |
52
|
2 |
|
assert($config instanceof ConfigInterface); |
53
|
|
|
|
54
|
2 |
|
$this->flushModelsCacheBlackList [] = $config->getModelClass(Session::class); |
55
|
2 |
|
$this->flushModelsCacheBlackList [] = $config->getModelClass(Audit::class); |
56
|
2 |
|
$this->flushModelsCacheBlackList [] = $config->getModelClass(AuditDetail::class); |
57
|
|
|
|
58
|
2 |
|
$this->addFlushCacheBehavior($this->flushModelsCacheBlackList); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adding Cache Behavior |
63
|
|
|
*/ |
64
|
2 |
|
public function addFlushCacheBehavior(?array $flushModelsCacheBlackList = null): void |
65
|
|
|
{ |
66
|
2 |
|
$flushModelsCacheBlackList ??= $this->flushModelsCacheBlackList; |
67
|
|
|
|
68
|
|
|
// flush cache prevented by current instance |
69
|
2 |
|
if ($this->preventFlushCache) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// flush cache prevented if current instance class is blacklisted |
74
|
2 |
|
if ($this->isInstanceOf($flushModelsCacheBlackList)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
$modelsCache = $this->getModelsCache(); |
79
|
2 |
|
$flushAction = function (Model $model) use ($modelsCache) { |
80
|
|
|
// Do not flush cache if nothing has changed |
81
|
|
|
return ($model->hasSnapshotData() && !($model->hasUpdated() || $model->hasChanged())) |
82
|
|
|
&& $modelsCache->clear(); |
83
|
2 |
|
}; |
84
|
|
|
|
85
|
2 |
|
$actions = ['flush' => $flushAction]; |
86
|
2 |
|
$this->addBehavior(new Behavior\Action([ |
87
|
2 |
|
'afterSave' => $actions, |
88
|
2 |
|
'afterCreate' => $actions, |
89
|
2 |
|
'afterUpdate' => $actions, |
90
|
2 |
|
'afterDelete' => $actions, |
91
|
2 |
|
'afterRestore' => $actions, |
92
|
2 |
|
'afterReorder' => $actions, |
93
|
2 |
|
])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check whether the current instance is any of the classes |
98
|
|
|
*/ |
99
|
2 |
|
public function isInstanceOf(array $classes = [], ?ModelInterface $that = null): bool |
100
|
|
|
{ |
101
|
2 |
|
$that ??= $this; |
102
|
|
|
|
103
|
|
|
// Prevent adding behavior to whiteListed models |
104
|
2 |
|
foreach ($classes as $class) { |
105
|
2 |
|
if ($that instanceof $class) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|