Completed
Push — master ( 37bc16...2b3d40 )
by Mikael
02:22
created

CThemeEngine::appendToVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace Anax\ThemeEngine;
4
5
/**
6
 * Anax base class for wrapping sessions.
7
 *
8
 */
9
class CThemeEngine implements IThemeEngine, \Anax\DI\IInjectionAware
10
{
11
    use \Anax\View\THelpers,
12
        \Anax\TConfigure,
13
        \Anax\DI\TInjectionAware;
14
15
16
17
    /**
18
     * Array with variables to provide to theme template files.
19
     */
20
    protected $data = [];
21
22
23
24
    /**
25
     * Overwrite template file as defined in config.
26
     */
27
    protected $template = null;
28
29
30
31
    /**
32
     * Set another template file to use.
33
     *
34
     * @param string $name of the template file.
35
     *
36
     * @return $this
37
     */
38
    public function setTemplate($name)
39
    {
40
        $this->template = $name;
41
        return $this;
42
    }
43
44
45
46
    /**
47
     * Shortcut to set title.
48
     *
49
     * @param string $value of the variable.
50
     *
51
     * @return $this
52
     */
53
    public function setTitle($value)
54
    {
55
        return $this->setVariable("title", $value);
56
    }
57
58
59
60
    /**
61
     * Set a base title which is appended to the page title.
62
     *
63
     * @param string $value of the variable.
64
     *
65
     * @return $this
66
     */
67
    public function setBaseTitle($value)
68
    {
69
        return $this->setVariable("title_append", $value);
70
    }
71
72
73
74
    /**
75
     * Set a variable which will be exposed to the template files during render.
76
     *
77
     * @param string $which variable to set value of.
78
     * @param mixed  $value of the variable.
79
     *
80
     * @return $this
81
     */
82
    public function setVariable($which, $value)
83
    {
84
        $this->data[$which] = $value;
85
        return $this;
86
    }
87
88
89
90
    /**
91
     * Append value to variable by making the variable an array.
92
     *
93
     * @param string $which variable to set value of.
94
     * @param mixed  $value of the variable.
95
     *
96
     * @return $this
97
     */
98
    public function appendToVariable($which, $value)
99
    {
100
        $this->data[$which][] = $value;
101
        return $this;
102
    }
103
104
105
106
    /**
107
     * Add frontmatter to be exposed to theme template file.
108
     *
109
     * @param array|null $matter to add.
110
     *
111
     * @return $this
112
     */
113
    public function addFrontmatter($matter)
114
    {
115
        $this->data = array_merge($this->data, $matter);
116
        return $this;
117
    }
118
119
120
121
    /**
122
     * Get a value of a variable which will be exposed to the template files
123
     * during render.
124
     *
125
     * @param string $which variable to get value of.
126
     *
127
     * @return mixed as value of variable, or null if value is not set.
128
     */
129
    public function getVariable($which)
130
    {
131
        if (isset($this->data[$which])) {
132
            return $this->data[$which];
133
        } elseif (isset($this->config["data"])) {
134
            return $this->config["data"][$which];
135
        }
136
137
        return null;
138
    }
139
140
141
142
    /**
143
     * Add a stylesheet.
144
     *
145
     * @param string $uri to add.
146
     *
147
     * @return $this
148
     */
149
    public function addStylesheet($uri)
150
    {
151
        $this->config["data"]["stylesheets"][] = $uri;
152
        return $this;
153
    }
154
155
156
157
    /**
158
     * Add a javascript asset.
159
     *
160
     * @param string $uri to add.
161
     *
162
     * @return $this
163
     */
164
    public function addJavaScript($uri)
165
    {
166
        $this->config["data"]["javascript_include"][] = $uri;
167
        return $this;
168
    }
169
170
171
172
    /**
173
     * Render the theme by applying the variables onto the template files.
174
     *
175
     * @return void
176
     */
177
    public function render()
178
    {
179
        // Create views for regions, from config-file
180
        if (isset($this->config["views"])) {
181
            foreach ($this->config["views"] as $view) {
182
                $this->di->views->add($view);
183
            }
184
        }
185
186
        // Get default view to start render from
187
        $defaultData = [
188
            "currentRoute" => "route-" . str_replace("/", "-", $this->di->get("request")->getRoute()),
189
        ];
190
        $view = $this->config["view"];
191
        $view["data"] = array_merge_recursive($defaultData, $this->data, $view["data"]);
192
193
        if (isset($this->template)) {
194
            $view["template"] = $this->template;
195
        }
196
197
        // Get the path to the view template file
198
        $view["template"] = $this->di->get("views")->getTemplateFile($view["template"]);
199
200
        // Send response headers, if any.
201
        $this->di->get("response")->sendHeaders();
202
203
        // Execute the default view
204
        $start = $this->di->get("view");
205
        $start->setDI($this->di);
206
        $start->set($view);
207
        $start->render();
208
    }
209
}
210