Passed
Push — twig-cache ( cac4f3 )
by Arnaud
04:41
created

Twig::__construct()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 26
c 2
b 0
f 0
nc 32
nop 2
dl 0
loc 40
rs 8.8817
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
    /** @var \Twig\Profiler\Profile */
26
    public $profile;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct(Builder $builder, $templatesPath)
32
    {
33
        // load layouts
34
        $loader = new \Twig\Loader\FilesystemLoader($templatesPath);
35
        // Twig
36
        $loaderOptions = [
37
            'debug'            => getenv('CECIL_DEBUG') == 'true' ? true : false,
38
            'strict_variables' => true,
39
            'autoescape'       => false,
40
            'cache'            => false,
41
            'auto_reload'      => true,
42
        ];
43
        // Trying to optimize Twig rendering time
44
        $performance = true;
45
        $cachePath = \Cecil\Util::joinFile($builder->getConfig()->getCachePath(), 'templates');
46
        if ($performance) {
0 ignored issues
show
introduced by
The condition $performance is always true.
Loading history...
47
            $loaderOptions = array_replace($loaderOptions, ['cache' => $cachePath]);
48
        }
49
        $this->twig = new \Twig\Environment($loader, $loaderOptions);
50
        // set date format & timezone
51
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
52
            ->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

52
            ->/** @scrutinizer ignore-call */ setDateFormat($builder->getConfig()->get('date.format'));
Loading history...
53
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
54
            ->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

54
            ->/** @scrutinizer ignore-call */ setTimezone($builder->getConfig()->get('date.timezone'));
Loading history...
55
        // adds extensions
56
        $this->twig->addExtension(new TwigExtension($builder));
57
        $this->twig->addExtension(new \Twig\Extension\StringLoaderExtension());
58
        // internationalisation
59
        if (extension_loaded('intl')) {
60
            $this->twig->addExtension(new \Twig_Extensions_Extension_Intl());
61
        }
62
        if (extension_loaded('gettext')) {
63
            $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
64
        }
65
        if (getenv('CECIL_DEBUG') == 'true') {
66
            // dump()
67
            $this->twig->addExtension(new \Twig\Extension\DebugExtension());
68
            // profiler
69
            $this->profile = new \Twig\Profiler\Profile();
70
            $this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile));
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function addGlobal(string $name, $value): void
78
    {
79
        $this->twig->addGlobal($name, $value);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function render(string $template, array $variables): string
86
    {
87
        return $this->twig->render($template, $variables);
88
    }
89
}
90