Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

Twig::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Renderer;
10
11
use Cecil\Renderer\Twig\Extension as TwigExtension;
12
13
/**
14
 * Class Twig.
15
 */
16
class Twig implements RendererInterface
17
{
18
    /**
19
     * @var \Twig_Environment
20
     */
21
    protected $twig;
22
    /**
23
     * @var string
24
     */
25
    protected $templatesDir;
26
    /**
27
     * @var string
28
     */
29
    protected $rendered;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function __construct($templatesPath, $config)
35
    {
36
        // load layouts
37
        $loader = new \Twig_Loader_Filesystem($templatesPath);
38
        // Twig
39
        $this->twig = new \Twig_Environment($loader, [
40
            'debug'            => true,
41
            'strict_variables' => true,
42
            'autoescape'       => false,
43
            'cache'            => false,
44
            'auto_reload'      => true,
45
        ]);
46
        // add extensions
47
        $this->twig->addExtension(new \Twig_Extension_Debug());
48
        $this->twig->addExtension(new TwigExtension($config->getOutputPath()));
49
        $this->twig->addExtension(new \Twig_Extension_StringLoader());
50
        $this->twig->getExtension('Twig_Extension_Core')->setDateFormat($config->get('site.date.format'));
51
        $this->twig->getExtension('Twig_Extension_Core')->setTimezone($config->get('site.date.timezone'));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function addGlobal($name, $value)
58
    {
59
        $this->twig->addGlobal($name, $value);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function render($template, $variables)
66
    {
67
        $this->rendered = $this->twig->render($template, $variables);
68
69
        // add generator meta
70
        if (!preg_match('/<meta name="generator".*/i', $this->rendered)) {
71
            $meta = '<meta name="generator" content="Cecil" />';
72
            $this->rendered = preg_replace('/(<head>|<head[[:space:]]+.*>)/i', '$1'."\n\t".$meta, $this->rendered);
73
        }
74
75
        // replace excerpt tag by HTML anchor
76
        $pattern = '/(.*)(<!-- excerpt -->)(.*)/i';
77
        $replacement = '$1<span id="more"></span>$3';
78
        $this->rendered = preg_replace($pattern, $replacement, $this->rendered);
79
80
        return $this->rendered;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function isValid($template)
87
    {
88
        try {
89
            $this->twig->parse($this->twig->tokenize($template));
90
91
            return true;
92
        } catch (\Twig_Error_Syntax $e) {
93
            return false;
94
        }
95
    }
96
}
97