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\cc\ResponseHeaders; |
||
11 | use CloudControl\Cms\storage\Cache; |
||
12 | |||
13 | class CachableBaseComponent extends BaseComponent |
||
14 | { |
||
15 | const DEFAULT_MAXAGE = 'P7D'; |
||
16 | |||
17 | const PARAMETER_CACHABLE = 'cachable'; |
||
18 | const PARAMETER_MAXAGE = 'maxage'; |
||
19 | |||
20 | protected $cachable = true; |
||
21 | protected $maxAge = self::DEFAULT_MAXAGE; |
||
22 | protected $cacheValidity; |
||
23 | protected $cache; |
||
24 | |||
25 | /** |
||
26 | * CachableBaseComponent constructor. |
||
27 | * @param string $template |
||
28 | * @param Request $request |
||
29 | * @param array $parameters |
||
30 | * @param $matchedSitemapItem |
||
31 | */ |
||
32 | public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem) |
||
33 | { |
||
34 | parent::__construct($template, $request, $parameters, $matchedSitemapItem); |
||
35 | |||
36 | if (isset($this->parameters[self::PARAMETER_CACHABLE]) && $this->parameters[self::PARAMETER_CACHABLE] === 'false') { |
||
37 | $this->cachable = false; |
||
38 | } |
||
39 | |||
40 | if (isset($this->parameters[self::PARAMETER_MAXAGE])) { |
||
41 | $this->maxAge = $this->parameters[self::PARAMETER_MAXAGE]; |
||
42 | } |
||
43 | |||
44 | $this->setCacheValidity(); |
||
45 | } |
||
46 | |||
47 | public function get() |
||
48 | { |
||
49 | $isCachable = $this->isCachable(); |
||
50 | if ($isCachable && $this->isCacheValid()) { |
||
51 | $this->setHeadersFromCache(); |
||
52 | return $this->cache->contents; |
||
53 | } |
||
54 | |||
55 | if ($isCachable && !$this->isCacheValid()) { |
||
56 | $this->createCache($this->renderedContent); |
||
57 | } |
||
58 | return $this->renderedContent; |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function isCachable() |
||
66 | { |
||
67 | return $this->cachable; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function isCacheValid() |
||
74 | { |
||
75 | if ($this->isLoggedIn()) { |
||
76 | return false; |
||
77 | } |
||
78 | if ($this->cacheValidity === null) { |
||
79 | return $this->setCacheValidity(); |
||
80 | } |
||
81 | return $this->cacheValidity; |
||
82 | } |
||
83 | |||
84 | private function isLoggedIn() |
||
85 | { |
||
86 | return CmsComponent::isCmsLoggedIn(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getMaxAge() |
||
93 | { |
||
94 | return $this->maxAge; |
||
95 | } |
||
96 | |||
97 | protected function setCacheValidity() |
||
98 | { |
||
99 | if ($this->cacheValidity === null) { |
||
100 | $this->cache = Cache::getInstance()->getCacheForPath(Request::$requestUri); |
||
101 | $cacheExists = $this->cache !== false; |
||
102 | $cacheExpired = false; |
||
103 | if ($cacheExists) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
104 | $cacheCreationStamp = (int)$this->cache->creationStamp; |
||
105 | $currentTime = time(); |
||
106 | $cacheAge = $currentTime - $cacheCreationStamp; |
||
107 | $maxAgeInterval = new \DateInterval($this->maxAge); |
||
108 | $maxAgeSeconds = date_create('@0')->add($maxAgeInterval)->getTimestamp(); |
||
109 | $cacheExpired = $cacheAge > $maxAgeSeconds; |
||
110 | } |
||
111 | $this->cacheValidity = $cacheExists && !$cacheExpired; |
||
112 | } |
||
113 | return $this->cacheValidity; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Sets the new cache, unless a cms user is logged in |
||
118 | * @param $renderedContent |
||
119 | * @throws \RuntimeException |
||
120 | */ |
||
121 | private function createCache($renderedContent) |
||
122 | { |
||
123 | if (!CmsComponent::isCmsLoggedIn()) { |
||
124 | Cache::getInstance()->setCacheForPath(Request::$requestUri, $renderedContent, json_encode(ResponseHeaders::getHeaders())); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | private function setHeadersFromCache() |
||
129 | { |
||
130 | if (isset($this->cache->headers)) { |
||
131 | $headers = (array)json_decode($this->cache->headers); |
||
132 | /** @var array $headers */ |
||
133 | foreach ($headers as $headerName => $headerContent) { |
||
134 | ResponseHeaders::add($headerName, $headerContent); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | } |
||
140 | |||
141 | } |