Completed
Push — develop ( 64e374...a38615 )
by Jens
04:00
created

HandlebarsEnvironment::getTemplateExtension()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3.0416
1
<?php
2
/**
3
 * @author @jayS-de <[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 12
    public function __construct(
59
        FilesystemLoader $loader,
60
        HandlebarsHelperServiceInterface $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' => $this->helper->getHelperMethods(),
74 12
            'partialresolver' => function($cx, $name) use ($loader, &$partials) {
75 3
                $extension = $this->getTemplateExtension($loader, $name);
76 2
                if ($extension === false) {
77
                    return null;
78
                }
79 2
                $partials[] = new FileResource($loader->getCacheKey($name.$extension));
80 2
                return $loader->getSource($name.$extension);
81 12
            },
82
        ], $options);
83 12
        $this->debug = (bool) $this->options['debug'];
84 12
        $this->autoReload = null === $this->options['auto_reload'] ? $this->debug : (bool) $this->options['auto_reload'];
85 12
        $this->cache = $cache;
86 12
        $this->profiler = $profiler;
87 12
    }
88
89 3
    private function getTemplateExtension(FilesystemLoader $loader, $name) {
90 3
        if ($loader->exists($name.'.handlebars')) {
91 2
            return '.handlebars';
92
        }
93 2
        if ($loader->exists($name.'.hbs')) {
94 2
            return '.hbs';
95
        }
96
        return false;
97
    }
98
99 7
    public function compile($name)
100
    {
101 7
        $source = $this->getLoader()->getSource($name);
102 7
        $cacheKey = $this->getCacheFilename($name);
103
104 7
        $phpStr = '';
105
        try {
106 7
            $this->partials->exchangeArray([new FileResource($this->getLoader()->getCacheKey($name))]);
107 7
            $phpStr = LightnCandy::compile($source, $this->options);
108 1
        } catch (\Exception $e) {
109 1
            throw new LoaderException($e->getMessage());
110
        }
111 6
        $this->cache->write($cacheKey, '<?php // '.$name.PHP_EOL.$phpStr, $this->partials->getArrayCopy());
112
113 6
        return $phpStr;
114
    }
115
116 2
    public function render($name, array $context = [])
117
    {
118 2
        $renderer = $this->loadTemplate($name);
119
120 2
        $templateProfile = new \Twig_Profiler_Profile($name, \Twig_Profiler_Profile::TEMPLATE, $name);
121 2
        $this->profiler->enter($templateProfile);
122 2
        $html = $renderer($context, ['helpers' => $this->helper->getHelpers()]);
123 2
        $this->profiler->leave($templateProfile);
124
125 2
        return $html;
126
    }
127
128 12
    public function getCacheFilename($name)
129
    {
130 12
        $key = $this->cache->generateKey($name);
131
132 12
        return !$key ? false : $key;
133
    }
134
135 7
    public function getLoader()
136
    {
137 7
        return $this->loader;
138
    }
139
140
    /**
141
     * @param $templateName
142
     * @return callable
143
     */
144 7
    public function loadTemplate($templateName)
145
    {
146 7
        $name = (string) $templateName;
147 7
        $cacheKey = $this->getCacheFilename($name);
148
149 7
        if (!file_exists($cacheKey) || $this->isAutoReload() && !$this->cache->isFresh($cacheKey)) {
150 6
            $this->compile($name);
151
        }
152
153 7
        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