1 | <?php |
||
13 | class Response |
||
14 | { |
||
15 | private $config; |
||
16 | private $container; |
||
17 | private $view; |
||
18 | private $session; |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $statusCode = 200; |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $headers = []; |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $cookies = []; |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $body = ''; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $data = []; |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $contentType = Selami\Router::HTML; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $redirect; |
||
49 | private $downloadFilePath; |
||
50 | private $downloadFileName; |
||
51 | private $customContentType; |
||
52 | |||
53 | public function __construct(ContainerInterface $container) |
||
54 | { |
||
55 | $this->container = $container; |
||
56 | $this->config = $container->get(ZendConfig::class); |
||
57 | } |
||
58 | |||
59 | private function checkTemplateFile($template, $type, $controller) : void |
||
60 | { |
||
61 | if (!file_exists($this->config->app->get('templates_dir', './templates') .'/'. $template)) { |
||
62 | $message = sprintf( |
||
63 | '%s template file not found! %s needs a main template file at: %s', |
||
64 | $type, |
||
65 | $controller, |
||
66 | $this->config['app_dir'] .'/'. $template |
||
67 | ); |
||
68 | throw new \DomainException($message); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public function setResponse(int $returnType, array $actionOutput, string $controller) : void |
||
73 | { |
||
74 | switch ($returnType) { |
||
75 | case Selami\Router::HTML: |
||
76 | $this->setRenderedResponse(Selami\Router::HTML, $actionOutput, $controller); |
||
77 | break; |
||
78 | case Selami\Router::JSON: |
||
79 | $this->setJsonResponse($actionOutput); |
||
80 | break; |
||
81 | case Selami\Router::TEXT: |
||
82 | $this->setRenderedResponse(Selami\Router::TEXT, $actionOutput, $controller); |
||
83 | break; |
||
84 | case Selami\Router::XML: |
||
85 | $this->setRenderedResponse(Selami\Router::XML, $actionOutput, $controller); |
||
86 | break; |
||
87 | case Selami\Router::DOWNLOAD: |
||
88 | $this->setDownloadResponse($actionOutput); |
||
89 | break; |
||
90 | case Selami\Router::CUSTOM: |
||
91 | $this->setRenderedResponse(Selami\Router::CUSTOM, $actionOutput, $controller); |
||
92 | break; |
||
93 | case Selami\Router::REDIRECT: |
||
94 | $this->setRedirectResponse($actionOutput); |
||
95 | break; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public function setRedirectResponse(array $actionOutput) : void |
||
100 | { |
||
101 | $this->contentType = Selami\Router::REDIRECT; |
||
102 | if (isset($actionOutput['meta']['type']) && $actionOutput['meta']['type'] === Dispatcher::REDIRECT) { |
||
103 | $this->contentType = Selami\Router::REDIRECT; |
||
104 | $this->statusCode = $actionOutput['status'] ?? 302; |
||
105 | $this->redirect = $actionOutput['meta']['redirect_url']; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | public function setDownloadResponse(array $actionOutput) : void |
||
110 | { |
||
111 | $this->contentType = Selami\Router::DOWNLOAD; |
||
112 | if (isset($actionOutput['meta']['type']) ?? $actionOutput['meta']['type'] === Dispatcher::DOWNLOAD) { |
||
113 | $statusCode = $actionOutput['status'] ?? 200; |
||
114 | $this->statusCode = (int) $statusCode; |
||
115 | $this->downloadFilePath = $actionOutput['meta']['download_file_path']; |
||
116 | $this->downloadFileName = $actionOutput['meta']['download_file_name'] ?? date('Ymdhis'); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | public function setJsonResponse(array $actionOutput) : void |
||
121 | { |
||
122 | $this->contentType = Selami\Router::JSON; |
||
123 | |||
124 | if (!is_array($actionOutput)) { |
||
125 | $actionOutput = ['status' => 500, 'error' => 'Internal Server Error']; |
||
126 | } |
||
127 | if (!isset($actionOutput['status'])) { |
||
128 | $actionOutput['status'] = 200; |
||
129 | } |
||
130 | $status = (int) $actionOutput['status']; |
||
131 | $this->statusCode = $status; |
||
132 | $this->data = $actionOutput; |
||
133 | } |
||
134 | |||
135 | private function setRenderedResponse(int $returnType, array $actionOutput, string $controllerClass) : void |
||
136 | { |
||
137 | $this->useSession(); |
||
138 | $this->useView($this->container->get(ViewInterface::class)); |
||
139 | $this->view->addGlobal('defined', get_defined_constants(true)['user'] ?? []); |
||
140 | $this->view->addGlobal('session', $this->session->all()); |
||
141 | $this->renderResponse($returnType, $actionOutput, $controllerClass); |
||
142 | } |
||
143 | |||
144 | private function renderResponse(int $returnType, array $actionOutput, string $controllerClass) : void |
||
145 | { |
||
146 | $paths = explode("\\", $controllerClass); |
||
147 | $templateFile = array_pop($paths); |
||
148 | $templateFolder = array_pop($paths); |
||
149 | $template = CaseConverter::toSnakeCase($templateFolder) . '/' . 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.