|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Http\Controllers\Api; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Config\Provider as ConfigProvider; |
|
8
|
|
|
use AbterPhp\Framework\Databases\Queries\FoundRows; |
|
9
|
|
|
use AbterPhp\Framework\Http\Controllers\ApiAbstract; |
|
10
|
|
|
use AbterPhp\Framework\Template\Engine as TemplateEngine; |
|
11
|
|
|
use AbterPhp\Website\Domain\Entities\Page as Entity; |
|
12
|
|
|
use AbterPhp\Website\Service\Execute\Page as RepoService; |
|
13
|
|
|
use Opulence\Http\Responses\Response; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
|
|
16
|
|
|
class Page extends ApiAbstract |
|
17
|
|
|
{ |
|
18
|
|
|
const ENTITY_SINGULAR = 'page'; |
|
19
|
|
|
const ENTITY_PLURAL = 'pages'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var TemplateEngine */ |
|
22
|
|
|
protected $templateEngine; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Page constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param LoggerInterface $logger |
|
28
|
|
|
* @param RepoService $repoService |
|
29
|
|
|
* @param FoundRows $foundRows |
|
30
|
|
|
* @param ConfigProvider $configProvider |
|
31
|
|
|
* @param TemplateEngine $templateEngine |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
LoggerInterface $logger, |
|
35
|
|
|
RepoService $repoService, |
|
36
|
|
|
FoundRows $foundRows, |
|
37
|
|
|
ConfigProvider $configProvider, |
|
38
|
|
|
TemplateEngine $templateEngine |
|
39
|
|
|
) { |
|
40
|
|
|
parent::__construct($logger, $repoService, $foundRows, $configProvider); |
|
41
|
|
|
|
|
42
|
|
|
$this->templateEngine = $templateEngine; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $entityId |
|
47
|
|
|
* |
|
48
|
|
|
* @return Response |
|
49
|
|
|
*/ |
|
50
|
|
|
public function get(string $entityId): Response |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->request->getQuery()->get('embed') === 'rendered') { |
|
53
|
|
|
return $this->getWithRendered($entityId); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return parent::get($entityId); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $entityId |
|
61
|
|
|
* |
|
62
|
|
|
* @return Response |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getWithRendered(string $entityId): Response |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
|
|
/** @var Entity $entity */ |
|
68
|
|
|
$entity = $this->repoService->retrieveEntityWithLayout($entityId); |
|
69
|
|
|
|
|
70
|
|
|
$vars = ['title' => $entity->getTitle()]; |
|
71
|
|
|
$templates = [ |
|
72
|
|
|
'body' => $entity->getBody(), |
|
73
|
|
|
'layout' => $entity->getLayout(), |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
$renderedBody = $this->templateEngine->run('page', $entity->getIdentifier(), $templates, $vars); |
|
77
|
|
|
|
|
78
|
|
|
$entity->setRenderedBody($renderedBody); |
|
79
|
|
|
} catch (\Exception $e) { |
|
80
|
|
|
$msg = sprintf(static::LOG_MSG_GET_FAILURE, static::ENTITY_SINGULAR, $entityId); |
|
81
|
|
|
|
|
82
|
|
|
return $this->handleException($msg, $e); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->handleGetSuccess($entity); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getSharedData(): array |
|
92
|
|
|
{ |
|
93
|
|
|
$data = $this->request->getJsonBody(); |
|
94
|
|
|
|
|
95
|
|
|
$data['meta'] = !empty($data['meta']) ? $data['meta'] : []; |
|
96
|
|
|
$data['assets'] = !empty($data['assets']) ? $data['assets'] : []; |
|
97
|
|
|
|
|
98
|
|
|
$data = array_merge($data, $data['meta'], $data['assets']); |
|
99
|
|
|
|
|
100
|
|
|
unset($data['meta'], $data['assets']); |
|
101
|
|
|
|
|
102
|
|
|
return $data; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|