|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Http\Controllers\Website; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Assets\AssetManager; |
|
8
|
|
|
use AbterPhp\Framework\Config\EnvReader; |
|
9
|
|
|
use AbterPhp\Framework\Constant\Session; |
|
10
|
|
|
use AbterPhp\Framework\Http\Controllers\ControllerAbstract; |
|
11
|
|
|
use AbterPhp\Framework\Session\FlashService; |
|
12
|
|
|
use AbterPhp\Website\Constant\Env; |
|
13
|
|
|
use AbterPhp\Website\Constant\Route; |
|
14
|
|
|
use AbterPhp\Website\Domain\Entities\Page; |
|
15
|
|
|
use AbterPhp\Website\Domain\Entities\Page\Assets; |
|
16
|
|
|
use AbterPhp\Website\Domain\Entities\Page\Meta; |
|
17
|
|
|
use AbterPhp\Website\Domain\Entities\PageLayout\Assets as LayoutAssets; |
|
18
|
|
|
use AbterPhp\Website\Service\Website\Index as IndexService; |
|
19
|
|
|
use Casbin\Exceptions\CasbinException; |
|
20
|
|
|
use League\Flysystem\FilesystemException; |
|
21
|
|
|
use Opulence\Http\Responses\Response; |
|
22
|
|
|
use Opulence\Http\Responses\ResponseHeaders; |
|
23
|
|
|
use Opulence\Orm\OrmException; |
|
24
|
|
|
use Opulence\Routing\Urls\UrlGenerator; |
|
25
|
|
|
use Opulence\Sessions\ISession; |
|
26
|
|
|
use Psr\Log\LoggerInterface; |
|
27
|
|
|
use Throwable; |
|
28
|
|
|
|
|
29
|
|
|
class Index extends ControllerAbstract |
|
30
|
|
|
{ |
|
31
|
|
|
const ERROR = 'error'; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ISession */ |
|
34
|
|
|
protected $session; |
|
35
|
|
|
|
|
36
|
|
|
/** @var IndexService */ |
|
37
|
|
|
protected $indexService; |
|
38
|
|
|
|
|
39
|
|
|
/** @var UrlGenerator */ |
|
40
|
|
|
protected $urlGenerator; |
|
41
|
|
|
|
|
42
|
|
|
/** @var AssetManager */ |
|
43
|
|
|
protected $assetManager; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string */ |
|
46
|
|
|
protected $baseUrl; |
|
47
|
|
|
|
|
48
|
|
|
/** @var string */ |
|
49
|
|
|
protected $siteTitle; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Index constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param FlashService $flashService |
|
55
|
|
|
* @param LoggerInterface $logger |
|
56
|
|
|
* @param ISession $session |
|
57
|
|
|
* @param IndexService $indexService |
|
58
|
|
|
* @param UrlGenerator $urlGenerator |
|
59
|
|
|
* @param AssetManager $assetManager |
|
60
|
|
|
* @param EnvReader $envReader |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( |
|
63
|
|
|
FlashService $flashService, |
|
64
|
|
|
LoggerInterface $logger, |
|
65
|
|
|
ISession $session, |
|
66
|
|
|
IndexService $indexService, |
|
67
|
|
|
UrlGenerator $urlGenerator, |
|
68
|
|
|
AssetManager $assetManager, |
|
69
|
|
|
EnvReader $envReader |
|
70
|
|
|
) { |
|
71
|
|
|
$this->session = $session; |
|
72
|
|
|
$this->indexService = $indexService; |
|
73
|
|
|
$this->urlGenerator = $urlGenerator; |
|
74
|
|
|
$this->assetManager = $assetManager; |
|
75
|
|
|
|
|
76
|
|
|
$this->baseUrl = $envReader->get(Env::WEBSITE_BASE_URL); |
|
77
|
|
|
$this->siteTitle = $envReader->get(Env::WEBSITE_SITE_TITLE); |
|
78
|
|
|
|
|
79
|
|
|
parent::__construct($flashService, $logger); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Shows the homepage |
|
84
|
|
|
* |
|
85
|
|
|
* @return Response |
|
86
|
|
|
* @throws Throwable |
|
87
|
|
|
*/ |
|
88
|
|
|
public function index(): Response |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->fallback('index'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Shows the homepage |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $identifier |
|
97
|
|
|
* |
|
98
|
|
|
* @return Response |
|
99
|
|
|
* @throws Throwable |
|
100
|
|
|
*/ |
|
101
|
|
|
public function fallback(string $identifier): Response |
|
102
|
|
|
{ |
|
103
|
|
|
$this->view = $this->viewFactory->createView('contents/frontend/page'); |
|
104
|
|
|
|
|
105
|
|
|
try { |
|
106
|
|
|
$page = $this->indexService->getRenderedPage($identifier, $this->getUserGroupIdentifiers()); |
|
107
|
|
|
} catch (OrmException $e) { |
|
108
|
|
|
$this->logger->error($e->getMessage(), $e->getTrace()); |
|
109
|
|
|
} catch (CasbinException $e) { |
|
110
|
|
|
$this->logger->info($e->getMessage(), $e->getTrace()); |
|
111
|
|
|
} catch (\Exception $e) { |
|
112
|
|
|
$this->logger->critical($e->getMessage(), $e->getTrace()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (empty($page)) { |
|
116
|
|
|
if ($identifier == static::ERROR) { |
|
117
|
|
|
return $this->fallback(static::ERROR); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$page = new Page('', '', '', '', '', '', false); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument |
|
124
|
|
|
$pageUrl = $this->urlGenerator->createFromName(Route::FALLBACK, $identifier); |
|
125
|
|
|
$homepageUrl = $this->urlGenerator->createFromName(Route::INDEX); |
|
126
|
|
|
|
|
127
|
|
|
$this->view->setVar('body', $page->getRenderedBody()); |
|
128
|
|
|
$this->view->setVar('siteTitle', $this->siteTitle); |
|
129
|
|
|
$this->view->setVar('pageUrl', $pageUrl); |
|
130
|
|
|
$this->view->setVar('homepageUrl', $homepageUrl); |
|
131
|
|
|
$this->view->setVar('classes', trim($page->getClasses())); |
|
132
|
|
|
|
|
133
|
|
|
$this->setMetaVars($page->getMeta()); |
|
134
|
|
|
$this->setAssetsVars($page->getAssets()); |
|
135
|
|
|
|
|
136
|
|
|
return $this->createResponse($page->getTitle()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string[] |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getUserGroupIdentifiers(): array |
|
143
|
|
|
{ |
|
144
|
|
|
$username = (string)$this->session->get(Session::USERNAME, ''); |
|
145
|
|
|
if (!$username) { |
|
146
|
|
|
return []; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$userGroupIdentifiers = $this->indexService->getUserGroupIdentifiers($username); |
|
150
|
|
|
|
|
151
|
|
|
return $userGroupIdentifiers; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param Meta $meta |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function setMetaVars(Meta $meta) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->view->setVar('metaDescription', $meta->getDescription()); |
|
160
|
|
|
$this->view->setVar('metaKeywords', explode(',', $meta->getKeywords())); |
|
161
|
|
|
$this->view->setVar('metaCopyright', $meta->getCopyright()); |
|
162
|
|
|
$this->view->setVar('authorName', $meta->getAuthor()); |
|
163
|
|
|
$this->view->setVar('metaRobots', $meta->getRobots()); |
|
164
|
|
|
$this->view->setVar('metaOGDescription', $meta->getOGDescription()); |
|
165
|
|
|
$this->view->setVar('metaOGTitle', $meta->getOGTitle()); |
|
166
|
|
|
$this->view->setVar('metaOGImage', $meta->getOGImage()); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param Assets|null $assets |
|
171
|
|
|
* |
|
172
|
|
|
* @throws FilesystemException |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function setAssetsVars(?Assets $assets) |
|
175
|
|
|
{ |
|
176
|
|
|
if ($assets === null) { |
|
177
|
|
|
return; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$origHeader = $this->view->hasVar('header') ? (string)$this->view->getVar('header') : ''; |
|
181
|
|
|
$origFooter = $this->view->hasVar('footer') ? (string)$this->view->getVar('footer') : ''; |
|
182
|
|
|
|
|
183
|
|
|
$this->view->setVar('header', $origHeader . $assets->getHeader()); |
|
184
|
|
|
$this->view->setVar('footer', $origFooter . $assets->getFooter()); |
|
185
|
|
|
$this->view->setVar('page', $assets->getKey()); |
|
186
|
|
|
|
|
187
|
|
|
$key = $assets->getKey(); |
|
188
|
|
|
foreach ($assets->getJsFiles() as $jsFile) { |
|
189
|
|
|
$this->assetManager->addJs($key, $jsFile); |
|
190
|
|
|
} |
|
191
|
|
|
foreach ($assets->getCssFiles() as $cssFile) { |
|
192
|
|
|
$this->assetManager->addCss($key, $cssFile); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this->view->setVar('pageJs', count($assets->getJsFiles())); |
|
196
|
|
|
$this->view->setVar('pageCss', count($assets->getCssFiles())); |
|
197
|
|
|
|
|
198
|
|
|
$this->setLayoutAssetsVars($assets->getLayoutAssets()); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param LayoutAssets|null $assets |
|
203
|
|
|
* |
|
204
|
|
|
* @throws FilesystemException |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function setLayoutAssetsVars(?LayoutAssets $assets) |
|
207
|
|
|
{ |
|
208
|
|
|
if ($assets === null) { |
|
209
|
|
|
return; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$origHeader = $this->view->hasVar('header') ? (string)$this->view->getVar('header') : ''; |
|
213
|
|
|
$origFooter = $this->view->hasVar('footer') ? (string)$this->view->getVar('footer') : ''; |
|
214
|
|
|
|
|
215
|
|
|
$this->view->setVar('header', $origHeader . $assets->getHeader()); |
|
216
|
|
|
$this->view->setVar('footer', $origFooter . $assets->getFooter()); |
|
217
|
|
|
$this->view->setVar('layout', $assets->getKey()); |
|
218
|
|
|
|
|
219
|
|
|
$key = $assets->getKey(); |
|
220
|
|
|
foreach ($assets->getJsFiles() as $jsFile) { |
|
221
|
|
|
$this->assetManager->addJs($key, $jsFile); |
|
222
|
|
|
} |
|
223
|
|
|
foreach ($assets->getCssFiles() as $cssFile) { |
|
224
|
|
|
$this->assetManager->addCss($key, $cssFile); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$this->view->setVar('layoutJs', count($assets->getJsFiles())); |
|
228
|
|
|
$this->view->setVar('layoutCss', count($assets->getCssFiles())); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* 404 page |
|
233
|
|
|
* |
|
234
|
|
|
* @return Response |
|
235
|
|
|
* @throws \Throwable |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function notFound(): Response |
|
238
|
|
|
{ |
|
239
|
|
|
$this->view = $this->viewFactory->createView('contents/frontend/404'); |
|
240
|
|
|
|
|
241
|
|
|
$response = $this->createResponse('404 Page not Found'); |
|
242
|
|
|
$response->setStatusCode(ResponseHeaders::HTTP_NOT_FOUND); |
|
243
|
|
|
|
|
244
|
|
|
return $response; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|