Passed
Push — master ( 417605...635760 )
by Divine Niiquaye
11:55
created

LatteRender::setPolicy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\UI\Renders;
19
20
use Biurad\UI\Interfaces\CacheInterface;
21
use Biurad\UI\Interfaces\RenderInterface;
22
use Biurad\UI\Template;
23
use Latte;
24
25
/**
26
 * Render for Latte templating.
27
 *
28
 * @author Divine Niiquaye Ibok <[email protected]>
29
 */
30
final class LatteRender extends AbstractRender implements CacheInterface
31
{
32
    protected const EXTENSIONS = ['latte'];
33
34
    protected const DEFAULT_TEMPLATE = 'hello.latte';
35
36
    /** @var Latte\Engine */
37
    protected $latte;
38
39
    /**
40
     * LatteEngine constructor.
41
     *
42
     * @param string[] $extensions
43
     */
44 4
    public function __construct(Latte\Engine $engine = null, array $extensions = self::EXTENSIONS)
45
    {
46 4
        $this->latte = $engine ?? new Latte\Engine();
47 4
        $this->extensions = $extensions;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 4
    public function withCache(?string $cacheDir): void
54
    {
55 4
        if (null !== $cacheDir) {
56 3
            $this->latte->setTempDirectory($cacheDir); // Replace regardless ...
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 4
    public function withLoader(Template $loader): RenderInterface
64
    {
65 4
        $this->latte->addFunction('template', static function (string $template, array $parameters = []) use ($loader): string {
66 3
            return $loader->render($template, $parameters);
67
        });
68
69 4
        return parent::withLoader($loader);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 4
    public function render(string $template, array $parameters): string
76
    {
77 4
        if (\file_exists($template)) {
78 4
            $templateLoader = new Latte\Loaders\FileLoader();
79
        } else {
80 3
            $templateId = \substr(\md5($template), 0, 7);
81 3
            $templateLoader = new StringLoader([$templateId => (\file_exists($this->latte->getCacheFile($templateId)) ? '' : self::loadHtml($template) ?? $template)]);
82
83 3
            $template = $templateId;
84
        }
85
86 4
        $this->latte->setLoader($templateLoader);
87
88 4
        return $this->latte->renderToString($template, $parameters);
89
    }
90
91
    /**
92
     * Registers run-time filter.
93
     *
94
     * @return $this
95
     */
96
    public function addFilter(?string $name, callable $callback)
97
    {
98
        $this->latte->addFilter($name, $callback);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Adds new macro.
105
     *
106
     * @return $this
107
     */
108
    public function addMacro(string $name, Latte\Macro $macro)
109
    {
110
        $this->latte->addMacro($name, $macro);
111
112
        return $this;
113
    }
114
115
    /**
116
     * Registers run-time function.
117
     *
118
     * @return $this
119
     */
120
    public function addFunction(string $name, callable $callback)
121
    {
122
        $this->latte->addFunction($name, $callback);
123
124
        return $this;
125
    }
126
127
    /**
128
     * Adds new provider.
129
     *
130
     * @param mixed $value
131
     *
132
     * @return $this
133
     */
134
    public function addProvider(string $name, $value)
135
    {
136
        $this->latte->addProvider($name, $value);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return $this
143
     */
144
    public function setPolicy(?Latte\Policy $policy)
145
    {
146
        $this->latte->setPolicy($policy);
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return $this
153
     */
154
    public function setExceptionHandler(callable $callback)
155
    {
156
        $this->latte->setExceptionHandler($callback);
157
158
        return $this;
159
    }
160
}
161
162
/**
163
 * Latte Template loader.
164
 */
165
class StringLoader extends \Latte\Loaders\StringLoader
166
{
167
    /**
168
     * {@inheritdoc}
169
     *
170
     * @param string $name
171
     */
172 3
    public function getUniqueId($name): string
173
    {
174 3
        return $name;
175
    }
176
}
177