Passed
Push — master ( 635760...191350 )
by Divine Niiquaye
11:17
created

LatteRender::render()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 3
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
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
    /** @var Latte\Engine */
35
    protected $latte;
36
37
    /**
38
     * LatteEngine constructor.
39
     *
40
     * @param string[] $extensions
41
     */
42 4
    public function __construct(Latte\Engine $engine = null, array $extensions = self::EXTENSIONS)
43
    {
44 4
        $this->latte = $engine ?? new Latte\Engine();
45 4
        $this->extensions = $extensions;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 4
    public function withCache(?string $cacheDir): void
52
    {
53 4
        if (null !== $cacheDir) {
54 3
            $this->latte->setTempDirectory($cacheDir); // Replace regardless ...
55
        }
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 4
    public function withLoader(Template $loader): RenderInterface
62
    {
63 4
        $this->latte->addFunction('template', static function (string $template, array $parameters = []) use ($loader): string {
64 3
            return $loader->render($template, $parameters);
65
        });
66
67 4
        return parent::withLoader($loader);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 4
    public function render(string $template, array $parameters): string
74
    {
75 4
        $source = self::loadHtml($template) ?? $template;
76
77 4
        if ($source !== $template || !\file_exists($template)) {
78 3
            $templateLoader = new Latte\Loaders\StringLoader([$template => $source]);
79
        }
80
81 4
        $latte = $this->latte->setLoader($templateLoader ?? new Latte\Loaders\FileLoader());
82
83 4
        return $latte->renderToString($template, $parameters);
84
    }
85
86
    /**
87
     * Registers run-time filter.
88
     *
89
     * @return $this
90
     */
91
    public function addFilter(?string $name, callable $callback)
92
    {
93
        $this->latte->addFilter($name, $callback);
94
95
        return $this;
96
    }
97
98
    /**
99
     * Adds new macro.
100
     *
101
     * @return $this
102
     */
103
    public function addMacro(string $name, Latte\Macro $macro)
104
    {
105
        $this->latte->addMacro($name, $macro);
106
107
        return $this;
108
    }
109
110
    /**
111
     * Registers run-time function.
112
     *
113
     * @return $this
114
     */
115
    public function addFunction(string $name, callable $callback)
116
    {
117
        $this->latte->addFunction($name, $callback);
118
119
        return $this;
120
    }
121
122
    /**
123
     * Adds new provider.
124
     *
125
     * @param mixed $value
126
     *
127
     * @return $this
128
     */
129
    public function addProvider(string $name, $value)
130
    {
131
        $this->latte->addProvider($name, $value);
132
133
        return $this;
134
    }
135
136
    /**
137
     * @return $this
138
     */
139
    public function setPolicy(?Latte\Policy $policy)
140
    {
141
        $this->latte->setPolicy($policy);
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return $this
148
     */
149
    public function setExceptionHandler(callable $callback)
150
    {
151
        $this->latte->setExceptionHandler($callback);
152
153
        return $this;
154
    }
155
}
156