|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vssl\Render; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates\Engine; |
|
6
|
|
|
|
|
7
|
|
|
class Renderer |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Renderer configuration. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $config; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Page data to render. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $data; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Cache adapter. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Journey\Cache\CacheAdapterIterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $cache; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* String data to output. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $output; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Template engine. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \League\Plates\Engine |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $engine; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Fields that are required in order to render. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $required; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Name of the current theme. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $theme; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Initialize our new renderer. |
|
60
|
|
|
*/ |
|
61
|
9 |
|
public function __construct(array $config, array $data) |
|
62
|
|
|
{ |
|
63
|
9 |
|
$this->config = $config; |
|
64
|
9 |
|
$this->required = array_filter($config['required_fields']); |
|
65
|
9 |
|
$this->setData($data); |
|
66
|
9 |
|
$this->engine = new Engine(__DIR__ . "/../templates"); |
|
67
|
9 |
|
$this->registerFunctions(); |
|
68
|
9 |
|
if (isset($config['templates'])) { |
|
69
|
1 |
|
$this->registerTheme('default', $config['templates']); |
|
70
|
1 |
|
} |
|
71
|
9 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Register custom theme functions particular function. |
|
75
|
|
|
* |
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
9 |
|
public function registerFunctions() |
|
79
|
|
|
{ |
|
80
|
9 |
|
$this->engine->registerFunction('wrapperClasses', [$this, 'wrapperClasses']); |
|
81
|
9 |
|
$this->engine->registerFunction('image', [$this, 'image']); |
|
82
|
9 |
|
$this->engine->registerFunction('inline', [$this, 'inline']); |
|
83
|
9 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Add a single directory to the template engine. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $directories directory of templates |
|
|
|
|
|
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public function registerTheme($theme, $directory) |
|
92
|
|
|
{ |
|
93
|
3 |
|
if (!is_dir($directory)) { |
|
94
|
1 |
|
throw new RendererException('The specified theme directory does not exist: ' . $directory); |
|
95
|
|
|
} |
|
96
|
2 |
|
$this->theme = $theme; |
|
97
|
2 |
|
$this->engine->addFolder($theme, $directory, true); |
|
98
|
2 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the template rendering engine. |
|
103
|
|
|
* |
|
104
|
|
|
* @return League\Plates\Engine |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getEngine() |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->engine; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the page data that will be rendered. |
|
113
|
|
|
* |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function getData() |
|
117
|
|
|
{ |
|
118
|
2 |
|
return $this->data; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Replace the page data |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $data |
|
125
|
|
|
*/ |
|
126
|
9 |
|
public function setData(array $data) |
|
127
|
|
|
{ |
|
128
|
9 |
|
$this->validateData($data); |
|
129
|
9 |
|
$this->data = $data; |
|
130
|
9 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Validate that the page data is complete enough to render. |
|
135
|
|
|
* |
|
136
|
|
|
* Note: Throws an exception if the page data is incomplete. |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $data array |
|
139
|
|
|
* @return boolean |
|
140
|
|
|
*/ |
|
141
|
9 |
|
public function validateData(array $data) |
|
142
|
|
|
{ |
|
143
|
9 |
|
if (!count(array_diff_key(array_flip($this->required), $data))) { |
|
144
|
9 |
|
return true; |
|
145
|
|
|
} |
|
146
|
1 |
|
throw new RendererException('Invalid or incomplete page data'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Generate wrapper classes for stripes |
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
3 |
|
public function wrapperClasses($string) |
|
155
|
|
|
{ |
|
156
|
3 |
|
$type = str_replace("stripe-", "", $string); |
|
157
|
3 |
|
return 'vssl-stripe vssl-stripe--' . $type; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Returns the image url on the vssl server. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $imageName hash.extension |
|
|
|
|
|
|
164
|
|
|
* @param string $style image style name |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
2 |
|
public function image($name, $style = false) |
|
168
|
|
|
{ |
|
169
|
2 |
|
return ltrim($this->config['base_uri'], '/') . "/images" . ($style ? '/' . $style : '') . "/" . $name; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Strip most tags from output stored by the inline editor. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $str Output |
|
176
|
|
|
* @param string $allowed_tags Permitted HTML tags |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
2 |
|
public function inline($str, $allowed_tags = '<a><b><strong><i><em>') |
|
180
|
|
|
{ |
|
181
|
2 |
|
return strip_tags($str, $allowed_tags); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Process data before its output. This is the last chance to make changes |
|
186
|
|
|
* to data before being passed to the actual template files. |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
3 |
|
public function processData($data) |
|
191
|
|
|
{ |
|
192
|
3 |
|
$data['themePrefix'] = $this->theme ? $this->theme . "::" : ""; |
|
193
|
3 |
|
if (isset($data['stripes'])) { |
|
194
|
3 |
|
$data['stripes'] = array_filter($data['stripes'], function ($stripe) use ($data) { |
|
195
|
3 |
|
return $this->engine->exists($data['themePrefix'] . 'stripes/' . $stripe['type']); |
|
196
|
3 |
|
}); |
|
197
|
3 |
|
} |
|
198
|
3 |
|
return $data; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Render the current data. |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
3 |
|
public function render() |
|
207
|
|
|
{ |
|
208
|
3 |
|
if (!isset($this->data['type'])) { |
|
209
|
|
|
print_r($this->data); |
|
210
|
|
|
die(); |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
3 |
|
return $this->engine->render('page', $this->processData($this->data)); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Return the rendered page. |
|
217
|
|
|
* |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
3 |
|
public function __toString() |
|
221
|
|
|
{ |
|
222
|
|
|
try { |
|
223
|
3 |
|
return $this->render(); |
|
224
|
|
|
} catch (\Exception $e) { |
|
225
|
|
|
return "Error rendering page: " . $e->getMessage(); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.