TwigTemplateEngine::sourceEngine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of template
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Template\Engine;
13
14
use Slick\Template\Exception\MissingParsedTemplate;
15
use Slick\Template\TemplateEngineInterface;
16
use Twig\Environment;
17
use Twig\TemplateWrapper;
18
19
/**
20
 * TwigTemplateEngine
21
 *
22
 * @package Slick\Template\Engine
23
 */
24
final class TwigTemplateEngine implements TemplateEngineInterface
25
{
26
    private ?TemplateWrapper $template = null;
27
28
    public function __construct(private readonly Environment $twigEnvironment)
29
    {
30
    }
31
32
    public function parse(string $source): TemplateEngineInterface
33
    {
34
        $engine = clone $this;
35
        $engine->template = $this->twigEnvironment->load($source);
36
        return $engine;
37
    }
38
39
    public function process(array $data = array()): string
40
    {
41
        if (null === $this->template) {
42
            throw new MissingParsedTemplate(
43
                "You need to set a template before using the process() method. ".
44
                "Use TemplateEngine::parse() first."
45
            );
46
        }
47
        return $this->template->render($data);
48
    }
49
50
    public function sourceEngine(): Environment
51
    {
52
        return $this->twigEnvironment;
53
    }
54
}
55