Passed
Push — master ( 3317fd...38d44b )
by Nicolaas
03:15
created

SimpleTemplateCachingSiteConfigExtension.php (2 issues)

1
<?php
2
3
namespace Sunnysideup\SimpleTemplateCaching\Extensions;
4
5
use SilverStripe\Forms\DatetimeField;
6
use SilverStripe\Forms\ReadonlyField;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\FieldList;
9
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer;
12
use SilverStripe\ORM\DataExtension;
13
use SilverStripe\ORM\DB;
14
use SilverStripe\ORM\FieldType\DBDatetime;
15
use SilverStripe\SiteConfig\SiteConfig;
16
17
use SilverStripe\Core\Injector\Injector;
18
19
use Sunnysideup\SimpleTemplateCaching\Model\ObjectsUpdated;
20
21
class SimpleTemplateCachingSiteConfigExtension extends DataExtension
22
{
23
    private static $db = [
24
        'HasCaching' => 'Boolean(1)',
25
        'RecordCacheUpdates' => 'Boolean(0)',
26
        'CacheKeyLastEdited' => 'DBDatetime',
27
        'ClassNameLastEdited' => 'Varchar(200)',
28
    ];
29
30
    public function updateCMSFields(FieldList $fields)
31
    {
32
        $name = '';
33
        if(class_exists($this->getOwner()->ClassNameLastEdited)) {
34
            $name = Injector::inst()->get($this->getOwner()->ClassNameLastEdited)->i18n_singular_name();
35
        }
36
        $fields->addFieldsToTab(
37
            'Root.Caching',
38
            [
39
                CheckboxField::create('HasCaching', 'Use caching'),
40
                CheckboxField::create('RecordCacheUpdates', 'To work out when the cache is being updated, you can track every change. This will slow down all your edits, so it is recommend only to turn this on temporarily - for tuning purposes.'),
41
                DatetimeField::create('CacheKeyLastEdited', 'Content Last Edited')
42
                    ->setRightTitle('The frontend template cache will be invalidated every time this value changes. It changes every time anything is changed in the database.'),
43
                ReadonlyField::create('ClassNameLastEdited', 'Last class updated')
44
                    ->setRightTitle('Last object updated. The name of this object is: '.$name),
45
            ]
46
        );
47
        if($this->getOwner()->RecordCacheUpdates) {
48
            $fields->addFieldsToTab(
49
                'Root.Caching',
50
                [
51
                    GridField::create(
52
                        'ObjectsUpdated',
53
                        'Last 1000 objects updated',
54
                        ObjectsUpdated::get()->limit(1000),
55
                        GridFieldConfig_RecordViewer::create()
56
                    )
57
                ]
58
            );
59
        }
60
    }
61
62
    public static function site_cache_key(): string
63
    {
64
        $obj = SiteConfig::current_site_config();
65
        if($obj->HasCaching) {
66
            return (string) 'ts_'.strtotime($obj->CacheKeyLastEdited);
67
        } else {
68
            return  (string) 'ts_'.time();
69
        }
70
    }
71
72
    public static function update_cache_key(?string $className = '')
73
    {
74
        $obj = SiteConfig::current_site_config();
75
        if($obj->HasCaching) {
76
            DB::query('
77
                UPDATE "SiteConfig"
78
                SET
79
                    "CacheKeyLastEdited" = \'' . DBDatetime::now()->Rfc2822() . '\',
80
                    "ClassNameLastEdited" = \'' . addslashes($className). '\'
0 ignored issues
show
It seems like $className can also be of type null; however, parameter $string of addslashes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                    "ClassNameLastEdited" = \'' . addslashes(/** @scrutinizer ignore-type */ $className). '\'
Loading history...
81
                WHERE ID = '.$obj->ID.'
82
            ;');
83
        }
84
        if($obj->RecordCacheUpdates) {
85
            $record = Injector::inst()
0 ignored issues
show
The assignment to $record is dead and can be removed.
Loading history...
86
                ->create(ObjectsUpdated::class, ['ClassNameLastEdited' => $className])
87
                ->write();
88
        }
89
    }
90
}
91