-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageGenerator.php
More file actions
227 lines (211 loc) · 7.8 KB
/
PageGenerator.php
File metadata and controls
227 lines (211 loc) · 7.8 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* Definition of class PageGenerator
*
* @copyright 2014-today Justso GmbH
* @author j.schirrmacher@justso.de
* @package justso\justgen
*/
namespace justso\justgen;
use justso\justapi\InvalidParameterException;
use justso\justapi\NotFoundException;
use justso\justapi\RestService;
use justso\justapi\SystemEnvironmentInterface;
/**
* Class PageGenerator
*
* @package justso\justgen
*/
class PageGenerator extends RestService
{
private $defaultsApplied = false;
private $language;
private $page = 'index';
private $forceSiteMapUpdate = false;
public function __construct(SystemEnvironmentInterface $environment)
{
parent::__construct($environment);
}
/**
* Service to generate a page
*/
public function getAction()
{
$config = $this->environment->getBootstrap()->getConfiguration();
$languages = $config['languages'];
try {
$this->extractParams($languages);
$rule = $this->findMatchingPageRule();
$content = $this->handlePageRule($rule, $languages);
$this->updateSiteMap();
$this->environment->sendResult("200 Ok", "text/html; charset=utf-8", $content);
} catch (RedirectException $e) {
$destination = $e->getMessage();
$this->environment->sendHeader("Location: " . $destination);
$this->environment->sendResult("301 Moved Permanently", 'text/plain', "Location: " . $destination);
} catch (NotFoundException $e) {
if (!empty($config['errorpages']['404'])) {
$this->page = $config['errorpages']['404'];
$this->defaultsApplied = false;
$content = $this->handlePageRule($config['pages'][$this->page], $languages);
$mime = 'text/html';
} else {
$content = "Page '{$this->page}' not found";
$mime = 'text/plain';
}
$this->environment->sendResult("404 Not found", "$mime; charset=utf-8", $content);
} catch (\Exception $e) {
$this->environment->sendResult('500 Server Error', 'text/plain', $e->getMessage());
}
}
/**
* Searches for a matching page rule and returns the template name.
*
* @return string Template name
* @throws \Exception
*/
private function findMatchingPageRule()
{
$config = $this->environment->getBootstrap()->getConfiguration();
$ruleMatcher = new RuleMatcher($config['pages']);
$entry = $ruleMatcher->find($this->page);
if ($entry !== null) {
return $entry;
}
throw new NotFoundException('No page rule defined');
}
/**
* Extracts language and page parameters from access path
*
* @param $languages
* @throws \justso\justapi\InvalidParameterException
*/
private function extractParams($languages)
{
$config = $this->environment->getBootstrap()->getConfiguration();
preg_match('/^\/?(..)?\/([^\?]*)/', $this->getURI(), $parts);
$this->language = $languages[0];
$fallback = !empty($config['fallbackForUnknownLanguage']);
if (empty($parts[1]) || !in_array($parts[1], $languages) && $fallback) {
$this->defaultsApplied = true;
} else {
if (!in_array($parts[1], $languages)) {
throw new InvalidParameterException("Unknown language");
}
$this->language = $parts[1];
}
if (empty($parts[2])) {
$this->defaultsApplied = true;
} else {
$this->page = preg_replace('/(\.html|\/)$/', '', $parts[2]);
if (isset($config['redirects'])) {
$ruleMatcher = new RuleMatcher($config['redirects']);
$entry = $ruleMatcher->find($this->page);
if ($entry !== null) {
$this->page = $entry;
$this->defaultsApplied = true;
}
}
}
}
private function getURI()
{
$server = $this->environment->getRequestHelper()->getServerParams();
if (empty($server['REQUEST_URI'])) {
throw new InvalidParameterException("Missing path info");
}
return $server['REQUEST_URI'];
}
/**
* Stores the content as a file for improved access performance for later requests.
*
* @param $content
*/
private function storeContent($content)
{
$appRoot = $this->environment->getBootstrap()->getAppRoot();
$destination = $appRoot . '/htdocs/' . $this->language . '/' . $this->page . '.html';
$fs = $this->environment->getFileSystem();
$fs->putFile($destination, $content);
}
/**
* Handles the page rule by generating the content, caching it and sending the result.
*
* @param string $rule
* @param string[] $languages
* @param string $okCode
* @return string content of page
* @throws RedirectException
*/
public function handlePageRule($rule, $languages, $okCode = '200 Ok')
{
$dynamic = strpos($rule, 'dynamic:') === 0;
$develop = $this->environment->getBootstrap()->getInstallationType() === 'development';
$template = $dynamic ? substr($rule, strlen('dynamic:')) : $rule;
$content = $this->generate($template, $languages);
if (!$dynamic && !$develop) {
$this->storeContent($content);
}
if ($this->defaultsApplied && !$dynamic) {
throw new RedirectException('/' . $this->language . '/' . $this->page);
}
return $content;
}
/**
* Generates the page by applying parameters, variables and texts to the template.
*
* @param string $templateName
* @param string[] $languages
* @return string
*/
private function generate($templateName, $languages)
{
$rawParams = $this->environment->getRequestHelper()->getAllParams();
$params = array_filter($rawParams, function () use (&$rawParams) {
$key = key($rawParams);
next($rawParams);
return strpos($key, '_') !== 0;
});
$templateRoot = $this->environment->getBootstrap()->getAppRoot() . '/templates';
$pageTemplate = new PageTemplate($templateRoot, $templateName . '.tpl', $languages, $params);
return $pageTemplate->generate($this->language, $this->page, $this->environment);
}
/**
* Updates the sitemap.xml file
*/
private function updateSiteMap()
{
$bootstrap = $this->environment->getBootstrap();
if ($this->forceSiteMapUpdate || $bootstrap->getInstallationType() === 'production') {
$url = $bootstrap->getWebAppUrl() . '/' . $this->language . '/' . $this->page;
$fs = $this->environment->getFileSystem();
$now = (new \DateTime())->format(\DateTime::W3C);
$fileName = $bootstrap->getAppRoot() . '/htdocs/sitemap.xml';
$sitemap = new \SimpleXMLElement($fs->getFile($fileName));
$found = false;
foreach ($sitemap->url as $page) {
if ($page->loc == $url) {
$page->lastmod = $now;
$found = true;
break;
}
}
if (!$found) {
$page = $sitemap->addChild('url');
$page->loc = $url;
$page->lastmod = $now;
$page->changefreq = 'daily';
$page->priority = '0.6';
}
$dom = new \DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sitemap->asXML());
$fs->putFile($fileName, $dom->saveXML());
}
}
public function enforceSiteMapUpdate()
{
$this->forceSiteMapUpdate = true;
}
}