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