-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.class.php
57 lines (49 loc) · 1.43 KB
/
Plugin.class.php
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
<?php
abstract class Plugin {
protected $config;
protected $name = "unknown";
protected $cache_duration = 300;
abstract protected function init();
abstract protected function getData();
public function __construct() {
$this->config = ConfigProvider::getInstance()->get($this->name);
$this->init();
}
public function getName() {
return $this->name;
}
public function isActive() {
return $this->config["active"];
}
public function renderToString() {
$data = $this->load_cache($this->name, $this->cache_duration);
if ($data === false) {
$data = $this->getData();
$this->save_cache($this->name, $data);
}
$template = new Template("plugins/{$this->name}.view.php", $data);
return $template->renderToString();
}
/* *******************************
* Caching functions
* Adapted from http://staticfloat.com/php-programmieren/simplen-cache-mit-php-erstellen/
* and http://www.theukwebdesigncompany.com/articles/php-caching.php
* ******************************* */
private function save_cache($key, $data) {
$key = md5($key);
$data = serialize($data);
$path = BASE_PATH.'/cache/'.$key;
file_put_contents($path, $data);
}
private function load_cache($key, $expire) {
$key = md5($key);
$path = BASE_PATH.'/cache/'.$key;
if (file_exists($path)) {
if (time() < (filemtime($path) + $expire)) {
return unserialize(file_get_contents($path));
}
unlink($path);
}
return false;
}
}