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

TwigView   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A with() 0 6 1
A render() 0 13 3
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