1 | <?php |
||
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 = Selami\Router::HTML; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $redirect; |
||
48 | private $downloadFilePath; |
||
49 | private $downloadFileName; |
||
50 | private $customContentType; |
||
51 | |||
52 | public function __construct(ContainerInterface $container) |
||
57 | |||
58 | private function checkTemplateFile($template, $type, $controller) : void |
||
70 | |||
71 | public function setResponse(int $returnType, array $actionOutput, string $controller) : void |
||
97 | |||
98 | public function setRedirectResponse(array $actionOutput) : void |
||
107 | |||
108 | public function setDownloadResponse(array $actionOutput) : void |
||
118 | |||
119 | public function setJsonResponse(array $actionOutput) : void |
||
120 | { |
||
121 | $this->contentType = Selami\Router::JSON; |
||
122 | |||
123 | if (!is_array($actionOutput)) { |
||
124 | $actionOutput = ['status' => 500, 'error' => 'Internal Server Error']; |
||
125 | } |
||
126 | if (!isset($actionOutput['status'])) { |
||
127 | $actionOutput['status'] = 200; |
||
128 | } |
||
129 | $status = (int) $actionOutput['status']; |
||
130 | $this->statusCode = $status; |
||
131 | $this->data = $actionOutput; |
||
132 | } |
||
133 | |||
134 | private function setRenderedResponse(int $returnType, array $actionOutput, string $controllerClass) : void |
||
135 | { |
||
136 | $this->useSession(); |
||
137 | $this->useView($this->container->get(ViewInterface::class)); |
||
138 | $this->view->addGlobal('defined', get_defined_constants(true)['user'] ?? []); |
||
139 | $this->view->addGlobal('session', $this->session->all()); |
||
140 | $this->renderResponse($returnType, $actionOutput, $controllerClass); |
||
141 | } |
||
142 | |||
143 | private function renderResponse(int $returnType, array $actionOutput, string $controllerClass) : void |
||
144 | { |
||
145 | $paths = explode("\\", $controllerClass); |
||
146 | $templateFile = array_pop($paths); |
||
147 | $templateFolder = array_pop($paths); |
||
148 | $template = CaseConverter::toSnakeCase($templateFolder) |
||
149 | . '/' . CaseConverter::toSnakeCase($templateFile) . '.twig'; |
||
150 | $this->checkTemplateFile($template, 'Method\'s', $controllerClass); |
||
151 | $actionOutput['data'] = $actionOutput['data'] ?? []; |
||
152 | $output = [ |
||
153 | 'status' => $actionOutput['status'] ?? 200, |
||
154 | 'meta' => $actionOutput['meta'] ?? [], |
||
155 | 'app' => null |
||
156 | ]; |
||
157 | $output['app']['_content'] = $this->view->render($template, $actionOutput['data']); |
||
158 | $mainTemplateName = $actionOutput['meta']['layout'] ?? 'default'; |
||
159 | $mainTemplate = '_' . CaseConverter::toSnakeCase($mainTemplateName) . '.twig'; |
||
160 | $this->checkTemplateFile($mainTemplate, 'Layout', $controllerClass); |
||
161 | $this->contentType = $returnType; |
||
|
|||
162 | if ($returnType === Selami\Router::CUSTOM) { |
||
163 | $this->customContentType = $actionOutput['meta']['content_type'] ?? 'plain/text'; |
||
164 | } |
||
165 | $this->body = $this->view->render($mainTemplate, $output); |
||
166 | } |
||
167 | |||
168 | public function notFound($status = 404, $returnType = Selami\Router::HTML, $message = 'Not Found') : void |
||
183 | |||
184 | private function useView(ViewInterface $view) : void |
||
188 | |||
189 | private function useSession() : void |
||
193 | |||
194 | private function setHeaders() : void |
||
206 | |||
207 | public function getResponse() : array |
||
221 | |||
222 | public function sendResponse() : void |
||
262 | } |
||
263 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.