1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SideDevOrg\MiniPhpFw; |
4
|
|
|
|
5
|
|
|
use Mustache_Engine; |
6
|
|
|
use Mustache_Loader_FilesystemLoader; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Framework Controller. |
10
|
|
|
*/ |
11
|
|
|
class Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Data array container. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $data = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* I18n array container. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $i18n = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Request. |
29
|
|
|
* |
30
|
|
|
* @var \Psr\Http\Message\RequestInterface |
31
|
|
|
*/ |
32
|
|
|
private $request; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Mustache instance. |
36
|
|
|
* |
37
|
|
|
* @var Mustache_Engine |
38
|
|
|
*/ |
39
|
|
|
private $mustacheInstance; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set request. |
43
|
|
|
* |
44
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
45
|
|
|
* |
46
|
|
|
* @return \Psr\Http\Message\RequestInterface |
47
|
|
|
*/ |
48
|
10 |
|
public function setRequest(\Psr\Http\Message\RequestInterface $request) : \Psr\Http\Message\RequestInterface |
49
|
|
|
{ |
50
|
10 |
|
return $this->request = $request; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get header. |
55
|
|
|
* |
56
|
|
|
* @param string $name |
57
|
|
|
* |
58
|
|
|
* @return mixed srtring or false |
59
|
|
|
*/ |
60
|
5 |
|
protected function header(string $name) |
61
|
|
|
{ |
62
|
5 |
|
return isset($this->request->getHeaders()[$name]) ? |
63
|
5 |
|
$this->request->getHeaders()[$name][0] : false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get input. |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* @param mixed $defaultValue |
71
|
|
|
* @param bool $autoTrim |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
1 |
|
protected function input(string $name, $defaultValue = null, bool $autoTrim = true) |
76
|
|
|
{ |
77
|
1 |
|
$value = false; |
78
|
|
|
|
79
|
1 |
|
if ($this->request->getMethod() === 'GET') { |
80
|
1 |
|
if (isset($this->request->getQueryParams()[$name])) { |
|
|
|
|
81
|
1 |
|
$value = $this->request->getQueryParams()[$name]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
if ($this->request->getMethod() === 'POST') { |
86
|
1 |
|
if (isset($this->request->getParsedBody()[$name])) { |
|
|
|
|
87
|
1 |
|
$value = $this->request->getParsedBody()[$name]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
if ($value) { |
92
|
1 |
|
return (!$autoTrim) ? $value : trim($value); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $defaultValue; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set data. |
100
|
|
|
* |
101
|
|
|
* @param mixed $key string or array |
102
|
|
|
* @param mixed $value if $key is string |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
1 |
|
protected function data($key, $value = false) |
107
|
|
|
{ |
108
|
1 |
|
if (is_array($key)) { |
109
|
1 |
|
return $this->data = array_merge($this->data, $key); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $this->data[$key] = $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get data. |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
4 |
|
protected function getData() : array |
121
|
|
|
{ |
122
|
4 |
|
return $this->data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get lang key in file.key format. |
127
|
|
|
* |
128
|
|
|
* @param string $fileKey |
129
|
|
|
* @param bool|string $changeLang false or lang code |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
1 |
|
protected function lang($fileKey, $changeLang = false) : string |
134
|
|
|
{ |
135
|
1 |
|
$defaultHeaderLang = 'en'; |
136
|
1 |
|
$headerLang = ($this->header('lang')) ? $this->header('lang') : $defaultHeaderLang; |
137
|
|
|
|
138
|
1 |
|
$lang = (!$changeLang) ? $headerLang : $changeLang; |
139
|
|
|
|
140
|
1 |
|
$search = explode('.', $fileKey); |
141
|
1 |
|
$file = $search[0].'.php'; |
142
|
1 |
|
$key = isset($search[1]) ? $search[1] : ''; |
143
|
|
|
|
144
|
1 |
|
$route = $this->config('paths.i18n').'/'.$lang.'/'.$file; |
145
|
1 |
|
if (!file_exists($route)) { |
146
|
1 |
|
return $fileKey; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
$data = require_once $route; |
150
|
|
|
|
151
|
1 |
|
if (is_array($data)) { |
152
|
1 |
|
$this->i18n[$lang][$file] = $data; |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return isset($this->i18n[$lang][$file][$key]) ? |
156
|
1 |
|
$this->i18n[$lang][$file][$key] : |
157
|
1 |
|
$fileKey |
158
|
|
|
; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get config item. |
163
|
|
|
* |
164
|
|
|
* @param string $key |
165
|
|
|
* |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
4 |
|
protected function config(string $key) |
169
|
|
|
{ |
170
|
4 |
|
$config = json_decode($this->header('config'), true); |
|
|
|
|
171
|
4 |
|
$definition = explode('.', $key); |
172
|
4 |
|
$item = isset($config[$definition[0]]) ? $config[$definition[0]] : null; |
173
|
|
|
|
174
|
4 |
|
$numberOfDefinitions = count($definition); |
175
|
|
|
|
176
|
4 |
|
for ($i = 1; $i < $numberOfDefinitions; ++$i) { |
177
|
4 |
|
$item = isset($item[$definition[$i]]) ? $item[$definition[$i]] : null; |
178
|
|
|
} |
179
|
|
|
|
180
|
4 |
|
return $item; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get view. |
185
|
|
|
* |
186
|
|
|
* @param string $template |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
3 |
|
protected function view(string $template, array $data = []) : string |
191
|
|
|
{ |
192
|
3 |
|
if (!$this->mustacheInstance) { |
193
|
3 |
|
$viewsRoute = $this->config('paths.view'); |
194
|
3 |
|
$options = ['extension' => '.hbs']; |
195
|
|
|
|
196
|
3 |
|
$this->mustacheInstance = new Mustache_Engine(array( |
197
|
3 |
|
'loader' => new Mustache_Loader_FilesystemLoader($viewsRoute, $options), |
198
|
3 |
|
'partials_loader' => new Mustache_Loader_FilesystemLoader($viewsRoute, $options), |
199
|
3 |
|
'charset' => 'UTF-8', |
200
|
|
|
)); |
201
|
|
|
} |
202
|
|
|
|
203
|
3 |
|
$tpl = $this->mustacheInstance->loadTemplate($template); |
204
|
3 |
|
$data = array_merge($this->getData(), $data); |
205
|
|
|
|
206
|
3 |
|
return $tpl->render($this->mapData($data)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Map data to output. |
211
|
|
|
* |
212
|
|
|
* @param array $data |
213
|
|
|
* |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
3 |
|
private function mapData(array $data) : array |
217
|
|
|
{ |
218
|
|
|
$assets = [ |
219
|
3 |
|
'js' => false, |
220
|
|
|
'css' => false, |
221
|
|
|
]; |
222
|
|
|
|
223
|
3 |
|
$assets_path = $this->config('paths.assets_manifest'); |
224
|
|
|
|
225
|
3 |
|
if (file_exists($assets_path)) { |
226
|
3 |
|
$assets_data = json_decode(file_get_contents($assets_path), true); |
227
|
|
|
$assets = [ |
228
|
3 |
|
'js' => $assets_data['/app.js'], |
229
|
3 |
|
'css' => $assets_data['/app.css'], |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$tplData = [ |
234
|
3 |
|
'fw' => [ |
235
|
3 |
|
'assets' => $assets, |
236
|
|
|
'i18n' => [], |
237
|
3 |
|
'data' => $data, |
238
|
|
|
], |
239
|
|
|
]; |
240
|
|
|
|
241
|
3 |
|
return $tplData; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get "not found" view. |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
2 |
|
public function not_found() : string |
250
|
|
|
{ |
251
|
2 |
|
return $this->view('404'); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|