1 | <?php |
||
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 HandlebarsHelper |
||
50 | */ |
||
51 | private $helper; |
||
52 | |||
53 | /** |
||
54 | * @var \ArrayObject |
||
55 | */ |
||
56 | private $partials; |
||
57 | |||
58 | 12 | public function __construct( |
|
59 | FilesystemLoader $loader, |
||
60 | HandlebarsHelper $helper, |
||
61 | $options = [], |
||
62 | Filesystem $cache, |
||
63 | HandlebarsProfileExtension $profiler |
||
64 | ) |
||
65 | { |
||
66 | 12 | $this->loader = $loader; |
|
67 | 12 | $this->partials = $partials = new \ArrayObject(); |
|
68 | 12 | $this->helper = $helper; |
|
69 | |||
70 | 12 | $this->options = array_merge([ |
|
71 | 12 | 'auto_reload' => null, |
|
72 | 'debug' => true, |
||
73 | 12 | 'helpers' => $helper->getHelperMethods(), |
|
74 | 12 | 'partialresolver' => function($cx, $name) use ($loader, &$partials) { |
|
75 | 3 | $extension = false; |
|
76 | 3 | if ($loader->exists($name.'.handlebars')) { |
|
77 | $extension = '.handlebars'; |
||
78 | 2 | } else if ($loader->exists($name.'.hbs')) { |
|
79 | 2 | $extension = '.hbs'; |
|
80 | } |
||
81 | 2 | if ($extension === false) { |
|
82 | return null; |
||
83 | } |
||
84 | 2 | $partials[] = new FileResource($loader->getCacheKey($name.$extension)); |
|
85 | 2 | return $loader->getSource($name.$extension); |
|
86 | 12 | }, |
|
87 | ], $options); |
||
88 | 12 | $this->debug = (bool) $this->options['debug']; |
|
89 | 12 | $this->autoReload = null === $this->options['auto_reload'] ? $this->debug : (bool) $this->options['auto_reload']; |
|
90 | 12 | $this->cache = $cache; |
|
91 | 12 | $this->profiler = $profiler; |
|
92 | 12 | } |
|
93 | |||
94 | 5 | public function compile($name) |
|
95 | { |
||
96 | 5 | $source = $this->getLoader()->getSource($name); |
|
97 | 5 | $cacheKey = $this->getCacheFilename($name); |
|
98 | |||
99 | 5 | $phpStr = ''; |
|
100 | try { |
||
101 | 5 | $this->partials->exchangeArray([new FileResource($this->getLoader()->getCacheKey($name))]); |
|
102 | 5 | $phpStr = LightnCandy::compile($source, $this->options); |
|
103 | 1 | } catch (\Exception $e) { |
|
104 | 1 | throw new LoaderException($e->getMessage()); |
|
105 | } |
||
106 | 4 | $this->cache->write($cacheKey, '<?php // '.$name.PHP_EOL.$phpStr, $this->partials->getArrayCopy()); |
|
107 | |||
108 | 4 | return $phpStr; |
|
109 | } |
||
110 | |||
111 | 2 | public function render($name, array $context = []) |
|
122 | |||
123 | 12 | public function getCacheFilename($name) |
|
129 | |||
130 | 5 | public function getLoader() |
|
134 | |||
135 | /** |
||
136 | * @param $templateName |
||
137 | * @return callable |
||
138 | */ |
||
139 | 7 | public function loadTemplate($templateName) |
|
152 | |||
153 | /** |
||
154 | * Checks if the auto_reload option is enabled. |
||
155 | * |
||
156 | * @return bool true if auto_reload is enabled, false otherwise |
||
157 | */ |
||
158 | 7 | public function isAutoReload() |
|
162 | } |
||
163 |