Passed
Push — master ( 590757...67ec3e )
by Divine Niiquaye
02:17
created

LatteRender::addFilter()   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 Latte;
21
use Latte\Loaders\StringLoader;
22
use Nette;
0 ignored issues
show
Bug introduced by
The type Nette was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
final class LatteRender extends AbstractRender
25
{
26
    protected const EXTENSIONS = ['latte'];
27
28
    /** @var Latte\Engine */
29
    protected $latte;
30
31
    /**
32
     * LatteEngine constructor.
33
     *
34
     * @param Latte\Engine $engine
35
     * @param string[]     $extensions
36
     */
37
    public function __construct(Latte\Engine $engine, array $extensions = self::EXTENSIONS)
38
    {
39
        $this->latte      = $engine;
40
        $this->extensions = $extensions;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function render(string $template, array $parameters): string
47
    {
48
        $parameters = \array_merge($parameters, ['view' => $this]);
49
        $source     = $this->getLoader()->find($template);
50
51
        $this->latte->setLoader(new StringLoader([
52
            $template => (string) $source->isFile() ? \file_get_contents($source) : $source,
53
        ]));
54
55
        return $this->latte->renderToString($template, $parameters);
56
    }
57
58
    /**
59
     * Registers run-time filter.
60
     *
61
     * @param null|string $name
62
     * @param callable    $callback
63
     *
64
     * @return static
65
     */
66
    public function addFilter(?string $name, callable $callback)
67
    {
68
        $this->latte->addFilter($name, $callback);
69
70
        return $this;
71
    }
72
73
    /**
74
     * Adds new macro.
75
     *
76
     * @param string      $name
77
     * @param Latte\Macro $macro
78
     *
79
     * @return static
80
     */
81
    public function addMacro(string $name, Latte\Macro $macro)
82
    {
83
        $this->latte->addMacro($name, $macro);
84
85
        return $this;
86
    }
87
88
    /**
89
     * Registers run-time function.
90
     *
91
     * @param string   $name
92
     * @param callable $callback
93
     *
94
     * @return static
95
     */
96
    public function addFunction(string $name, callable $callback)
97
    {
98
        $this->latte->addFunction($name, $callback);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Sets translate adapter.
105
     *
106
     * @param null|Nette\Localization\ITranslator $translator
107
     *
108
     * @return static
109
     */
110
    public function setTranslator(?Nette\Localization\ITranslator $translator)
0 ignored issues
show
Bug introduced by
The type Nette\Localization\ITranslator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
    {
112
        $this->latte->addFilter('translate', function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator): string {
113
            return $translator === null ? $args[0] : $translator->translate(...$args);
114
        });
115
116
        return $this;
117
    }
118
119
    /**
120
     * Adds new provider.
121
     *
122
     * @param string $name
123
     * @param mixed  $value
124
     *
125
     * @return static
126
     */
127
    public function addProvider(string $name, $value)
128
    {
129
        $this->latte->addProvider($name, $value);
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param null|Latte\Policy $policy
136
     *
137
     * @return static
138
     */
139
    public function setPolicy(?Latte\Policy $policy)
140
    {
141
        $this->latte->setPolicy($policy);
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param callable $callback
148
     *
149
     * @return static
150
     */
151
    public function setExceptionHandler(callable $callback)
152
    {
153
        $this->latte->setExceptionHandler($callback);
154
155
        return $this;
156
    }
157
}
158