Completed
Push — develop ( 87f5bf...c17c7b )
by Jens
03:04
created

HandlebarsEnvironment   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 96.3%

Importance

Changes 12
Bugs 2 Features 2
Metric Value
wmc 17
c 12
b 2
f 2
lcom 1
cbo 8
dl 0
loc 125
ccs 52
cts 54
cp 0.963
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 35 5
A compile() 0 16 2
A render() 0 11 1
A getCacheFilename() 0 6 2
A getLoader() 0 4 1
B loadTemplate() 0 13 5
A isAutoReload() 0 4 1
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
    protected $options;
19
20
    /**
21
     * @var Filesystem
22
     */
23
    protected $cache;
24
    /**
25
     * @var FilesystemLoader
26
     */
27
    protected $loader;
28
29
    protected $extensions = [];
30
    protected $autoReload;
31
    protected $debug;
32
    private $profiler;
33
34
    private $helper;
35
36 11
    public function __construct(
37
        FilesystemLoader $loader,
38
        HandlebarsHelper $helper,
39
        $options = [],
40
        Filesystem $cache,
41
        HandlebarsProfileExtension $profiler
42
    )
43
    {
44 11
        $this->loader = $loader;
45 11
        $this->partials = $partials = new \ArrayObject();
0 ignored issues
show
Bug introduced by
The property partials does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46 11
        $this->helper = $helper;
47
48 11
        $this->options = array_merge([
49 11
            'auto_reload' => null,
50
            'debug' => true,
51 11
            'helpers' => $helper->getHelperMethods(),
52 11
            'partialresolver' => function ($cx, $name) use ($loader, &$partials) {
53 2
                $extension = false;
54 2
                if ($loader->exists($name . '.handlebars')) {
55
                    $extension = '.handlebars';
56 2
                } else if ($loader->exists($name . '.hbs')) {
57 2
                    $extension = '.hbs';
58
                }
59 2
                if ($extension === false) {
60
                    return null;
61
                }
62 2
                $partials[] = new FileResource($loader->getCacheKey($name . $extension));
63 2
                return $loader->getSource($name . $extension);
64 11
            },
65
        ], $options);
66 11
        $this->debug = (bool) $this->options['debug'];
67 11
        $this->autoReload = null === $this->options['auto_reload'] ? $this->debug : (bool) $this->options['auto_reload'];
68 11
        $this->cache = $cache;
69 11
        $this->profiler = $profiler;
70 11
    }
71
72 4
    public function compile($name)
73
    {
74 4
        $source = $this->getLoader()->getSource($name);
75 4
        $cacheKey = $this->getCacheFilename($name);
76
77 4
        $phpStr = '';
78
        try {
79 4
            $this->partials->exchangeArray([new FileResource($this->getLoader()->getCacheKey($name))]);
80 4
            $phpStr = LightnCandy::compile($source, $this->options);
81
        } catch (\Exception $e) {
82
            throw new LoaderException($e->getMessage());
83
        }
84 4
        $this->cache->write($cacheKey, '<?php // ' . $name . PHP_EOL . $phpStr, $this->partials->getArrayCopy());
85
86 4
        return $phpStr;
87
    }
88
89 2
    public function render($name, array $context = [])
90
    {
91 2
        $renderer = $this->loadTemplate($name);
92
93 2
        $templateProfile = new \Twig_Profiler_Profile($name, \Twig_Profiler_Profile::TEMPLATE, $name);
94 2
        $this->profiler->enter($templateProfile);
95 2
        $html = $renderer($context, ['helpers' => $this->helper->getHelpers()]);
96 2
        $this->profiler->leave($templateProfile);
97
98 2
        return $html;
99
    }
100
101 11
    public function getCacheFilename($name)
102
    {
103 11
        $key = $this->cache->generateKey($name);
104
105 11
        return !$key ? false : $key;
106
    }
107
108 4
    public function getLoader()
109
    {
110 4
        return $this->loader;
111
    }
112
113
    /**
114
     * @param $templateName
115
     * @return callable
116
     */
117 7
    public function loadTemplate($templateName)
118
    {
119 7
        $name = (string)$templateName;
120 7
        $cacheKey = $this->getCacheFilename($name);
121 7
        if (!$this->isAutoReload() && file_exists($cacheKey)) {
122 1
            return $this->cache->load($cacheKey);
123 6
        } else if ($this->isAutoReload() && $this->cache->isFresh($cacheKey)) {
124 2
            return $this->cache->load($cacheKey);
125
        }
126 4
        $this->compile($name);
127
128 4
        return $this->cache->load($cacheKey);
129
    }
130
131
    /**
132
     * Checks if the auto_reload option is enabled.
133
     *
134
     * @return bool true if auto_reload is enabled, false otherwise
135
     */
136 7
    public function isAutoReload()
137
    {
138 7
        return $this->autoReload;
139
    }
140
}
141