Completed
Push — develop ( e53d5b...127c8a )
by Jens
03:01
created

HandlebarsEnvironment::compile()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 11
nc 3
nop 1
crap 2
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 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 = [])
112
    {
113 2
        $renderer = $this->loadTemplate($name);
114
115 2
        $templateProfile = new \Twig_Profiler_Profile($name, \Twig_Profiler_Profile::TEMPLATE, $name);
116 2
        $this->profiler->enter($templateProfile);
117 2
        $html = $renderer($context, ['helpers' => $this->helper->getHelpers()]);
118 2
        $this->profiler->leave($templateProfile);
119
120 2
        return $html;
121
    }
122
123 12
    public function getCacheFilename($name)
124
    {
125 12
        $key = $this->cache->generateKey($name);
126
127 12
        return !$key ? false : $key;
128
    }
129
130 5
    public function getLoader()
131
    {
132 5
        return $this->loader;
133
    }
134
135
    /**
136
     * @param $templateName
137
     * @return callable
138
     */
139 7
    public function loadTemplate($templateName)
140
    {
141 7
        $name = (string) $templateName;
142 7
        $cacheKey = $this->getCacheFilename($name);
143 7
        if (!$this->isAutoReload() && file_exists($cacheKey)) {
144 1
            return $this->cache->load($cacheKey);
145 6
        } else if ($this->isAutoReload() && $this->cache->isFresh($cacheKey)) {
146 2
            return $this->cache->load($cacheKey);
147
        }
148 4
        $this->compile($name);
149
150 4
        return $this->cache->load($cacheKey);
151
    }
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()
159
    {
160 7
        return $this->autoReload;
161
    }
162
}
163