CThemeEngine   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 217
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setTemplate() 0 5 1
A setTitle() 0 4 1
A setBaseTitle() 0 4 1
A setVariable() 0 5 1
A appendToVariable() 0 5 1
A addFrontmatter() 0 5 1
A getVariable() 0 10 3
A addStylesheet() 0 5 1
A addJavaScript() 0 5 1
A setConfigKey() 0 5 1
A render() 0 33 4
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["view"]["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["view"]["data"]["javascript_include"][] = $uri;
167
        return $this;
168
    }
169
170
171
172
    /**
173
     * Set/clear a key/value from the configuration file.
174
     *
175
     * @param string $uri to add.
0 ignored issues
show
Bug introduced by
There is no parameter named $uri. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
176
     *
177
     * @return $this
178
     */
179
    public function setConfigKey($key, $value)
180
    {
181
        $this->config["view"]["data"][$key] = $value;
182
        return $this;
183
    }
184
185
186
187
    /**
188
     * Render the theme by applying the variables onto the template files.
189
     *
190
     * @return void
191
     */
192
    public function render()
193
    {
194
        // Create views for regions, from config-file
195
        if (isset($this->config["views"])) {
196
            foreach ($this->config["views"] as $view) {
197
                $this->di->views->add($view);
198
            }
199
        }
200
201
        // Get default view to start render from
202
        $defaultData = [
203
            "currentRoute" => "route-" . str_replace("/", "-", $this->di->get("request")->getRoute()),
204
        ];
205
        $view = $this->config["view"];
206
207
        $view["data"] = array_merge_recursive($defaultData, $this->data, $view["data"]);
208
209
        if (isset($this->template)) {
210
            $view["template"] = $this->template;
211
        }
212
213
        // Get the path to the view template file
214
        $view["template"] = $this->di->get("views")->getTemplateFile($view["template"]);
215
216
        // Send response headers, if any.
217
        $this->di->get("response")->sendHeaders();
218
219
        // Execute the default view
220
        $start = $this->di->get("view");
221
        $start->setDI($this->di);
222
        $start->set($view);
223
        $start->render();
224
    }
225
}
226