Completed
Push — master ( 90ac5a...649bdf )
by Amine
11s
created

TwigLoader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Tarsana\Command\Template\Twig;
2
3
use Tarsana\Command\Interfaces\Template\TemplateInterface;
4
use Tarsana\Command\Interfaces\Template\TemplateLoaderInterface;
5
use Tarsana\Command\Template\Twig\TwigTemplate;
6
7
/**
8
 * Twig Templates Loader
9
 */
10
class TwigLoader implements TemplateLoaderInterface {
11
12
    /**
13
     * Twig Environment.
14
     *
15
     * @var \Twig_Environment
16
     */
17
    protected $env;
18
19
    /**
20
     * Initialize the loader.
21
     *
22
     * @param  string $templatesPath
23
     * @param  string $cachePath
24
     * @return self
25
     */
26
    public function init(string $templatesPath, string $cachePath = null) : TemplateLoaderInterface
27
    {
28
        $loader = new \Twig_Loader_Filesystem($templatesPath);
29
        if (null !== $cachePath) {
30
            $this->env = new \Twig_Environment($loader, [
31
                'cache' => $cachePath,
32
            ]);
33
        } else {
34
            $this->env = new \Twig_Environment($loader);
35
        }
36
        return $this;
37
    }
38
39
    /**
40
     * Loads a template.
41
     *
42
     * @param  string $name
43
     * @return Tarsana\Command\Template\Twig\TwigTemplate
44
     */
45
    public function load(string $name) : TemplateInterface
46
    {
47
        return new TwigTemplate($this->env->loadTemplate($name));
48
    }
49
50
}
51