1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Template; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Config\Provider as ConfigProvider; |
8
|
|
|
|
9
|
|
|
class Engine |
10
|
|
|
{ |
11
|
|
|
const ERROR_MSG_CACHING_FAILURE = 'Caching failure'; |
12
|
|
|
|
13
|
|
|
const ERROR_INVALID_LOADER = 'Loaders must be an instance of %s'; |
14
|
|
|
|
15
|
|
|
/** @var ILoader[] */ |
16
|
|
|
protected $loaders = []; |
17
|
|
|
|
18
|
|
|
/** @var string[] */ |
19
|
|
|
protected $templateTypes = []; |
20
|
|
|
|
21
|
|
|
/** @var string[][] */ |
22
|
|
|
protected $allSubTemplateIds = []; |
23
|
|
|
|
24
|
|
|
/** @var Renderer */ |
25
|
|
|
protected $renderer; |
26
|
|
|
|
27
|
|
|
/** @var CacheManager */ |
28
|
|
|
protected $cacheManager; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $isCacheAllowed; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Engine constructor. |
35
|
|
|
* |
36
|
|
|
* @param Renderer $renderer |
37
|
|
|
* @param CacheManager $cache |
38
|
|
|
* @param ConfigProvider $configProvider |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Renderer $renderer, CacheManager $cache, ConfigProvider $configProvider) |
41
|
|
|
{ |
42
|
|
|
$this->renderer = $renderer; |
43
|
|
|
$this->cacheManager = $cache; |
44
|
|
|
$this->isCacheAllowed = $configProvider->isCacheAllowed(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Renders a list of templates |
49
|
|
|
* previously rendered templates can be referenced as variables |
50
|
|
|
* the last template rendered will be returned as a string |
51
|
|
|
* |
52
|
|
|
* @param string $type |
53
|
|
|
* @param string $documentId |
54
|
|
|
* @param string[] $templates |
55
|
|
|
* @param string[] $vars |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function run(string $type, string $documentId, array $templates, array $vars): string |
60
|
|
|
{ |
61
|
|
|
$cacheId = md5($type . '/' . $documentId); |
62
|
|
|
|
63
|
|
|
if ($this->hasValidCache($cacheId)) { |
64
|
|
|
return $this->cacheManager->getDocument($cacheId); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->allSubTemplateIds = []; |
68
|
|
|
|
69
|
|
|
$content = ''; |
70
|
|
|
foreach ($templates as $key => $template) { |
71
|
|
|
$content = $this->renderer->render($template, $vars); |
72
|
|
|
$vars[$key] = $content; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->updateCache($cacheId, $content); |
76
|
|
|
|
77
|
|
|
return $content; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Renderer |
82
|
|
|
*/ |
83
|
|
|
public function getRenderer() |
84
|
|
|
{ |
85
|
|
|
return $this->renderer; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $cacheId |
90
|
|
|
* @param string $content |
91
|
|
|
*/ |
92
|
|
|
protected function updateCache(string $cacheId, string $content): void |
93
|
|
|
{ |
94
|
|
|
if (!$this->isCacheAllowed) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$this->cacheManager->storeCacheData($cacheId, $this->allSubTemplateIds)) { |
99
|
|
|
throw new Exception(static::ERROR_MSG_CACHING_FAILURE); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!$this->cacheManager->storeDocument($cacheId, $content)) { |
103
|
|
|
throw new Exception(static::ERROR_MSG_CACHING_FAILURE); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $cacheId |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
protected function hasValidCache(string $cacheId): bool |
113
|
|
|
{ |
114
|
|
|
if (!$this->isCacheAllowed) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$cacheData = $this->cacheManager->getCacheData($cacheId); |
119
|
|
|
if ($cacheData === null) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->renderer->hasAllValidLoaders($cacheData->getSubTemplates(), $cacheData->getDate()); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|