This repository was archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwig.php
More file actions
331 lines (293 loc) · 9.64 KB
/
Copy pathtwig.php
File metadata and controls
331 lines (293 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace herbie\plugin\twig;
use Herbie\Page;
use herbie\plugin\twig\classes\HerbieExtension;
use Herbie\StringValue;
use Twig_Environment;
use Twig_Extension_Debug;
use Twig_Loader_Array;
use Twig_Loader_Chain;
use Twig_Loader_Filesystem;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerInterface;
class TwigPlugin extends \Herbie\Plugin
{
/** @var EventManagerInterface */
private $events;
/**
* @var \Twig_Environment
*/
private $twigEnvironment;
/**
* @var boolean
*/
private $initialized = false;
/**
* @param EventManagerInterface $events
* @param int $priority
*/
public function attach(EventManagerInterface $events, $priority = 1): void
{
$this->events = $events;
$events->attach('onPluginsInitialized', [$this, 'onPluginsInitialized'], $priority);
$events->attach('onRenderContent', [$this, 'onRenderContent'], $priority);
$events->attach('onRenderLayout', [$this, 'onRenderLayout'], $priority);
}
/**
* @param EventInterface $event
*/
public function onPluginsInitialized(EventInterface $event)
{
#$config = $this->getConfig();
// Add custom namespace path to Imagine lib
#$vendorDir = $config->get('site.path') . '/../vendor';
#$autoload = require($vendorDir . '/autoload.php');
#$autoload->add('Twig_', __DIR__ . '/vendor/twig/twig/lib');
$this->init();
}
/**
* @param EventInterface $event
*/
public function onRenderContent(EventInterface $event)
{
$twig = $event->getParam('twig');
if (empty($twig)) {
return;
}
/** @var StringValue $stringValue */
$stringValue = $event->getTarget();
$parsed = $this->renderString($stringValue->get());
$stringValue->set($parsed);
}
/**
* @param EventInterface $event
*/
public function onRenderLayout(EventInterface $event)
{
/** @var StringValue $stringValue */
$stringValue = $event->getTarget();
/** @var \Herbie\Page $page */
$page = $event->getParam('page');
$config = $this->getConfig();
$this->twigEnvironment
->getExtension(HerbieExtension::class)
->setPage($page);
$extension = trim($config->get('layouts.extension'));
$layout = empty($extension) ? $page->layout : sprintf('%s.%s', $page->layout, $extension);
$stringValue->set($this->render($layout));
}
public function init()
{
$loader = $this->getTwigFilesystemLoader();
$this->twigEnvironment = new Twig_Environment($loader, [
'debug' => $this->getConfig()->get('twig.debug'),
'cache' => $this->getConfig()->get('twig.cache')
]);
if (!$this->getConfig()->isEmpty('twig.debug')) {
$this->twigEnvironment->addExtension(new Twig_Extension_Debug());
}
$herbieExtension = new HerbieExtension(
$this->getAlias(),
$this->getConfig(),
$this->getRequest(),
$this->getUrlGenerator(),
$this->getSlugGenerator(),
$this->getAssets(),
$this->getMenuList(),
$this->getMenuTree(),
$this->getMenuRootPath(),
$this->getEnvironment(),
$this->getDataRepository(),
$this->getTranslator(),
$this
);
$this->twigEnvironment->addExtension($herbieExtension);
$this->addTwigPlugins();
/*
foreach (Hook::trigger(Hook::CONFIG, 'addTwigFunction') as $function) {
try {
list($name, $callable, $options) = $function;
$this->twigEnvironment->addFunction(new \Twig_SimpleFunction($name, $callable, (array)$options));
} catch (\Exception $e) {
; //do nothing else yet
}
}
foreach (Hook::trigger(Hook::CONFIG, 'addTwigFilter') as $filter) {
try {
list($name, $callable, $options) = $filter;
$this->twigEnvironment->addFilter(new \Twig_SimpleFilter($name, $callable, (array)$options));
} catch (\Exception $e) {
; //do nothing else yet
}
}
foreach (Hook::trigger(Hook::CONFIG, 'addTwigTest') as $test) {
try {
list($name, $callable, $options) = $test;
$this->twigEnvironment->addTest(new \Twig_SimpleTest($name, $callable, (array)$options));
} catch (\Exception $e) {
; //do nothing else yet
}
}
*/
$this->setTwig($this);
$this->initialized = true;
$this->events->trigger('onTwigInitialized', $this->twigEnvironment);
}
/**
* @param string $name
* @param array $context
* @return string
*/
public function render($name, array $context = [])
{
$context = array_merge($context, $this->getContext());
return $this->twigEnvironment->render($name, $context);
}
/**
* Renders a page content segment.
* @param string $segmentId
* @param Page $page
* @return string
*/
public function renderPageSegment(string $segmentId, Page $page)
{
/*if (is_null($page)) {
$page = $this->getPage();
}*/
$segment = $page->getSegment($segmentId);
$this->events->trigger('onRenderContent', $segment, $page->getData());
return $segment;
}
/**
* @param string $string
* @return string
*/
public function renderString($string)
{
// no rendering if empty
if (empty($string)) {
return $string;
}
// see Twig\Extensions\Twig_Extension_StringLoader
$name = '__twig_string__';
// get current loader
$loader = $this->twigEnvironment->getLoader();
// set loader chain with new array loader
$this->twigEnvironment->setLoader(new Twig_Loader_Chain([new Twig_Loader_Array([$name => $string]), $loader]));
// render string
$context = $this->getContext();
$rendered = $this->twigEnvironment->render($name, $context);
// reset current loader
$this->twigEnvironment->setLoader($loader);
return $rendered;
}
/**
* @return array
*/
private function getContext()
{
return [
'route' => $this->getEnvironment()->getRoute(),
'baseUrl' => $this->getEnvironment()->getBaseUrl(),
'theme' => $this->getConfig()->get('theme')
];
}
/**
* @return void
*/
public function addTwigPlugins()
{
if ($this->getConfig()->isEmpty('twig.extend')) {
return;
}
// Functions
$dir = $this->getConfig()->get('twig.extend.functions');
foreach ($this->readPhpFiles($dir) as $file) {
$included = $this->includePhpFile($file);
$this->twigEnvironment->addFunction($included);
}
// Filters
$dir = $this->getConfig()->get('twig.extend.filters');
foreach ($this->readPhpFiles($dir) as $file) {
$included = $this->includePhpFile($file);
$this->twigEnvironment->addFilter($included);
}
// Tests
$dir = $this->getConfig()->get('twig.extend.tests');
foreach ($this->readPhpFiles($dir) as $file) {
$included = $this->includePhpFile($file);
$this->twigEnvironment->addTest($included);
}
}
/**
* @return Twig_Loader_Filesystem
* @throws \Twig_Error_Loader
*/
private function getTwigFilesystemLoader()
{
$paths = [];
if ($this->getConfig()->isEmpty('theme')) {
$paths[] = $this->getConfig()->get('layouts.path');
} elseif ($this->getConfig()->get('theme') == 'default') {
$paths[] = $this->getConfig()->get('layouts.path') . '/default';
} else {
$paths[] = $this->getConfig()->get('layouts.path') . '/' . $this->getConfig()->get('theme');
$paths[] = $this->getConfig()->get('layouts.path') . '/default';
}
$loader = new Twig_Loader_Filesystem($paths);
// namespaces
$namespaces = [
'plugin' => $this->getConfig()->get('plugins.path'),
'page' => $this->getConfig()->get('pages.path'),
'post' => $this->getConfig()->get('posts.path'),
'site' => $this->getConfig()->get('site.path'),
'widget' => __DIR__ . '/classes/widgets'
];
foreach ($namespaces as $namespace => $path) {
if (is_readable($path)) {
$loader->addPath($path, $namespace);
}
}
return $loader;
}
/**
* @param string $file
* @return string
*/
private function includePhpFile($file)
{
return include($file);
}
/**
* @param string $dir
* @return array
*/
private function readPhpFiles($dir)
{
$dir = rtrim($dir, '/');
if (empty($dir) || !is_readable($dir)) {
return [];
}
$pattern = $dir . '/*.php';
return glob($pattern);
}
/**
* @return bool
*/
public function isInitialized()
{
return true === $this->initialized;
}
public function __debugInfo()
{
return [
'config' => call_user_func('get_object_vars', $this->getConfig()),
'environment' => call_user_func('get_object_vars', $this->twigEnvironment),
'initialized' => $this->initialized
];
}
public function getTwigEnvironment()
{
return $this->twigEnvironment;
}
}