|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Service\Execute; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
|
8
|
|
|
use AbterPhp\Framework\Http\Service\Execute\RepoServiceAbstract; |
|
9
|
|
|
use AbterPhp\Website\Domain\Entities\Page as Entity; |
|
10
|
|
|
use AbterPhp\Website\Domain\Entities\Page\Assets; |
|
11
|
|
|
use AbterPhp\Website\Domain\Entities\Page\Meta; |
|
12
|
|
|
use AbterPhp\Website\Domain\Entities\PageCategory; |
|
|
|
|
|
|
13
|
|
|
use AbterPhp\Website\Orm\PageRepo as GridRepo; |
|
14
|
|
|
use AbterPhp\Website\Validation\Factory\Page as ValidatorFactory; |
|
15
|
|
|
use Cocur\Slugify\Slugify; |
|
16
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
|
17
|
|
|
use Opulence\Http\Requests\UploadedFile; |
|
18
|
|
|
use Opulence\Orm\IUnitOfWork; |
|
19
|
|
|
|
|
20
|
|
|
class Page extends RepoServiceAbstract |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Slugify */ |
|
23
|
|
|
protected $slugify; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Page constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param GridRepo $repo |
|
29
|
|
|
* @param ValidatorFactory $validatorFactory |
|
30
|
|
|
* @param IUnitOfWork $unitOfWork |
|
31
|
|
|
* @param IEventDispatcher $eventDispatcher |
|
32
|
|
|
* @param Slugify $slugify |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
GridRepo $repo, |
|
36
|
|
|
ValidatorFactory $validatorFactory, |
|
37
|
|
|
IUnitOfWork $unitOfWork, |
|
38
|
|
|
IEventDispatcher $eventDispatcher, |
|
39
|
|
|
Slugify $slugify |
|
40
|
|
|
) { |
|
41
|
|
|
parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher); |
|
42
|
|
|
|
|
43
|
|
|
$this->slugify = $slugify; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $entityId |
|
48
|
|
|
* |
|
49
|
|
|
* @return Entity |
|
50
|
|
|
*/ |
|
51
|
|
|
public function createEntity(string $entityId): IStringerEntity |
|
52
|
|
|
{ |
|
53
|
|
|
return new Entity($entityId, '', '', ''); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
58
|
|
|
* |
|
59
|
|
|
* @param Entity $entity |
|
60
|
|
|
* @param array $postData |
|
61
|
|
|
* @param UploadedFile[] $fileData |
|
62
|
|
|
* |
|
63
|
|
|
* @return Entity |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity |
|
66
|
|
|
{ |
|
67
|
|
|
if (!($entity instanceof Entity)) { |
|
68
|
|
|
throw new \InvalidArgumentException('Not a page...'); |
|
69
|
|
|
} |
|
70
|
|
|
$title = (string)$postData['title']; |
|
71
|
|
|
|
|
72
|
|
|
$identifier = (string)$postData['identifier']; |
|
73
|
|
|
if (empty($identifier)) { |
|
74
|
|
|
$identifier = $title; |
|
75
|
|
|
} |
|
76
|
|
|
$identifier = $this->slugify->slugify($identifier); |
|
77
|
|
|
|
|
78
|
|
|
$category = null; |
|
79
|
|
|
if (!empty($postData['category_id'])) { |
|
80
|
|
|
$category = new PageCategory((string)$postData['category_id'], '', ''); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$body = (string)$postData['body']; |
|
84
|
|
|
|
|
85
|
|
|
$layoutId = (string)$postData['layout_id']; |
|
86
|
|
|
$layout = ''; |
|
87
|
|
|
if (!$layoutId) { |
|
88
|
|
|
$layoutId = null; |
|
89
|
|
|
$layout = (string)$postData['layout']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$meta = $this->getMeta($postData); |
|
93
|
|
|
$assets = $this->getAssets($postData); |
|
94
|
|
|
|
|
95
|
|
|
$entity |
|
96
|
|
|
->setIdentifier($identifier) |
|
97
|
|
|
->setTitle($title) |
|
98
|
|
|
->setCategory($category) |
|
99
|
|
|
->setBody($body) |
|
100
|
|
|
->setLayoutId($layoutId) |
|
101
|
|
|
->setLayout($layout) |
|
102
|
|
|
->setMeta($meta) |
|
103
|
|
|
->setAssets($assets); |
|
104
|
|
|
|
|
105
|
|
|
return $entity; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param array $postData |
|
110
|
|
|
* |
|
111
|
|
|
* @return Meta |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getMeta(array $postData): Meta |
|
114
|
|
|
{ |
|
115
|
|
|
$entity = new Meta( |
|
116
|
|
|
$postData['description'], |
|
117
|
|
|
$postData['robots'], |
|
118
|
|
|
$postData['author'], |
|
119
|
|
|
$postData['copyright'], |
|
120
|
|
|
$postData['keywords'], |
|
121
|
|
|
$postData['og-title'], |
|
122
|
|
|
$postData['og-image'], |
|
123
|
|
|
$postData['og-description'] |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
return $entity; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param array $postData |
|
131
|
|
|
* |
|
132
|
|
|
* @return Assets |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getAssets(array $postData): Assets |
|
135
|
|
|
{ |
|
136
|
|
|
if (is_string($postData['css-files'])) { |
|
137
|
|
|
$postData['css-files'] = explode('\r\n', $postData['css-files']); |
|
138
|
|
|
} |
|
139
|
|
|
if (is_string($postData['js-files'])) { |
|
140
|
|
|
$postData['js-files'] = explode('\r\n', $postData['js-files']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$entity = new Assets( |
|
144
|
|
|
$postData['identifier'], |
|
145
|
|
|
$postData['header'], |
|
146
|
|
|
$postData['footer'], |
|
147
|
|
|
$postData['css-files'], |
|
148
|
|
|
$postData['js-files'], |
|
149
|
|
|
null |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
return $entity; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $entityId |
|
157
|
|
|
* |
|
158
|
|
|
* @return IStringerEntity |
|
159
|
|
|
* @throws OrmException |
|
160
|
|
|
*/ |
|
161
|
|
|
public function retrieveEntityWithLayout(string $entityId): IStringerEntity |
|
162
|
|
|
{ |
|
163
|
|
|
/** @var IStringerEntity $entity */ |
|
164
|
|
|
$entity = $this->repo->getById($entityId); |
|
165
|
|
|
|
|
166
|
|
|
$entity = $this->repo->getWithLayout($entity->getIdentifier()); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
return $entity; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
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: