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
|
14 |
|
public function __construct( |
59
|
|
|
FilesystemLoader $loader, |
60
|
|
|
HandlebarsHelperServiceInterface $helper, |
61
|
|
|
$options = [], |
62
|
|
|
Filesystem $cache, |
63
|
|
|
HandlebarsProfileExtension $profiler |
64
|
|
|
) |
65
|
|
|
{ |
66
|
14 |
|
$this->loader = $loader; |
67
|
14 |
|
$this->partials = $partials = new \ArrayObject(); |
68
|
14 |
|
$this->helper = $helper; |
69
|
|
|
|
70
|
14 |
|
$this->options = array_merge([ |
71
|
14 |
|
'auto_reload' => null, |
72
|
|
|
'debug' => true, |
73
|
14 |
|
'helpers' => $this->helper->getHelperMethods(), |
74
|
14 |
|
'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
|
14 |
|
}, |
82
|
14 |
|
], $options); |
83
|
14 |
|
$this->debug = (bool) $this->options['debug']; |
84
|
14 |
|
$this->autoReload = null === $this->options['auto_reload'] ? $this->debug : (bool) $this->options['auto_reload']; |
85
|
14 |
|
$this->cache = $cache; |
86
|
14 |
|
$this->profiler = $profiler; |
87
|
14 |
|
} |
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
|
9 |
|
public function compile($name) |
100
|
|
|
{ |
101
|
9 |
|
$source = $this->getLoader()->getSource($name); |
102
|
9 |
|
$cacheKey = $this->getCacheFilename($name); |
103
|
|
|
|
104
|
9 |
|
$phpStr = ''; |
105
|
|
|
try { |
106
|
9 |
|
$this->partials->exchangeArray([new FileResource($this->getLoader()->getCacheKey($name))]); |
107
|
9 |
|
$phpStr = LightnCandy::compile($source, $this->options); |
108
|
1 |
|
} catch (\Exception $e) { |
109
|
1 |
|
throw new LoaderException($e->getMessage()); |
110
|
|
|
} |
111
|
8 |
|
$this->cache->write($cacheKey, '<?php // '.$name.PHP_EOL.$phpStr, $this->partials->getArrayCopy()); |
112
|
|
|
|
113
|
8 |
|
return $phpStr; |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
public function render($name, array $context = []) |
117
|
|
|
{ |
118
|
4 |
|
$renderer = $this->loadTemplate($name); |
119
|
|
|
|
120
|
4 |
|
$templateProfile = new \Twig_Profiler_Profile($name, \Twig_Profiler_Profile::TEMPLATE, $name); |
|
|
|
|
121
|
4 |
|
$this->profiler->enter($templateProfile); |
122
|
4 |
|
$html = $renderer($context, ['helpers' => $this->helper->getHelpers()]); |
123
|
4 |
|
$this->profiler->leave($templateProfile); |
124
|
|
|
|
125
|
4 |
|
return $html; |
126
|
|
|
} |
127
|
|
|
|
128
|
14 |
|
public function getCacheFilename($name) |
129
|
|
|
{ |
130
|
14 |
|
$key = $this->cache->generateKey($name); |
131
|
|
|
|
132
|
14 |
|
return !$key ? false : $key; |
133
|
|
|
} |
134
|
|
|
|
135
|
9 |
|
public function getLoader() |
136
|
|
|
{ |
137
|
9 |
|
return $this->loader; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $templateName |
142
|
|
|
* @return callable |
143
|
|
|
*/ |
144
|
9 |
|
public function loadTemplate($templateName) |
145
|
|
|
{ |
146
|
9 |
|
$name = (string) $templateName; |
147
|
9 |
|
$cacheKey = $this->getCacheFilename($name); |
148
|
|
|
|
149
|
9 |
|
if (!file_exists($cacheKey) || $this->isAutoReload() && !$this->cache->isFresh($cacheKey)) { |
150
|
8 |
|
$this->compile($name); |
151
|
|
|
} |
152
|
|
|
|
153
|
9 |
|
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
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.