1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Template engine class |
4
|
|
|
*/ |
5
|
|
|
namespace Phile\Plugin\Phile\TemplateTwig\Template; |
6
|
|
|
|
7
|
|
|
use Phile\Core\Container; |
8
|
|
|
use Phile\Core\Registry; |
9
|
|
|
use Phile\Model\Page; |
10
|
|
|
use Phile\ServiceLocator\TemplateInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Twig |
14
|
|
|
* |
15
|
|
|
* @author PhileCMS |
16
|
|
|
* @link https://philecms.com |
17
|
|
|
* @license http://opensource.org/licenses/MIT |
18
|
|
|
* @package Phile\Plugin\Phile\TemplateTwig\Template |
19
|
|
|
*/ |
20
|
|
|
class Twig implements TemplateInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array the config for twig |
24
|
|
|
*/ |
25
|
|
|
protected $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Page the page model |
29
|
|
|
*/ |
30
|
|
|
protected $page; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string theme name |
34
|
|
|
*/ |
35
|
|
|
protected $theme; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string path to theme directory |
39
|
|
|
*/ |
40
|
|
|
protected $themesDir; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* the constructor |
44
|
|
|
* |
45
|
|
|
* @param array $config the configuration |
46
|
|
|
*/ |
47
|
28 |
|
public function __construct(array $config = []) |
48
|
|
|
{ |
49
|
28 |
|
$this->theme = $config['theme']; |
50
|
28 |
|
$this->themesDir = $config['themes_dir']; |
51
|
28 |
|
unset($config['theme'], $config['themes_dir']); |
52
|
28 |
|
$this->config = $config; |
53
|
28 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* method to set the current page |
57
|
|
|
* |
58
|
|
|
* @param Page $page the page model |
59
|
|
|
* |
60
|
|
|
* @return mixed|void |
61
|
|
|
*/ |
62
|
3 |
|
public function setCurrentPage(Page $page) |
63
|
|
|
{ |
64
|
3 |
|
$this->page = $page; |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* method to render the page/template |
69
|
|
|
* |
70
|
|
|
* @return mixed|string |
71
|
|
|
*/ |
72
|
3 |
|
public function render() |
73
|
|
|
{ |
74
|
3 |
|
$engine = $this->getEngine(); |
75
|
3 |
|
$vars = $this->getTemplateVars(); |
76
|
3 |
|
Container::getInstance()->get('Phile_EventBus')->trigger( |
77
|
3 |
|
'template_engine_registered', |
78
|
3 |
|
['engine' => &$engine, 'data' => &$vars] |
79
|
|
|
); |
80
|
3 |
|
return $this->doRender($engine, $vars); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* wrapper to call the render engine |
85
|
|
|
* |
86
|
|
|
* @param \Twig_Environment $engine |
87
|
|
|
* @param array $vars |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
3 |
|
protected function doRender(\Twig_Environment $engine, array $vars): string |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
3 |
|
$template = $this->getTemplateFileName(); |
94
|
|
|
} catch (\RuntimeException $e) { |
95
|
|
|
return $e->getMessage(); |
96
|
|
|
} |
97
|
3 |
|
return $engine->render($template, $vars); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* get template engine |
102
|
|
|
* |
103
|
|
|
* @return \Twig_Environment |
104
|
|
|
*/ |
105
|
3 |
|
protected function getEngine() |
106
|
|
|
{ |
107
|
3 |
|
$loader = new \Twig_Loader_Filesystem($this->getTemplatePath()); |
108
|
3 |
|
$twig = new \Twig_Environment($loader, $this->config); |
109
|
|
|
|
110
|
|
|
// load the twig debug extension if required |
111
|
3 |
|
if (!empty($this->config['debug'])) { |
112
|
|
|
$twig->addExtension(new \Twig_Extension_Debug()); |
113
|
|
|
} |
114
|
3 |
|
return $twig; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* get template file name |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
* @throws \RuntimeException |
122
|
|
|
*/ |
123
|
3 |
|
protected function getTemplateFileName(): string |
124
|
|
|
{ |
125
|
3 |
|
$template = $this->page->getMeta()->get('template'); |
126
|
3 |
|
if (empty($template)) { |
127
|
3 |
|
$template = 'index'; |
128
|
|
|
} |
129
|
3 |
|
if (!empty($this->config['template-extension'])) { |
130
|
3 |
|
$template .= '.' . $this->config['template-extension']; |
131
|
|
|
} |
132
|
3 |
|
$templatePath = $this->getTemplatePath($template); |
133
|
3 |
|
if (!file_exists($templatePath)) { |
134
|
|
|
throw new \RuntimeException( |
135
|
|
|
"Template file '{$templatePath}' not found.", |
136
|
|
|
1427990135 |
137
|
|
|
); |
138
|
|
|
} |
139
|
3 |
|
return $template; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* get file path to (sub-path) in theme-path |
144
|
|
|
* |
145
|
|
|
* @param string $sub |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
3 |
|
protected function getTemplatePath(string $sub = ''): string |
149
|
|
|
{ |
150
|
3 |
|
$themePath = $this->themesDir . $this->theme; |
151
|
3 |
|
if (!empty($sub)) { |
152
|
3 |
|
$themePath .= '/' . ltrim($sub, DIRECTORY_SEPARATOR); |
153
|
|
|
} |
154
|
3 |
|
return $themePath; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* get template vars |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
* @throws \Exception |
162
|
|
|
*/ |
163
|
3 |
|
protected function getTemplateVars(): array |
164
|
|
|
{ |
165
|
|
|
$defaults = [ |
166
|
3 |
|
'content' => $this->page->getContent(), |
167
|
3 |
|
'meta' => $this->page->getMeta(), |
168
|
3 |
|
'current_page' => $this->page, |
169
|
3 |
|
'pages' => $this->page->getRepository()->findAll(), |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @var array $templateVars |
174
|
|
|
*/ |
175
|
3 |
|
$templateVars = Registry::get('templateVars'); |
176
|
3 |
|
$templateVars += $defaults; |
177
|
|
|
|
178
|
3 |
|
return $templateVars; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|