Passed
Pull Request — develop (#349)
by Schlaefer
15:02 queued 10s
created

Twig::getTemplateFileName()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
crap 4.0961
rs 9.9
1
<?php
2
/**
3
 * Template engine class
4
 */
5
namespace Phile\Plugin\Phile\TemplateTwig\Template;
6
7
use Phile\Core\Container;
8
use Phile\Core\Registry;
9
use Phile\Model\Page;
10
use Phile\ServiceLocator\TemplateInterface;
11
12
/**
13
 * Class Twig
14
 *
15
 * @author  PhileCMS
16
 * @link    https://philecms.github.io
17
 * @license http://opensource.org/licenses/MIT
18
 * @package Phile\Plugin\Phile\TemplateTwig\Template
19
 */
20
class Twig implements TemplateInterface
21
{
22
    /**
23
     * @var array the config for twig
24
     */
25
    protected $config;
26
27
    /**
28
     * @var Page the page model
29
     */
30
    protected $page;
31
32
    /**
33
     * @var string theme name
34
     */
35
    protected $theme;
36
37
    /**
38
     * @var string path to theme directory
39
     */
40
    protected $themesDir;
41
42
    /**
43
     * the constructor
44
     *
45
     * @param array $config the configuration
46
     */
47 31
    public function __construct(array $config = [])
48
    {
49 31
        $this->theme = $config['theme'];
50 31
        $this->themesDir = $config['themes_dir'];
51 31
        unset($config['theme'], $config['themes_dir']);
52 31
        $this->config = $config;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 5
    public function setCurrentPage(Page $page)
59
    {
60 5
        $this->page = $page;
61
    }
62
63
    /**
64
     * method to render the page/template
65
     *
66
     * @return mixed|string
67
     */
68 5
    public function render()
69
    {
70 5
        $engine = $this->getEngine();
71 5
        $vars = $this->getTemplateVars();
72 5
        Container::getInstance()->get('Phile_EventBus')->trigger(
73
            'template_engine_registered',
74 5
            ['engine' => &$engine, 'data' => &$vars]
75
        );
76 5
        return $this->doRender($engine, $vars);
77
    }
78
79
    /**
80
     * wrapper to call the render engine
81
     *
82
     * @param \Twig\Environment $engine
83
     * @param array $vars
84
     * @return string
85
     */
86 5
    protected function doRender(\Twig\Environment $engine, array $vars): string
87
    {
88
        try {
89 5
            $template = $this->getTemplateFileName();
90
        } catch (\RuntimeException $e) {
91
            return $e->getMessage();
92
        }
93 5
        return $engine->render($template, $vars);
94
    }
95
96
    /**
97
     * get template engine
98
     *
99
     * @return \Twig\Environment
100
     */
101 5
    protected function getEngine()
102
    {
103 5
        $loader = new \Twig\Loader\FilesystemLoader($this->getTemplatePath());
104 5
        $twig = new \Twig\Environment($loader, $this->config);
105
106
        // load the twig debug extension if required
107 5
        if (!empty($this->config['debug'])) {
108
            $twig->addExtension(new \Twig\Extension\DebugExtension());
109
        }
110 5
        return $twig;
111
    }
112
113
    /**
114
     * get template file name
115
     *
116
     * @return string
117
     * @throws \RuntimeException
118
     */
119 5
    protected function getTemplateFileName(): string
120
    {
121 5
        $template = $this->page->getMeta()->get('template');
122 5
        if (empty($template)) {
123 5
            $template = 'index';
124
        }
125 5
        if (!empty($this->config['template-extension'])) {
126 5
            $template .= '.' . $this->config['template-extension'];
127
        }
128 5
        $templatePath = $this->getTemplatePath($template);
129 5
        if (!file_exists($templatePath)) {
130
            throw new \RuntimeException(
131
                "Template file '{$templatePath}' not found.",
132
                1427990135
133
            );
134
        }
135 5
        return $template;
136
    }
137
138
    /**
139
     * get file path to (sub-path) in theme-path
140
     *
141
     * @param  string $sub
142
     * @return string
143
     */
144 5
    protected function getTemplatePath(string $sub = ''): string
145
    {
146 5
        $themePath = $this->themesDir . $this->theme;
147 5
        if (!empty($sub)) {
148 5
            $themePath .= '/' . ltrim($sub, DIRECTORY_SEPARATOR);
149
        }
150 5
        return $themePath;
151
    }
152
153
    /**
154
     * get template vars
155
     *
156
     * @return array
157
     * @throws \Exception
158
     */
159 5
    protected function getTemplateVars(): array
160
    {
161 5
        $defaults = [
162 5
            'content' => $this->page->getContent(),
163 5
            'meta' => $this->page->getMeta(),
164 5
            'current_page' => $this->page,
165 5
            'pages' => $this->page->getRepository()->findAll(),
166
        ];
167
168
        /**
169
         * @var array $templateVars
170
         */
171 5
        $templateVars = Registry::get('templateVars');
172 5
        $templateVars += $defaults;
173
174 5
        return $templateVars;
175
    }
176
}
177