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