Passed
Push — clean-code ( 5aba14...836038 )
by Arnaud
08:50 queued 03:05
created

Twig::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Renderer;
12
13
use Cecil\Builder;
14
use Cecil\Renderer\Twig\Extension as TwigExtension;
15
16
/**
17
 * Class Twig.
18
 */
19
class Twig implements RendererInterface
20
{
21
    /** @var \Twig\Environment */
22
    protected $twig;
23
    /** @var string */
24
    protected $templatesDir;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function __construct($templatesPath, Builder $builder)
30
    {
31
        // load layouts
32
        $loader = new \Twig\Loader\FilesystemLoader($templatesPath);
33
        // Twig
34
        $this->twig = new \Twig\Environment($loader, [
35
            'debug'            => true,
36
            'strict_variables' => true,
37
            'autoescape'       => false,
38
            'cache'            => false,
39
            'auto_reload'      => true,
40
        ]);
41
        // adds extensions
42
        $this->twig->addExtension(new \Twig\Extension\DebugExtension());
43
        $this->twig->addExtension(new TwigExtension($builder));
44
        $this->twig->addExtension(new \Twig\Extension\StringLoaderExtension());
45
        // set date format & timezone
46
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
47
            ->setDateFormat($builder->getConfig()->get('date.format'));
1 ignored issue
show
Bug introduced by
The method setDateFormat() does not exist on Twig\Extension\ExtensionInterface. It seems like you code against a sub-type of Twig\Extension\ExtensionInterface such as Twig\Extension\CoreExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            ->/** @scrutinizer ignore-call */ setDateFormat($builder->getConfig()->get('date.format'));
Loading history...
48
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
49
            ->setTimezone($builder->getConfig()->get('date.timezone'));
1 ignored issue
show
Bug introduced by
The method setTimezone() does not exist on Twig\Extension\ExtensionInterface. It seems like you code against a sub-type of Twig\Extension\ExtensionInterface such as Twig\Extension\CoreExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            ->/** @scrutinizer ignore-call */ setTimezone($builder->getConfig()->get('date.timezone'));
Loading history...
50
        // internationalisation
51
        if (extension_loaded('intl')) {
52
            $this->twig->addExtension(new \Twig_Extensions_Extension_Intl());
53
        }
54
        if (extension_loaded('gettext')) {
55
            $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function addGlobal($name, $value)
63
    {
64
        $this->twig->addGlobal($name, $value);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function render($template, $variables)
71
    {
72
        return $this->twig->render($template, $variables);
73
    }
74
}
75