1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Template\Loader; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Template\IBuilder; |
8
|
|
|
use AbterPhp\Framework\Template\IData; |
9
|
|
|
use AbterPhp\Framework\Template\ILoader; |
10
|
|
|
use AbterPhp\Framework\Template\ParsedTemplate; |
11
|
|
|
use AbterPhp\Website\Databases\Queries\PageCategoryCache; |
12
|
|
|
use AbterPhp\Website\Domain\Entities\Page; |
13
|
|
|
use AbterPhp\Website\Orm\PageRepo; |
14
|
|
|
|
15
|
|
|
class PageCategory implements ILoader |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var PageRepo |
19
|
|
|
*/ |
20
|
|
|
protected $pageRepo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PageCategoryCache |
24
|
|
|
*/ |
25
|
|
|
protected $pageCategoryCache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var IBuilder[] |
29
|
|
|
*/ |
30
|
|
|
protected $builders; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* PageCategoryLoader constructor. |
34
|
|
|
* |
35
|
|
|
* @param PageRepo $pageRepo |
36
|
|
|
* @param PageCategoryCache $blockCache |
37
|
|
|
* @param IBuilder[] $builders |
38
|
|
|
*/ |
39
|
|
|
public function __construct(PageRepo $pageRepo, PageCategoryCache $pageCategoryCache, array $builders) |
40
|
|
|
{ |
41
|
|
|
$this->pageRepo = $pageRepo; |
42
|
|
|
$this->pageCategoryCache = $pageCategoryCache; |
43
|
|
|
$this->builders = $builders; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ParsedTemplate[][] $parsedTemplates |
48
|
|
|
* |
49
|
|
|
* @return IData[] |
50
|
|
|
*/ |
51
|
|
|
public function load(array $parsedTemplates): array |
52
|
|
|
{ |
53
|
|
|
$identifiers = array_keys($parsedTemplates); |
54
|
|
|
|
55
|
|
|
$pages = $this->pageRepo->getByCategoryIdentifiers($identifiers); |
56
|
|
|
|
57
|
|
|
$groupedPages = $this->groupPages($pages); |
58
|
|
|
|
59
|
|
|
$templateData = $this->createTemplateData($parsedTemplates, $groupedPages); |
60
|
|
|
|
61
|
|
|
return $templateData; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ParsedTemplate[][] $parsedTemplates |
66
|
|
|
* @param Page[][] $groupedPages |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
protected function createTemplateData(array $parsedTemplates, array $groupedPages): array |
71
|
|
|
{ |
72
|
|
|
$templateData = []; |
73
|
|
|
foreach ($parsedTemplates as $identifier => $identifierTemplates) { |
74
|
|
|
foreach ($identifierTemplates as $parsedTemplate) { |
75
|
|
|
if (!array_key_exists($identifier, $groupedPages)) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$pages = $groupedPages[$identifier]; |
80
|
|
|
|
81
|
|
|
$builderName = $parsedTemplate->getAttribute('builder'); |
82
|
|
|
if ($builderName && array_key_exists($builderName, $this->builders)) { |
83
|
|
|
$templateData[] = $this->builders[$builderName]->build($pages); |
84
|
|
|
|
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$builder = reset($this->builders); |
89
|
|
|
|
90
|
|
|
$templateData[] = $builder->build($pages); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $templateData; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $pages |
99
|
|
|
* |
100
|
|
|
* @return Page[][] |
101
|
|
|
*/ |
102
|
|
|
protected function groupPages(array $pages): array |
103
|
|
|
{ |
104
|
|
|
$groupedPages = []; |
105
|
|
|
foreach ($pages as $page) { |
106
|
|
|
$groupedPages[$page->getCategory()->getIdentifier()][] = $page; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $groupedPages; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string[] $identifiers |
114
|
|
|
* @param string $cacheTime |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool |
119
|
|
|
{ |
120
|
|
|
return $this->pageCategoryCache->hasAnyChangedSince($identifiers, $cacheTime); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|