1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by: Jens |
4
|
|
|
* Date: 12-10-2017 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CloudControl\Cms\cc\application; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
use CloudControl\Cms\cc\Application; |
11
|
|
|
use CloudControl\Cms\cc\Request; |
12
|
|
|
use CloudControl\Cms\cc\ResponseHeaders; |
13
|
|
|
use CloudControl\Cms\components\CachableBaseComponent; |
14
|
|
|
use CloudControl\Cms\components\CmsComponent; |
15
|
|
|
use CloudControl\Cms\storage\Storage; |
16
|
|
|
|
17
|
|
|
class ApplicationRenderer |
18
|
|
|
{ |
19
|
|
|
protected $storage; |
20
|
|
|
protected $request; |
21
|
|
|
protected $application; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ApplicationRenderer constructor. |
25
|
|
|
* @param Application $application |
26
|
|
|
* @param Storage $storage |
27
|
|
|
* @param Request $request |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Application $application, Storage $storage, Request $request) |
30
|
|
|
{ |
31
|
|
|
$this->storage = $storage; |
32
|
|
|
$this->request = $request; |
33
|
|
|
$this->application = $application; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Loop through all application components and render them |
38
|
|
|
* @param array $applicationComponents |
39
|
|
|
*/ |
40
|
|
|
public function renderApplicationComponents($applicationComponents) |
41
|
|
|
{ |
42
|
|
|
foreach ($applicationComponents as $applicationComponent) { |
43
|
|
|
$applicationComponent->{'object'}->render(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Loop through all (matched) sitemap components and render them |
49
|
|
|
* @param array $matchedSitemapItems |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
|
|
public function renderSitemapComponents($matchedSitemapItems) |
53
|
|
|
{ |
54
|
|
|
if (count($matchedSitemapItems) < 1) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$sitemapItem = current($matchedSitemapItems); |
59
|
|
|
$this->renderSitemapComponent($sitemapItem); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the default caching of pages |
64
|
|
|
* @param string $intervalString |
65
|
|
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
|
|
public function setCachingHeaders($intervalString = CachableBaseComponent::DEFAULT_MAXAGE) |
68
|
|
|
{ |
69
|
|
|
$expires = new \DateTime(); |
70
|
|
|
$interval = new \DateInterval($intervalString); |
71
|
|
|
$maxAge = date_create('@0')->add($interval)->getTimestamp(); |
72
|
|
|
$expires = $expires->add($interval); |
73
|
|
|
ResponseHeaders::add(ResponseHeaders::HEADER_EXPIRES, gmdate('D, d M Y H:i:s \G\M\T', $expires->getTimestamp())); |
74
|
|
|
ResponseHeaders::add(ResponseHeaders::HEADER_CACHE_CONTROL, 'max-age=' . $maxAge); |
75
|
|
|
ResponseHeaders::add(ResponseHeaders::HEADER_PRAGMA, ResponseHeaders::HEADER_PRAGMA_CONTENT_CACHE); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set non caching |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
public function setNotCachingHeaders() |
83
|
|
|
{ |
84
|
|
|
ResponseHeaders::add(ResponseHeaders::HEADER_CACHE_CONTROL, ResponseHeaders::HEADER_CACHE_CONTROL_CONTENT_NO_STORE_NO_CACHE_MUST_REVALIDATE_MAX_AGE_0); |
85
|
|
|
ResponseHeaders::add(ResponseHeaders::HEADER_PRAGMA, ResponseHeaders::HEADER_PRAGMA_CONTENT_NO_CACHE); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $sitemapItem |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
|
|
private function renderSitemapComponent($sitemapItem) |
93
|
|
|
{ |
94
|
|
|
$isCachable = ($sitemapItem->object instanceof CachableBaseComponent) && !CmsComponent::isCmsLoggedIn(); |
95
|
|
|
|
96
|
|
|
$this->handleSitemapComponentCaching($sitemapItem, $isCachable); |
97
|
|
|
|
98
|
|
|
echo $sitemapItem->object->get(); |
99
|
|
|
$this->sendHeaders(); |
100
|
|
|
ob_end_flush(); |
101
|
|
|
exit; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $sitemapItem |
106
|
|
|
* @param $isCachable |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
private function handleSitemapComponentCaching($sitemapItem, $isCachable) |
110
|
|
|
{ |
111
|
|
|
if ($isCachable === false || ($isCachable && !$sitemapItem->object->isCachable())) { |
112
|
|
|
$sitemapItem->object->render($this->application); |
113
|
|
|
ob_clean(); |
114
|
|
|
$this->setNotCachingHeaders(); |
115
|
|
|
} elseif ($isCachable && $sitemapItem->object->isCachable()) { |
116
|
|
|
if (!$sitemapItem->object->isCacheValid()) { |
117
|
|
|
$sitemapItem->object->render($this->application); |
118
|
|
|
} |
119
|
|
|
ob_clean(); |
120
|
|
|
$this->setCachingHeaders($sitemapItem->object->getMaxAge()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Send headers |
126
|
|
|
*/ |
127
|
|
|
private function sendHeaders() |
128
|
|
|
{ |
129
|
|
|
if (PHP_SAPI !== 'cli') { |
130
|
|
|
ResponseHeaders::sendAllHeaders(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |