1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Alxarafe. Development of PHP applications in a flash! |
4
|
|
|
* Copyright (C) 2018-2019 Alxarafe <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Alxarafe\Core\Base; |
8
|
|
|
|
9
|
|
|
use Alxarafe\Core\Helpers\Session; |
10
|
|
|
use Alxarafe\Core\Helpers\Utils\ClassUtils; |
11
|
|
|
use Alxarafe\Core\Providers\Container; |
12
|
|
|
use Alxarafe\Core\Providers\DebugTool; |
13
|
|
|
use Alxarafe\Core\Providers\Logger; |
14
|
|
|
use Alxarafe\Core\Providers\TemplateRender; |
15
|
|
|
use Alxarafe\Core\Providers\Translator; |
16
|
|
|
use Monolog\Logger as MonologLogger; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Controller |
23
|
|
|
* |
24
|
|
|
* @package Alxarafe\Core\Base |
25
|
|
|
*/ |
26
|
|
|
abstract class Controller |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Contains the user's name or null |
31
|
|
|
* |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
public $username; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class short name. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $shortName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* To manage PHP Sessions. |
45
|
|
|
* |
46
|
|
|
* @var Session |
47
|
|
|
*/ |
48
|
|
|
public $session; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Manage the renderer. |
52
|
|
|
* |
53
|
|
|
* @var TemplateRender |
54
|
|
|
*/ |
55
|
|
|
public $renderer; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Request from client. |
59
|
|
|
* |
60
|
|
|
* @var Request |
61
|
|
|
*/ |
62
|
|
|
public $request; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Response to client. |
66
|
|
|
* |
67
|
|
|
* @var Response |
68
|
|
|
*/ |
69
|
|
|
public $response; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The debug tool used. |
73
|
|
|
* |
74
|
|
|
* @var DebugTool |
75
|
|
|
*/ |
76
|
|
|
public $debugTool; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The logger. |
80
|
|
|
* |
81
|
|
|
* @var MonologLogger |
82
|
|
|
*/ |
83
|
|
|
public $logger; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Contains dependencies. |
87
|
|
|
* |
88
|
|
|
* @var Container|null |
89
|
|
|
*/ |
90
|
|
|
protected $container; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The translator manager. |
94
|
|
|
* |
95
|
|
|
* @var Translator |
96
|
|
|
*/ |
97
|
|
|
protected $translator; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Array that contains the variables that will be passed to the template. |
101
|
|
|
* Among others it will contain the user name, the view and the controller. |
102
|
|
|
* |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
private $vars; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Controller constructor. |
109
|
|
|
*/ |
110
|
12 |
|
public function __construct() |
111
|
|
|
{ |
112
|
12 |
|
$this->shortName = ClassUtils::getShortName($this, static::class); |
113
|
12 |
|
$this->container = Container::getInstance(); |
114
|
12 |
|
$this->debugTool = DebugTool::getInstance(); |
115
|
12 |
|
$this->debugTool->startTimer($this->shortName, $this->shortName . ' Controller Constructor'); |
116
|
12 |
|
$this->request = Request::createFromGlobals(); |
117
|
12 |
|
$this->response = new Response(); |
118
|
12 |
|
$this->session = Session::getInstance(); |
119
|
12 |
|
$this->renderer = TemplateRender::getInstance(); |
120
|
12 |
|
$this->translator = Translator::getInstance(); |
121
|
12 |
|
$this->logger = Logger::getInstance()->getLogger(); |
122
|
12 |
|
$this->renderer->addVars(['ctrl' => $this,]); |
123
|
12 |
|
$this->renderer->setTemplate(strtolower($this->shortName)); |
124
|
12 |
|
$this->username = null; |
125
|
12 |
|
$this->debugTool->stopTimer($this->shortName); |
126
|
12 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add new vars to render, render the template and send the Response. |
130
|
|
|
* |
131
|
|
|
* @param array $data |
132
|
|
|
* |
133
|
|
|
* @return Response |
134
|
|
|
*/ |
135
|
5 |
|
public function sendResponseTemplate(array $data = []): Response |
136
|
|
|
{ |
137
|
5 |
|
$data = array_merge($data, ['ctrl' => $this]); |
138
|
5 |
|
return $this->sendResponse($this->renderer->render($data)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Send the Response with data received. |
143
|
|
|
* |
144
|
|
|
* @param string $reply |
145
|
|
|
* @param int $status |
146
|
|
|
* |
147
|
|
|
* @return Response |
148
|
|
|
*/ |
149
|
5 |
|
public function sendResponse(string $reply, $status = Response::HTTP_OK): Response |
150
|
|
|
{ |
151
|
5 |
|
$this->response->setStatusCode($status); |
152
|
5 |
|
$this->response->setContent($reply); |
153
|
5 |
|
return $this->response; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Send a RedirectResponse to destiny receive. |
158
|
|
|
* |
159
|
|
|
* @param string $destiny |
160
|
|
|
* |
161
|
|
|
* @return RedirectResponse |
162
|
|
|
*/ |
163
|
2 |
|
public function redirect(string $destiny = ''): RedirectResponse |
164
|
|
|
{ |
165
|
2 |
|
if (empty($destiny)) { |
166
|
|
|
$destiny = baseUrl('index.php'); |
167
|
|
|
} |
168
|
2 |
|
$vars = ['%from%' => $this->shortName, '%to%' => $destiny]; |
169
|
2 |
|
$this->logger->addDebug($this->translator->trans('redirected-from-to', $vars)); |
170
|
2 |
|
return new RedirectResponse($destiny); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $methodName |
175
|
|
|
* |
176
|
|
|
* @return Response |
177
|
|
|
*/ |
178
|
|
|
public function runMethod(string $methodName): Response |
179
|
|
|
{ |
180
|
|
|
$method = $methodName . 'Method'; |
181
|
|
|
$this->logger->addDebug($this->translator->trans('call-to', ['%called%' => $this->shortName . '->' . $method . '()'])); |
182
|
|
|
return $this->{$method}(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Add a new element to a value saved in the array that is passed to the template. |
187
|
|
|
* It is used when what we are saving is an array and we want to add a new element to that array. |
188
|
|
|
* IMPORTANT: The element only is added if is not empty. |
189
|
|
|
* |
190
|
|
|
* @param string $name |
191
|
|
|
* @param mixed $value |
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
public function addToVar(string $name, $value): void |
196
|
|
|
{ |
197
|
|
|
if (!empty($value)) { |
198
|
|
|
$this->vars[$name][] = $value; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Check if the resource is in the application's resource folder (for example, in the css or js folders |
204
|
|
|
* of the skin folder). It's a specific file. |
205
|
|
|
* |
206
|
|
|
* If it can not be found, check if it is in the templates folder (for example in the css or |
207
|
|
|
* js folders of the templates folder). It's a common file. |
208
|
|
|
* |
209
|
|
|
* If it is not in either of the two, no route is specified (it will surely give loading error). |
210
|
|
|
* |
211
|
|
|
* @param string $resourceName is the name of the file (with extension) |
212
|
|
|
* @param boolean $relative set to false for use an absolute path. |
213
|
|
|
* |
214
|
|
|
* @return string the complete path of resource. |
215
|
|
|
*/ |
216
|
|
|
public function addResource(string $resourceName, $relative = true): string |
217
|
|
|
{ |
218
|
|
|
if ($relative) { |
219
|
|
|
$uri = $this->renderer->getResourceUri($resourceName); |
220
|
|
|
if ($uri !== '') { |
221
|
|
|
return $uri; |
222
|
|
|
} |
223
|
|
|
$this->debugTool->addMessage('messages', "Relative resource '$resourceName' not found!"); |
224
|
|
|
} |
225
|
|
|
if (!file_exists($resourceName)) { |
226
|
|
|
$this->debugTool->addMessage('messages', "Absolute resource '$resourceName' not found!"); |
227
|
|
|
$this->debugTool->addMessage('messages', "File '$resourceName' not found!"); |
228
|
|
|
return ''; |
229
|
|
|
} |
230
|
|
|
return $resourceName; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* addCSS includes the CSS files to template. |
235
|
|
|
* |
236
|
|
|
* @param string $file |
237
|
|
|
* |
238
|
|
|
* @return void |
239
|
|
|
*/ |
240
|
|
|
public function addCSS(string $file): void |
241
|
|
|
{ |
242
|
|
|
$this->addToVar('cssCode', $this->addResource($file)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* addJS includes the JS files to template. |
247
|
|
|
* |
248
|
|
|
* @param string $file |
249
|
|
|
* |
250
|
|
|
* @return void |
251
|
|
|
*/ |
252
|
|
|
public function addJS(string $file): void |
253
|
|
|
{ |
254
|
|
|
$this->addToVar('jsCode', $this->addResource($file)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Return body parameters $_POST values. |
259
|
|
|
* |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
public function getArrayPost(): array |
263
|
|
|
{ |
264
|
|
|
return $this->request->request->all(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Return query string parameters $_GET values. |
269
|
|
|
* |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
|
|
public function getArrayGet(): array |
273
|
|
|
{ |
274
|
|
|
return $this->request->query->all(); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Return server and execution environment parameters from $_SERVER values. |
279
|
|
|
* |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
|
|
public function getArrayServer(): array |
283
|
|
|
{ |
284
|
|
|
return $this->request->server->all(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Return headers from $_SERVER header values. |
289
|
|
|
* |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
|
|
public function getArrayHeaders(): array |
293
|
|
|
{ |
294
|
|
|
return $this->request->headers->all(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Return uploaded files from $_FILES. |
299
|
|
|
* |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
|
|
public function getArrayFiles(): array |
303
|
|
|
{ |
304
|
|
|
return $this->request->files->all(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Return cookies from $_COOKIES. |
309
|
|
|
* |
310
|
|
|
* @return array |
311
|
|
|
*/ |
312
|
|
|
public function getArrayCookies(): array |
313
|
|
|
{ |
314
|
|
|
return $this->request->files->all(); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|