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

TwigRender::setCharset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
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\Exceptions\LoaderException;
21
use Biurad\UI\Interfaces\CacheInterface as RenderCacheInterface;
22
use Biurad\UI\Interfaces\RenderInterface;
23
use Biurad\UI\Template;
24
use Twig;
25
use Twig\Cache\FilesystemCache;
26
use Twig\Extension\ExtensionInterface;
27
use Twig\Loader\ArrayLoader;
28
use Twig\Loader\ChainLoader;
29
use Twig\Loader\FilesystemLoader;
30
use Twig\Loader\LoaderInterface;
31
use Twig\NodeVisitor\NodeVisitorInterface;
32
use Twig\RuntimeLoader\RuntimeLoaderInterface;
33
34
/**
35
 * Render for Twig templating.
36
 *
37
 * @author Divine Niiquaye Ibok <[email protected]>
38
 */
39
final class TwigRender extends AbstractRender implements RenderCacheInterface
40
{
41
    protected const EXTENSIONS = ['twig'];
42
43
    /** @var Twig\Environment */
44
    protected $environment;
45
46
    /**
47
     * TwigEngine constructor.
48
     *
49
     * @param string[] $extensions
50
     */
51 4
    public function __construct(Twig\Environment $environment = null, array $extensions = self::EXTENSIONS)
52
    {
53 4
        $this->environment = $environment ?? new Twig\Environment(new ArrayLoader());
54 4
        $this->extensions = $extensions;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 4
    public function withCache(?string $cacheDir): void
61
    {
62 4
        if (null !== $cacheDir) {
63 3
            if (false !== $this->environment->getCache()) {
64
                throw new LoaderException('The Twig render has an existing cache implementation which must be removed.');
65
            }
66
67 3
            $this->environment->setCache(new FilesystemCache($cacheDir));
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 4
    public function withLoader(Template $loader): RenderInterface
75
    {
76 4
        $this->environment->addFilter(new Twig\TwigFilter('template', [$loader, 'find'], ['is_safe' => ['all']]));
77 4
        $this->environment->addFunction(new Twig\TwigFunction('template', [$loader, 'render'], ['is_safe' => ['all']]));
78
79 4
        return parent::withLoader($loader);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 4
    public function render(string $template, array $parameters): string
86
    {
87 4
        $source = self::loadHtml($template) ?? $template;
88
89 4
        if ($source !== $template || !\file_exists($template)) {
90
            $loader = new ArrayLoader([$template => $source]);
91
        } else {
92 4
            $loader = new FilesystemLoader([\dirname($template), \dirname($template, 2)]);
93 4
            $template = \substr($template, \strripos($template, '/'));
94
        }
95
96 4
        $this->addLoader($loader);
97
98 4
        return $this->environment->load($template)->render($parameters);
99
    }
100
101
    public function setCharset(string $charset): void
102
    {
103
        $this->environment->setCharset($charset);
104
    }
105
106 4
    public function addLoader(LoaderInterface $loader): void
107
    {
108 4
        $templateLoader = $this->environment->getLoader();
109
110 4
        if ($templateLoader instanceof ChainLoader) {
111
            $templateLoader->addLoader($loader);
112
113
            return;
114
        }
115
116 4
        if ($loader instanceof ChainLoader) {
117
            $loader->addLoader($templateLoader);
118 4
        } elseif (!$templateLoader instanceof ArrayLoader) {
119 3
            $loader = new ChainLoader([$loader, $templateLoader]);
120
        }
121
122 4
        $this->environment->setLoader($loader);
123
    }
124
125
    public function addRuntimeLoader(RuntimeLoaderInterface $loader): void
126
    {
127
        $this->environment->addRuntimeLoader($loader);
128
    }
129
130
    public function addExtension(ExtensionInterface $extension): void
131
    {
132
        $this->environment->addExtension($extension);
133
    }
134
135
    /**
136
     * @param ExtensionInterface[] $extensions An array of extensions
137
     */
138
    public function setExtensions(array $extensions): void
139
    {
140
        $this->environment->setExtensions($extensions);
141
    }
142
143
    public function addNodeVisitor(NodeVisitorInterface $visitor): void
144
    {
145
        $this->environment->addNodeVisitor($visitor);
146
    }
147
148
    public function addFilter(Twig\TwigFilter $filter): void
149
    {
150
        $this->environment->addFilter($filter);
151
    }
152
153
    public function registerUndefinedFilterCallback(callable $callable): void
154
    {
155
        $this->environment->registerUndefinedFilterCallback($callable);
156
    }
157
158
    public function addFunction(Twig\TwigFunction $function): void
159
    {
160
        $this->environment->addFunction($function);
161
    }
162
}
163