Passed
Push — master ( 9c0a44...c2ed37 )
by Divine Niiquaye
15:22
created

LatteRender::addProvider()   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 2
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
    /** @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->addFilter('template', [$loader, 'find'])->addFunction('template', [$loader, 'render']);
64
65 4
        return parent::withLoader($loader);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function render(string $template, array $parameters): string
72
    {
73 4
        $source = self::loadHtml($template) ?? $template;
74
75 4
        if ($source !== $template || !\file_exists($template)) {
76 3
            $templateLoader = new Latte\Loaders\StringLoader([$template => $source]);
77
        }
78
79 4
        $latte = $this->latte->setLoader($templateLoader ?? new Latte\Loaders\FileLoader());
80
81 4
        return $latte->renderToString($template, $parameters);
82
    }
83
84
    /**
85
     * Registers run-time filter.
86
     *
87
     * @return $this
88
     */
89
    public function addFilter(?string $name, callable $callback)
90
    {
91
        $this->latte->addFilter($name, $callback);
92
93
        return $this;
94
    }
95
96
    /**
97
     * Adds new macro.
98
     *
99
     * @return $this
100
     */
101
    public function addMacro(string $name, Latte\Macro $macro)
102
    {
103
        $this->latte->addMacro($name, $macro);
104
105
        return $this;
106
    }
107
108
    /**
109
     * Registers run-time function.
110
     *
111
     * @return $this
112
     */
113
    public function addFunction(string $name, callable $callback)
114
    {
115
        $this->latte->addFunction($name, $callback);
116
117
        return $this;
118
    }
119
120
    /**
121
     * Adds new provider.
122
     *
123
     * @param mixed $value
124
     *
125
     * @return $this
126
     */
127
    public function addProvider(string $name, $value)
128
    {
129
        $this->latte->addProvider($name, $value);
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return $this
136
     */
137
    public function setPolicy(?Latte\Policy $policy)
138
    {
139
        $this->latte->setPolicy($policy);
140
141
        return $this;
142
    }
143
144
    /**
145
     * @return $this
146
     */
147
    public function setExceptionHandler(callable $callback)
148
    {
149
        $this->latte->setExceptionHandler($callback);
150
151
        return $this;
152
    }
153
}
154