|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Selami\Foundation; |
|
5
|
|
|
|
|
6
|
|
|
use Selami as s; |
|
7
|
|
|
use Zend\Config\Config as ZendConfig; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Selami\View\ViewInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
11
|
|
|
|
|
12
|
|
|
class Response |
|
13
|
|
|
{ |
|
14
|
|
|
private $config; |
|
15
|
|
|
private $container; |
|
16
|
|
|
private $view; |
|
17
|
|
|
private $session; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
private $statusCode = 200; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $headers = []; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $cookies = []; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $body = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $data = []; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $contentType = 'html'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $redirect; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(ContainerInterface $container) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->container = $container; |
|
52
|
|
|
$this->config = $container->get(ZendConfig::class); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function checkTemplateFile($template, $type, $controller) : void |
|
56
|
|
|
{ |
|
57
|
|
|
if (!file_exists($this->config->app->get('templates_dir', './templates') .'/'. $template)) { |
|
58
|
|
|
$message = sprintf( |
|
59
|
|
|
'%s template file not found! %s needs a main template file at: %s', |
|
60
|
|
|
$type, |
|
61
|
|
|
$controller, |
|
62
|
|
|
$this->config['app_dir'] .'/'. $template |
|
63
|
|
|
); |
|
64
|
|
|
throw new \DomainException($message); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function returnRedirect(array $functionOutput, string $controller) : void |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$this->contentType = 'redirect'; |
|
71
|
|
|
if (isset($functionOutput['redirect'])) { |
|
72
|
|
|
$this->contentType = 'redirect'; |
|
73
|
|
|
$this->statusCode = 301; |
|
74
|
|
|
$this->redirect = $functionOutput['redirect']; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function returnJson(array $functionOutput, string $controllerClass) : void |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$this->contentType = 'json'; |
|
81
|
|
|
if (isset($functionOutput['redirect'])) { |
|
82
|
|
|
$this->contentType = 'redirect'; |
|
83
|
|
|
$this->statusCode = 301; |
|
84
|
|
|
$this->redirect = $functionOutput['redirect']; |
|
85
|
|
|
} |
|
86
|
|
|
if (!is_array($functionOutput)) { |
|
87
|
|
|
$functionOutput = ['status' => 500, 'error' => 'Internal Server Error']; |
|
88
|
|
|
} elseif (!isset($functionOutput['status'])) { |
|
89
|
|
|
$functionOutput['status'] = 200; |
|
90
|
|
|
} |
|
91
|
|
|
$status = (int) $functionOutput['status']; |
|
92
|
|
|
$this->statusCode = $status; |
|
93
|
|
|
$this->data = $functionOutput; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
public function returnHtml(array $functionOutput, string $controllerClass) : void |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$this->useSession(); |
|
99
|
|
|
$this->useView($this->container->get(ViewInterface::class)); |
|
100
|
|
|
$paths = explode("\\", $controllerClass); |
|
101
|
|
|
$templateFile = array_pop($paths); |
|
102
|
|
|
$templateFolder = array_pop($paths); |
|
103
|
|
|
$template = strtolower($templateFolder) . '/' . strtolower($templateFile) . '.twig'; |
|
104
|
|
|
|
|
105
|
|
|
if (isset($functionOutput['redirect'])) { |
|
106
|
|
|
$this->contentType = 'redirect'; |
|
107
|
|
|
$this->statusCode = 301; |
|
108
|
|
|
$this->redirect = $functionOutput['redirect']; |
|
109
|
|
|
} |
|
110
|
|
|
$this->view->addGlobal('defined', get_defined_constants(true)['user'] ?? []); |
|
111
|
|
|
$this->view->addGlobal('session', $this->session->all()); |
|
112
|
|
|
$this->checkTemplateFile($template, 'Method\'s', $controllerClass); |
|
113
|
|
|
$functionOutput['data'] = $functionOutput['data'] ?? []; |
|
114
|
|
|
$functionOutput['app_content'] = $this->view->render($template, $functionOutput['data']); |
|
115
|
|
|
$mainTemplateName = $functionOutput['app_main_template'] ?? 'default'; |
|
116
|
|
|
$mainTemplate = '_' . strtolower($mainTemplateName) . '.twig'; |
|
117
|
|
|
$this->checkTemplateFile($mainTemplate, 'Main', $controllerClass); |
|
118
|
|
|
$this->body = $this->view->render($mainTemplate, $functionOutput); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
View Code Duplication |
public function returnText(array $functionOutput, string $controllerClass) : void |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$this->useSession(); |
|
125
|
|
|
$this->useView($this->container->get(ViewInterface::class)); |
|
126
|
|
|
$paths = explode("\\", $controllerClass); |
|
127
|
|
|
$templateFile = array_pop($paths); |
|
128
|
|
|
$templateFolder = array_pop($paths); |
|
129
|
|
|
$template = strtolower($templateFolder) . '/' . strtolower($templateFile) . '.twig'; |
|
130
|
|
|
if (isset($functionOutput['redirect'])) { |
|
131
|
|
|
$this->contentType = 'redirect'; |
|
132
|
|
|
$this->statusCode = 301; |
|
133
|
|
|
$this->redirect = $functionOutput['redirect']; |
|
134
|
|
|
} |
|
135
|
|
|
$this->view->addGlobal('defined', get_defined_constants(true)['user'] ?? []); |
|
136
|
|
|
$this->view->addGlobal('session', $this->session->all()); |
|
137
|
|
|
$this->checkTemplateFile($template, 'Method\'s', $controllerClass); |
|
138
|
|
|
$functionOutput['data'] = $functionOutput['data'] ?? []; |
|
139
|
|
|
$functionOutput['app_content'] = $this->view->render($template, $functionOutput['data']); |
|
140
|
|
|
$mainTemplateName = $functionOutput['layout'] ?? 'default'; |
|
141
|
|
|
$mainTemplate = '_' . strtolower($mainTemplateName) . '.twig'; |
|
142
|
|
|
$this->checkTemplateFile($mainTemplate, 'Main', $controllerClass); |
|
143
|
|
|
$this->contentType = 'text'; |
|
144
|
|
|
$this->body = $this->view->render($mainTemplate, $functionOutput); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function notFound($status = 404, $returnType = 'html', $message = 'Not Found') : void |
|
148
|
|
|
{ |
|
149
|
|
|
if ($returnType == 'json') { |
|
150
|
|
|
$this->body = ['status' => $status, 'message' => $message]; |
|
|
|
|
|
|
151
|
|
|
} else { |
|
152
|
|
|
$this->useView($this->container->get('view')); |
|
153
|
|
|
$notFoundTemplate = '_404.twig'; |
|
154
|
|
|
$this->contentType = $returnType; |
|
155
|
|
|
$this->body = $this->view->render( |
|
156
|
|
|
$notFoundTemplate, |
|
157
|
|
|
['message' => $message, 'status' => $status] |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
$this->statusCode = $status; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function useView(ViewInterface $view) : void |
|
164
|
|
|
{ |
|
165
|
|
|
$this->view = $view; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
private function useSession() : void |
|
169
|
|
|
{ |
|
170
|
|
|
$this->session = $this->container->get(Session::class); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function setHeaders() : void |
|
174
|
|
|
{ |
|
175
|
|
|
$this->headers['X-Powered-By'] = 'r/selami'; |
|
176
|
|
|
$this->headers['X-Frame-Options'] = 'SAMEORIGIN'; |
|
177
|
|
|
$this->headers['X-XSS-Protection'] = '1; mode=block'; |
|
178
|
|
|
$this->headers['Strict-Transport-Security'] = 'max-age=31536000'; |
|
179
|
|
|
if (array_key_exists('headers', $this->config) && is_array($this->config['headers'])) { |
|
180
|
|
|
foreach ($this->config['headers'] as $header => $value) { |
|
181
|
|
|
$this->headers[$header] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function getResponse() : array |
|
187
|
|
|
{ |
|
188
|
|
|
$headers = $this->config['headers'] ?? null; |
|
189
|
|
|
$this->setHeaders($headers); |
|
|
|
|
|
|
190
|
|
|
return [ |
|
191
|
|
|
'statusCode' => $this->statusCode, |
|
192
|
|
|
'headers' => $this->headers, |
|
193
|
|
|
'cookies' => $this->cookies, |
|
194
|
|
|
'body' => (string) $this->body, |
|
195
|
|
|
'data' => $this->data, |
|
196
|
|
|
'contentType' => $this->contentType, |
|
197
|
|
|
'redirect' => $this->redirect |
|
198
|
|
|
]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function sendResponse() : void |
|
202
|
|
|
{ |
|
203
|
|
|
$response = new s\Http\Response(); |
|
204
|
|
|
$response->setHeaders($this->headers); |
|
205
|
|
|
$response->setStatusCode($this->statusCode); |
|
206
|
|
|
switch ($this->contentType) { |
|
207
|
|
|
case 'redirect': |
|
|
|
|
|
|
208
|
|
|
$response->setOutputType('redirect'); |
|
209
|
|
|
$response->setRedirect($this->redirect); |
|
210
|
|
|
break; |
|
211
|
|
|
case 'json': |
|
|
|
|
|
|
212
|
|
|
$response->setOutputType('json'); |
|
213
|
|
|
$response->setData($this->data); |
|
214
|
|
|
break; |
|
215
|
|
|
case 'text': |
|
|
|
|
|
|
216
|
|
|
$response->setOutputType('text'); |
|
217
|
|
|
$response->setBody($this->body); |
|
218
|
|
|
break; |
|
219
|
|
|
case 'html': |
|
|
|
|
|
|
220
|
|
|
default: |
|
|
|
|
|
|
221
|
|
|
$response->setOutputType('html'); |
|
222
|
|
|
$response->setBody($this->body); |
|
223
|
|
|
break; |
|
224
|
|
|
} |
|
225
|
|
|
$response->send(); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.