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