1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by jensk on 25-10-2017. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CloudControl\Cms\components; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use CloudControl\Cms\cc\Request; |
10
|
|
|
use CloudControl\Cms\storage\Cache; |
11
|
|
|
|
12
|
|
|
class CachableBaseComponent extends BaseComponent |
13
|
|
|
{ |
14
|
|
|
const DEFAULT_MAXAGE = 'P7D'; |
15
|
|
|
|
16
|
|
|
const PARAMETER_CACHABLE = 'cachable'; |
17
|
|
|
const PARAMETER_MAXAGE = 'maxage'; |
18
|
|
|
|
19
|
|
|
protected $cachable = true; |
20
|
|
|
protected $maxAge = self::DEFAULT_MAXAGE; |
21
|
|
|
protected $cacheValidity; |
22
|
|
|
protected $cache; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* CachableBaseComponent constructor. |
26
|
|
|
* @param string $template |
27
|
|
|
* @param Request $request |
28
|
|
|
* @param array $parameters |
29
|
|
|
* @param $matchedSitemapItem |
30
|
|
|
*/ |
31
|
|
|
public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($template, $request, $parameters, $matchedSitemapItem); |
34
|
|
|
|
35
|
|
|
if (isset($this->parameters[self::PARAMETER_CACHABLE]) && $this->parameters[self::PARAMETER_CACHABLE] === 'false') { |
36
|
|
|
$this->cachable = false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (isset($this->parameters[self::PARAMETER_MAXAGE])) { |
40
|
|
|
$this->maxAge = $this->parameters[self::PARAMETER_MAXAGE]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->setCacheValidity(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get() |
47
|
|
|
{ |
48
|
|
|
if ($this->isCachable() && $this->isCacheValid()) { |
49
|
|
|
return $this->cache->contents; |
50
|
|
|
} else if ($this->isCachable() && !$this->isCacheValid()) { |
51
|
|
|
$this->createCache($this->renderedContent); |
52
|
|
|
} |
53
|
|
|
return $this->renderedContent; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function isCachable() |
61
|
|
|
{ |
62
|
|
|
return $this->cachable; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function isCacheValid() |
69
|
|
|
{ |
70
|
|
|
if ($this->cacheValidity === null) { |
71
|
|
|
return $this->setCacheValidity(); |
72
|
|
|
} |
73
|
|
|
return $this->cacheValidity; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getMaxAge() |
80
|
|
|
{ |
81
|
|
|
return $this->maxAge; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function setCacheValidity() |
85
|
|
|
{ |
86
|
|
|
if ($this->cacheValidity === null) { |
87
|
|
|
$this->cache = Cache::getInstance()->getCacheForPath(Request::$requestUri); |
88
|
|
|
$cacheExists = $this->cache !== false; |
89
|
|
|
$cacheExpired = false; |
90
|
|
|
if ($cacheExists) { |
91
|
|
|
$cacheCreationStamp = (int) $this->cache->creationStamp; |
92
|
|
|
$currentTime = time(); |
93
|
|
|
$cacheAge = $currentTime - $cacheCreationStamp; |
94
|
|
|
$maxAgeInterval = new \DateInterval($this->maxAge); |
95
|
|
|
$maxAgeSeconds = date_create('@0')->add($maxAgeInterval)->getTimestamp(); |
96
|
|
|
$cacheExpired = $cacheAge > $maxAgeSeconds; |
97
|
|
|
} |
98
|
|
|
$this->cacheValidity = $cacheExists && !$cacheExpired; |
99
|
|
|
} |
100
|
|
|
return $this->cacheValidity; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function createCache($renderedContent) |
104
|
|
|
{ |
105
|
|
|
Cache::getInstance()->setCacheForPath(Request::$requestUri, $renderedContent); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |