|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Service\Execute; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract; |
|
8
|
|
|
use AbterPhp\Framework\Constant\Session; |
|
9
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
|
10
|
|
|
use AbterPhp\Website\Constant\Authorization; |
|
11
|
|
|
use AbterPhp\Website\Domain\Entities\Page as Entity; |
|
12
|
|
|
use AbterPhp\Website\Domain\Entities\Page\Assets; |
|
13
|
|
|
use AbterPhp\Website\Domain\Entities\Page\Meta; |
|
14
|
|
|
use AbterPhp\Website\Domain\Entities\PageCategory; |
|
|
|
|
|
|
15
|
|
|
use AbterPhp\Website\Orm\PageRepo as GridRepo; |
|
16
|
|
|
use AbterPhp\Website\Validation\Factory\Page as ValidatorFactory; |
|
17
|
|
|
use Casbin\Enforcer; |
|
18
|
|
|
use Cocur\Slugify\Slugify; |
|
19
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
|
20
|
|
|
use Opulence\Http\Requests\UploadedFile; |
|
21
|
|
|
use Opulence\Orm\IUnitOfWork; |
|
22
|
|
|
use Opulence\Orm\OrmException; |
|
23
|
|
|
use Opulence\Sessions\ISession; |
|
24
|
|
|
|
|
25
|
|
|
class Page extends RepoServiceAbstract |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var Slugify */ |
|
28
|
|
|
protected $slugify; |
|
29
|
|
|
|
|
30
|
|
|
/** @var GridRepo */ |
|
31
|
|
|
protected $repo; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ISession */ |
|
34
|
|
|
protected $session; |
|
35
|
|
|
|
|
36
|
|
|
/** @var Enforcer */ |
|
37
|
|
|
protected $enforcer; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Page constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param GridRepo $repo |
|
43
|
|
|
* @param ValidatorFactory $validatorFactory |
|
44
|
|
|
* @param IUnitOfWork $unitOfWork |
|
45
|
|
|
* @param IEventDispatcher $eventDispatcher |
|
46
|
|
|
* @param Slugify $slugify |
|
47
|
|
|
* @param ISession $session |
|
48
|
|
|
* @param Enforcer $enforcer |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
GridRepo $repo, |
|
52
|
|
|
ValidatorFactory $validatorFactory, |
|
53
|
|
|
IUnitOfWork $unitOfWork, |
|
54
|
|
|
IEventDispatcher $eventDispatcher, |
|
55
|
|
|
Slugify $slugify, |
|
56
|
|
|
ISession $session, |
|
57
|
|
|
Enforcer $enforcer |
|
58
|
|
|
) { |
|
59
|
|
|
parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher); |
|
60
|
|
|
|
|
61
|
|
|
$this->slugify = $slugify; |
|
62
|
|
|
$this->session = $session; |
|
63
|
|
|
$this->enforcer = $enforcer; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $entityId |
|
68
|
|
|
* |
|
69
|
|
|
* @return Entity |
|
70
|
|
|
*/ |
|
71
|
|
|
public function createEntity(string $entityId): IStringerEntity |
|
72
|
|
|
{ |
|
73
|
|
|
return new Entity($entityId, '', '', '', '', '', false); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
78
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
79
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
80
|
|
|
* |
|
81
|
|
|
* @param IStringerEntity $entity |
|
82
|
|
|
* @param array $postData |
|
83
|
|
|
* @param UploadedFile[] $fileData |
|
84
|
|
|
* |
|
85
|
|
|
* @return Entity |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity |
|
88
|
|
|
{ |
|
89
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
|
90
|
|
|
|
|
91
|
|
|
$postData = $this->protectPostData($entity, $postData); |
|
92
|
|
|
|
|
93
|
|
|
$title = $postData['title']; |
|
94
|
|
|
|
|
95
|
|
|
$identifier = $postData['identifier'] ?? $entity->getIdentifier(); |
|
96
|
|
|
$identifier = $identifier ?: $title; |
|
97
|
|
|
$identifier = $this->slugify->slugify($identifier); |
|
98
|
|
|
|
|
99
|
|
|
$classes = $postData['classes'] ?? ''; |
|
100
|
|
|
$lede = $postData['lede']; |
|
101
|
|
|
$body = $postData['body']; |
|
102
|
|
|
|
|
103
|
|
|
$isDraft = !empty($postData['is_draft']); |
|
104
|
|
|
|
|
105
|
|
|
$category = null; |
|
106
|
|
|
if (!empty($postData['category_id'])) { |
|
107
|
|
|
$category = new PageCategory($postData['category_id'], '', ''); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$layoutId = $postData['layout_id']; |
|
111
|
|
|
$layout = ''; |
|
112
|
|
|
if (empty($layoutId)) { |
|
113
|
|
|
$layoutId = null; |
|
114
|
|
|
$layout = $postData['layout'] ?? ''; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$meta = $this->getMeta($postData); |
|
118
|
|
|
$assets = $this->getAssets($postData, $identifier); |
|
119
|
|
|
|
|
120
|
|
|
$entity |
|
121
|
|
|
->setIdentifier($identifier) |
|
122
|
|
|
->setTitle($title) |
|
123
|
|
|
->setClasses($classes) |
|
124
|
|
|
->setLede($lede) |
|
125
|
|
|
->setBody($body) |
|
126
|
|
|
->setIsDraft($isDraft) |
|
127
|
|
|
->setCategory($category) |
|
128
|
|
|
->setLayoutId($layoutId) |
|
129
|
|
|
->setLayout($layout) |
|
130
|
|
|
->setMeta($meta) |
|
131
|
|
|
->setAssets($assets); |
|
132
|
|
|
|
|
133
|
|
|
return $entity; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param Entity $entity |
|
138
|
|
|
* @param array $postData |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
* @throws \Casbin\Exceptions\CasbinException |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function protectPostData(Entity $entity, array $postData): array |
|
144
|
|
|
{ |
|
145
|
|
|
$username = $this->session->get(Session::USERNAME); |
|
146
|
|
|
$advancedAllowed = $this->enforcer->enforce( |
|
147
|
|
|
$username, |
|
148
|
|
|
Authorization::RESOURCE_PAGES, |
|
149
|
|
|
Authorization::ROLE_ADVANCED_WRITE |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
if ($advancedAllowed) { |
|
153
|
|
|
return $postData; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$postData['layout_id'] = empty($postData['layout_id']) ? $entity->getLayoutId() : $postData['layout_id']; |
|
157
|
|
|
$postData['layout'] = $entity->getLayout(); |
|
158
|
|
|
|
|
159
|
|
|
return $postData; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
164
|
|
|
* |
|
165
|
|
|
* @param array $postData |
|
166
|
|
|
* |
|
167
|
|
|
* @return Meta |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function getMeta(array $postData): Meta |
|
170
|
|
|
{ |
|
171
|
|
|
$description = $postData['description'] ?? ''; |
|
172
|
|
|
$robots = $postData['robots'] ?? ''; |
|
173
|
|
|
$author = $postData['author'] ?? ''; |
|
174
|
|
|
$copyright = $postData['copyright'] ?? ''; |
|
175
|
|
|
$keywords = $postData['keywords'] ?? ''; |
|
176
|
|
|
$ogTitle = $postData['og-title'] ?? ''; |
|
177
|
|
|
$ogImage = $postData['og-image'] ?? ''; |
|
178
|
|
|
$ogDescription = $postData['og-description'] ?? ''; |
|
179
|
|
|
|
|
180
|
|
|
$entity = new Meta( |
|
181
|
|
|
$description, |
|
182
|
|
|
$robots, |
|
183
|
|
|
$author, |
|
184
|
|
|
$copyright, |
|
185
|
|
|
$keywords, |
|
186
|
|
|
$ogTitle, |
|
187
|
|
|
$ogImage, |
|
188
|
|
|
$ogDescription |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
return $entity; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
196
|
|
|
* |
|
197
|
|
|
* @param array $postData |
|
198
|
|
|
* @param string $identifier |
|
199
|
|
|
* |
|
200
|
|
|
* @return Assets |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function getAssets(array $postData, string $identifier): Assets |
|
203
|
|
|
{ |
|
204
|
|
|
$header = $postData['header'] ?? ''; |
|
205
|
|
|
$footer = $postData['footer'] ?? ''; |
|
206
|
|
|
$cssFiles = empty($postData['css-files']) ? [] : explode('\r\n', $postData['css-files']); |
|
207
|
|
|
$jsFiles = empty($postData['js-files']) ? [] : explode('\r\n', $postData['js-files']); |
|
208
|
|
|
|
|
209
|
|
|
return new Assets( |
|
210
|
|
|
$identifier, |
|
211
|
|
|
$header, |
|
212
|
|
|
$footer, |
|
213
|
|
|
$cssFiles, |
|
214
|
|
|
$jsFiles, |
|
215
|
|
|
null |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $entityId |
|
221
|
|
|
* |
|
222
|
|
|
* @return IStringerEntity |
|
223
|
|
|
* @throws OrmException |
|
224
|
|
|
*/ |
|
225
|
|
|
public function retrieveEntityWithLayout(string $entityId): IStringerEntity |
|
226
|
|
|
{ |
|
227
|
|
|
$entity = $this->repo->getById($entityId); |
|
228
|
|
|
|
|
229
|
|
|
$entity = $this->repo->getWithLayout($entity->getIdentifier()); |
|
230
|
|
|
|
|
231
|
|
|
if ($entity === null) { |
|
232
|
|
|
throw new OrmException( |
|
233
|
|
|
sprintf('page with layout could not be returned, though page was found: "%s"', $entityId) |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $entity; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: