Passed
Push — fix/debug-mode ( e4c63f )
by Arnaud
05:20
created

Twig::addGlobal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        // default options
36
        $loaderOptions = [
37
            'debug'            => $builder->isDebug(),
38
            'strict_variables' => true,
39
            'autoescape'       => false,
40
            'auto_reload'      => true,
41
            'cache'            => false,
42
        ];
43
        // use Twig cache?
44
        if ($builder->getConfig()->get('cache.templates.enabled')) {
45
            $templatesCachePath = \Cecil\Util::joinFile(
46
                $builder->getConfig()->getCachePath(),
47
                $builder->getConfig()->get('cache.templates.dir')
48
            );
49
            $loaderOptions = array_replace($loaderOptions, ['cache' => $templatesCachePath]);
50
        }
51
        // create the Twig instance
52
        $this->twig = new \Twig\Environment($loader, $loaderOptions);
53
        // set date format & timezone
54
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
55
            ->setDateFormat($builder->getConfig()->get('date.format'));
56
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
57
            ->setTimezone($builder->getConfig()->get('date.timezone'));
58
        // adds extensions
59
        $this->twig->addExtension(new TwigExtension($builder));
60
        $this->twig->addExtension(new \Twig\Extension\StringLoaderExtension());
61
        // internationalisation
62
        if (extension_loaded('intl')) {
63
            $this->twig->addExtension(new \Twig_Extensions_Extension_Intl());
64
        }
65
        if (extension_loaded('gettext')) {
66
            $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
67
        }
68
        if ($builder->isDebug()) {
69
            // dump()
70
            $this->twig->addExtension(new \Twig\Extension\DebugExtension());
71
            // profiler
72
            $this->profile = new \Twig\Profiler\Profile();
73
            $this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile));
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function addGlobal(string $name, $value): void
81
    {
82
        $this->twig->addGlobal($name, $value);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function render(string $template, array $variables): string
89
    {
90
        return $this->twig->render($template, $variables);
91
    }
92
}
93