|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jenschude <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
namespace JaySDe\HandlebarsBundle; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
use JaySDe\HandlebarsBundle\Cache\Filesystem; |
|
11
|
|
|
use JaySDe\HandlebarsBundle\Error\LoaderException; |
|
12
|
|
|
use JaySDe\HandlebarsBundle\Loader\FilesystemLoader; |
|
13
|
|
|
use LightnCandy\LightnCandy; |
|
14
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
|
15
|
|
|
|
|
16
|
|
|
class HandlebarsEnvironment |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $options; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Filesystem |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cache; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var FilesystemLoader |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $loader; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $autoReload; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $debug; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var HandlebarsProfileExtension |
|
45
|
|
|
*/ |
|
46
|
|
|
private $profiler; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var HandlebarsHelperServiceInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $helper; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var \ArrayObject |
|
55
|
|
|
*/ |
|
56
|
|
|
private $partials; |
|
57
|
|
|
|
|
58
|
13 |
|
public function __construct( |
|
59
|
|
|
FilesystemLoader $loader, |
|
60
|
|
|
HandlebarsHelperServiceInterface $helper, |
|
61
|
|
|
$options = [], |
|
62
|
|
|
Filesystem $cache, |
|
63
|
|
|
HandlebarsProfileExtension $profiler |
|
64
|
|
|
) |
|
65
|
|
|
{ |
|
66
|
13 |
|
$this->loader = $loader; |
|
67
|
13 |
|
$this->partials = $partials = new \ArrayObject(); |
|
68
|
13 |
|
$this->helper = $helper; |
|
69
|
|
|
|
|
70
|
13 |
|
$this->options = array_merge([ |
|
71
|
13 |
|
'auto_reload' => null, |
|
72
|
|
|
'debug' => true, |
|
73
|
13 |
|
'helpers' => $this->helper->getHelperMethods(), |
|
74
|
13 |
|
'partialresolver' => function($cx, $name) use ($loader, &$partials) { |
|
75
|
4 |
|
$extension = $this->getTemplateExtension($loader, $name); |
|
76
|
3 |
|
if ($extension === false) { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
3 |
|
$partials[] = new FileResource($loader->getCacheKey($name.$extension)); |
|
80
|
3 |
|
return $loader->getSource($name.$extension); |
|
81
|
13 |
|
}, |
|
82
|
13 |
|
], $options); |
|
83
|
13 |
|
$this->debug = (bool) $this->options['debug']; |
|
84
|
13 |
|
$this->autoReload = null === $this->options['auto_reload'] ? $this->debug : (bool) $this->options['auto_reload']; |
|
85
|
13 |
|
$this->cache = $cache; |
|
86
|
13 |
|
$this->profiler = $profiler; |
|
87
|
13 |
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
private function getTemplateExtension(FilesystemLoader $loader, $name) { |
|
90
|
4 |
|
if ($loader->exists($name.'.handlebars')) { |
|
91
|
3 |
|
return '.handlebars'; |
|
92
|
|
|
} |
|
93
|
3 |
|
if ($loader->exists($name.'.hbs')) { |
|
94
|
3 |
|
return '.hbs'; |
|
95
|
|
|
} |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
8 |
|
public function compile($name) |
|
100
|
|
|
{ |
|
101
|
8 |
|
$source = $this->getLoader()->getSource($name); |
|
102
|
8 |
|
$cacheKey = $this->getCacheFilename($name); |
|
103
|
|
|
|
|
104
|
8 |
|
$phpStr = ''; |
|
105
|
|
|
try { |
|
106
|
8 |
|
$this->partials->exchangeArray([new FileResource($this->getLoader()->getCacheKey($name))]); |
|
107
|
8 |
|
$phpStr = LightnCandy::compile($source, $this->options); |
|
108
|
1 |
|
} catch (\Exception $e) { |
|
109
|
1 |
|
throw new LoaderException($e->getMessage()); |
|
110
|
|
|
} |
|
111
|
7 |
|
$this->cache->write($cacheKey, '<?php // '.$name.PHP_EOL.$phpStr, $this->partials->getArrayCopy()); |
|
112
|
|
|
|
|
113
|
7 |
|
return $phpStr; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
public function render($name, array $context = []) |
|
117
|
|
|
{ |
|
118
|
3 |
|
$renderer = $this->loadTemplate($name); |
|
119
|
|
|
|
|
120
|
3 |
|
$templateProfile = new \Twig_Profiler_Profile($name, \Twig_Profiler_Profile::TEMPLATE, $name); |
|
121
|
3 |
|
$this->profiler->enter($templateProfile); |
|
122
|
3 |
|
$html = $renderer($context, ['helpers' => $this->helper->getHelpers()]); |
|
123
|
3 |
|
$this->profiler->leave($templateProfile); |
|
124
|
|
|
|
|
125
|
3 |
|
return $html; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
13 |
|
public function getCacheFilename($name) |
|
129
|
|
|
{ |
|
130
|
13 |
|
$key = $this->cache->generateKey($name); |
|
131
|
|
|
|
|
132
|
13 |
|
return !$key ? false : $key; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
8 |
|
public function getLoader() |
|
136
|
|
|
{ |
|
137
|
8 |
|
return $this->loader; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $templateName |
|
142
|
|
|
* @return callable |
|
143
|
|
|
*/ |
|
144
|
8 |
|
public function loadTemplate($templateName) |
|
145
|
|
|
{ |
|
146
|
8 |
|
$name = (string) $templateName; |
|
147
|
8 |
|
$cacheKey = $this->getCacheFilename($name); |
|
148
|
|
|
|
|
149
|
8 |
|
if (!file_exists($cacheKey) || $this->isAutoReload() && !$this->cache->isFresh($cacheKey)) { |
|
150
|
7 |
|
$this->compile($name); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
8 |
|
return $this->cache->load($cacheKey); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Checks if the auto_reload option is enabled. |
|
158
|
|
|
* |
|
159
|
|
|
* @return bool true if auto_reload is enabled, false otherwise |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function isAutoReload() |
|
162
|
|
|
{ |
|
163
|
1 |
|
return $this->autoReload; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|