1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\SimpleTemplateCaching\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\Core\Extension; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\Versioned\Versioned; |
9
|
|
|
use Sunnysideup\SimpleTemplateCaching\Model\ObjectsUpdated; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class \Sunnysideup\SimpleTemplateCaching\Extensions\DataObjectExtension. |
13
|
|
|
* |
14
|
|
|
* @property DataObject|DataObjectExtension $owner |
15
|
|
|
*/ |
16
|
|
|
class DataObjectExtension extends Extension |
17
|
|
|
{ |
18
|
|
|
public function onAfterWrite() |
19
|
|
|
{ |
20
|
|
|
$owner = $this->getOwner(); |
21
|
|
|
|
22
|
|
|
// NB. |
23
|
|
|
// if the dataobject has the versioned extension then the cache should be invalidated onAfterPublish |
24
|
|
|
// hasStages function is part of the Versioned class so safe to check here |
25
|
|
|
if (! $owner->hasExtension(Versioned::class)) { |
26
|
|
|
$this->doUpdateCache(); |
27
|
|
|
} elseif (! $owner->hasStages()) { |
28
|
|
|
$this->doUpdateCache(); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function onAfterDelete() |
33
|
|
|
{ |
34
|
|
|
$this->doUpdateCache(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
//* this function needs further consideration as it is called many times on the front end */ |
38
|
|
|
// public function updateManyManyComponents() |
39
|
|
|
// { |
40
|
|
|
// $owner = $this->owner; |
41
|
|
|
// $className = $owner->ClassName; |
42
|
|
|
|
43
|
|
|
// if(!$owner->hasExtension(Versioned::class)){ |
44
|
|
|
// $this->doUpdateCache($className); |
45
|
|
|
// } |
46
|
|
|
// //if the dataobject has the versioned extension then the cache should be invalidated onAfterPublish |
47
|
|
|
// else if (!$owner->hasStages()){ |
48
|
|
|
// $this->doUpdateCache($className); |
49
|
|
|
// } |
50
|
|
|
// } |
51
|
|
|
|
52
|
|
|
public function onBeforeRollback() |
53
|
|
|
{ |
54
|
|
|
$this->doUpdateCache(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function onAfterPublish() |
58
|
|
|
{ |
59
|
|
|
$this->doUpdateCache(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function onAfterArchive() |
63
|
|
|
{ |
64
|
|
|
$this->doUpdateCache(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function onAfterUnpublish() |
68
|
|
|
{ |
69
|
|
|
$this->doUpdateCache(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function onAfterVersionedPublish() |
73
|
|
|
{ |
74
|
|
|
$this->doUpdateCache(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function onAfterWriteToStage($toStage) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$this->doUpdateCache(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function doUpdateCache() |
83
|
|
|
{ |
84
|
|
|
$className = (string) $this->getOwner()->ClassName; |
85
|
|
|
if ($className && $this->canUpdateCache($className)) { |
86
|
|
|
SimpleTemplateCachingSiteConfigExtension::update_cache_key($className); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function canUpdateCache($className): bool |
91
|
|
|
{ |
92
|
|
|
// we want to always avoid this to avoid a loop. |
93
|
|
|
if (ObjectsUpdated::class === $className) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
$excludedClasses = (array) Config::inst()->get(DataObjectExtension::class, 'excluded_classes_for_caching'); |
97
|
|
|
|
98
|
|
|
return ! in_array($className, $excludedClasses, true); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.