Completed
Push — master ( 413e31...6028d7 )
by Changwan
06:35
created

TwigView::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 13
ccs 0
cts 13
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\View\Bridges\Twig;
3
4
use Twig_Environment;
5
use Twig_Loader_Filesystem;
6
use Wandu\View\Contracts\RenderInterface;
7
use Wandu\View\FileNotFoundException;
8
9
class TwigView implements RenderInterface
10
{
11
    /** @var \Twig_Environment */
12
    protected $twig;
13
14
    /** @var array */
15
    protected $values = [];
16
17
    public function __construct(Twig_Environment $twig)
18
    {
19
        $this->twig = $twig;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function with(array $values = [])
26
    {
27
        $new = clone $this;
28
        $new->values = $values;
29
        return $new;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function render($template, array $values = [], $basePath = null)
36
    {
37
        $twig = $this->twig;
38
        if (isset($basePath)) {
39
            $twig = clone $this->twig;
40
            $twig->setLoader(new Twig_Loader_Filesystem($basePath));
41
        }
42
        try {
43
            return $twig->render($template, $values + $this->values);
44
        } catch (\Exception $e) {
45
            throw new FileNotFoundException("Cannot find the template file, {$template}");
46
        }
47
    }
48
}
49