This repository was archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPlugin.php
More file actions
135 lines (107 loc) · 3.62 KB
/
Plugin.php
File metadata and controls
135 lines (107 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php namespace ostark\upper;
use Craft;
use craft\base\Plugin as BasePlugin;
use ostark\upper\behaviors\CacheControlBehavior;
use ostark\upper\behaviors\TagHeaderBehavior;
use ostark\upper\drivers\CachePurgeInterface;
use ostark\upper\models\Settings;
/**
* Class Plugin
*
* @package ostark\upper
*
* @method models\Settings getSettings()
*/
class Plugin extends BasePlugin
{
// Event names
const EVENT_AFTER_SET_TAG_HEADER = 'upper_after_set_tag_header';
const EVENT_BEFORE_PURGE = 'upper_before_purge';
const EVENT_AFTER_PURGE = 'upper_after_purge';
// Tag prefixes
const TAG_PREFIX_ELEMENT = 'el';
const TAG_PREFIX_SECTION = 'se';
const TAG_PREFIX_STRUCTURE = 'st';
// Mapping element properties <> tag prefixes
const ELEMENT_PROPERTY_MAP = [
'id' => self::TAG_PREFIX_ELEMENT,
'sectionId' => self::TAG_PREFIX_SECTION,
'structureId' => self::TAG_PREFIX_STRUCTURE
];
// DB
const CACHE_TABLE = '{{%upper_cache}}';
// Header
const INFO_HEADER_NAME = 'X-UPPER-CACHE';
const TRUNCATED_HEADER_NAME = 'X-UPPER-CACHE-TRUNCATED';
public $schemaVersion = '1.0.1';
/**
* Initialize Plugin
*/
public function init()
{
parent::init();
// Config pre-check
if (!$this->getSettings()->drivers || !$this->getSettings()->driver) {
return false;
}
// Register plugin components
$this->setComponents([
'purger' => PurgerFactory::create($this->getSettings()->toArray()),
'tagCollection' => TagCollection::class
]);
// Attach Behaviors
\Craft::$app->getResponse()->attachBehavior('cache-control', CacheControlBehavior::class);
\Craft::$app->getResponse()->attachBehavior('tag-header', TagHeaderBehavior::class);
// Register event handlers
EventRegistrar::registerFrontendEvents();
EventRegistrar::registerCpEvents();
EventRegistrar::registerUpdateEvents();
if ($this->getSettings()->useLocalTags) {
EventRegistrar::registerFallback();
}
// Register Twig extension
\Craft::$app->getView()->registerTwigExtension(new TwigExtension);
}
// ServiceLocators
// =========================================================================
/**
* @return \ostark\upper\drivers\CachePurgeInterface
*/
public function getPurger(): CachePurgeInterface
{
return $this->get('purger');
}
/**
* @return \ostark\upper\TagCollection
*/
public function getTagCollection(): TagCollection
{
/* @var \ostark\upper\TagCollection $collection */
$collection = $this->get('tagCollection');
$collection->setKeyPrefix($this->getSettings()->getKeyPrefix());
return $collection;
}
// Protected Methods
// =========================================================================
/**
* Creates and returns the model used to store the plugin’s settings.
*
* @return \craft\base\Model|null
*/
protected function createSettingsModel()
{
return new Settings();
}
/**
* Is called after the plugin is installed.
* Copies example config to project's config folder
*/
protected function afterInstall()
{
$configSourceFile = __DIR__ . DIRECTORY_SEPARATOR . 'config.example.php';
$configTargetFile = \Craft::$app->getConfig()->configDir . DIRECTORY_SEPARATOR . $this->handle . '.php';
if (!file_exists($configTargetFile)) {
copy($configSourceFile, $configTargetFile);
}
}
}