1 | <?php |
||
44 | class Controller implements \JsonSerializable |
||
45 | { |
||
46 | use Helper; |
||
47 | use ResponseTrait; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $module; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $controller; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $template; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $file; |
||
68 | |||
69 | /** |
||
70 | * @var Meta |
||
71 | */ |
||
72 | protected $meta; |
||
73 | |||
74 | /** |
||
75 | * @var Data |
||
76 | */ |
||
77 | protected $data; |
||
78 | |||
79 | /** |
||
80 | * One of HTML, JSON or empty string |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $render = 'HTML'; |
||
85 | |||
86 | /** |
||
87 | * Constructor of Statement |
||
88 | * |
||
89 | * @param string $module |
||
90 | * @param string $controller |
||
91 | */ |
||
92 | 715 | public function __construct($module, $controller) |
|
93 | { |
||
94 | 715 | $this->module = $module; |
|
95 | 715 | $this->controller = $controller; |
|
96 | 715 | $this->template = $controller . '.phtml'; |
|
97 | |||
98 | // initial default helper path |
||
99 | 715 | $this->addHelperPath(__DIR__ . '/Helper/'); |
|
100 | 715 | } |
|
101 | |||
102 | /** |
||
103 | * Check `Privilege` |
||
104 | * |
||
105 | * @throws ForbiddenException |
||
106 | */ |
||
107 | 14 | public function checkPrivilege() |
|
108 | { |
||
109 | 14 | $privilege = $this->getMeta()->getPrivilege(); |
|
110 | 13 | if ($privilege && !Acl::isAllowed($this->module, $privilege)) { |
|
111 | throw new ForbiddenException; |
||
112 | } |
||
113 | 13 | } |
|
114 | |||
115 | /** |
||
116 | * Check `Method` |
||
117 | * |
||
118 | * @throws NotAllowedException |
||
119 | */ |
||
120 | 14 | public function checkMethod() |
|
121 | { |
||
122 | 14 | $methods = $this->getMeta()->getMethod(); |
|
123 | 13 | if ($methods && !in_array(Request::getMethod(), $methods)) { |
|
124 | throw new NotAllowedException(implode(',', $methods)); |
||
125 | } |
||
126 | 13 | } |
|
127 | |||
128 | /** |
||
129 | * Check `Accept` |
||
130 | * |
||
131 | * @throws NotAcceptableException |
||
132 | */ |
||
133 | 14 | public function checkAccept() |
|
134 | { |
||
135 | // all ok for CLI |
||
136 | 14 | if (PHP_SAPI === 'cli') { |
|
137 | 14 | return; |
|
138 | } |
||
139 | |||
140 | $allowAccept = $this->getMeta()->getAccept(); |
||
141 | |||
142 | // some controllers hasn't @accept tag |
||
143 | if (!$allowAccept) { |
||
144 | // but by default allow just HTML output |
||
145 | $allowAccept = [Request::TYPE_HTML, Request::TYPE_ANY]; |
||
146 | } |
||
147 | |||
148 | // get Accept with high priority |
||
149 | $accept = Request::checkAccept($allowAccept); |
||
150 | |||
151 | // some controllers allow any type (*/*) |
||
152 | // and client doesn't send Accept header |
||
153 | if (in_array(Request::TYPE_ANY, $allowAccept) && !$accept) { |
||
154 | // all OK, controller should realize logic for response |
||
155 | return; |
||
156 | } |
||
157 | |||
158 | // some controllers allow just selected types |
||
159 | // choose MIME type by browser accept header |
||
160 | // filtered by controller @accept |
||
161 | // switch statement for this logic |
||
162 | switch ($accept) { |
||
163 | case Request::TYPE_ANY: |
||
164 | case Request::TYPE_HTML: |
||
165 | // HTML response with layout |
||
166 | break; |
||
167 | case Request::TYPE_JSON: |
||
168 | // JSON response |
||
169 | $this->template = null; |
||
170 | break; |
||
171 | default: |
||
172 | throw new NotAcceptableException; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * __invoke |
||
178 | * |
||
179 | * @param array $params |
||
180 | * |
||
181 | * @return Data |
||
182 | * @throws ControllerException |
||
183 | */ |
||
184 | 13 | public function run(array $params = []) |
|
185 | { |
||
186 | // initial variables for use inside controller |
||
187 | 13 | $module = $this->module; |
|
188 | 13 | $controller = $this->controller; |
|
189 | |||
190 | 13 | $cacheKey = "data.$module.$controller." . md5(http_build_query($params)); |
|
191 | |||
192 | 13 | $cacheTime = $this->getMeta()->getCache(); |
|
193 | |||
194 | 13 | if ($cacheTime && $cached = Cache::get($cacheKey)) { |
|
195 | $this->data = $cached; |
||
196 | return $cached; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @var \closure $controllerClosure |
||
201 | */ |
||
202 | 13 | $controllerClosure = include $this->getFile(); |
|
203 | |||
204 | 13 | if (!is_callable($controllerClosure)) { |
|
205 | throw new ControllerException("Controller is not callable '{$module}/{$controller}'"); |
||
206 | } |
||
207 | |||
208 | // process params |
||
209 | 13 | $params = $this->getMeta()->params($params); |
|
210 | |||
211 | // call Closure or Controller |
||
212 | 13 | $result = $controllerClosure(...$params); |
|
213 | |||
214 | // switch statement for result of Closure run |
||
215 | switch (true) { |
||
216 | 11 | case ($result === false): |
|
217 | // return "false" is equal to disable view and layout |
||
218 | 5 | $this->disableLayout(); |
|
219 | 5 | $this->disableView(); |
|
220 | 5 | break; |
|
221 | 6 | case is_string($result): |
|
222 | // return string variable is equal to change view template |
||
223 | $this->template = $result; |
||
224 | break; |
||
225 | 6 | case is_array($result): |
|
226 | // return associative array is equal to setup view data |
||
227 | 3 | $this->getData()->setFromArray($result); |
|
228 | 3 | break; |
|
229 | 3 | case ($result instanceof Controller): |
|
230 | $this->getData()->setFromArray($result->getData()->toArray()); |
||
231 | break; |
||
232 | } |
||
233 | |||
234 | 11 | if ($cacheTime) { |
|
235 | Cache::set( |
||
236 | $cacheKey, |
||
237 | $this->getData(), |
||
238 | $cacheTime, |
||
239 | ['system', 'data', Cache::prepare("$module.$controller")] |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | 11 | return $this->getData(); |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * Setup controller file |
||
248 | * |
||
249 | * @return void |
||
250 | * @throws ControllerException |
||
251 | */ |
||
252 | 715 | protected function setFile() |
|
263 | |||
264 | /** |
||
265 | * Get controller file path |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | 715 | protected function getFile() // : string |
|
276 | |||
277 | /** |
||
278 | * Retrieve reflection for anonymous function |
||
279 | * |
||
280 | * @return void |
||
281 | * @throws \Bluz\Common\Exception\ComponentException |
||
282 | */ |
||
283 | 715 | protected function setMeta() |
|
301 | |||
302 | /** |
||
303 | * Get meta information |
||
304 | * |
||
305 | * @return Meta |
||
306 | */ |
||
307 | 715 | public function getMeta() |
|
314 | |||
315 | /** |
||
316 | * Assign key/value pair to Data object |
||
317 | * |
||
318 | * @param string $key |
||
319 | * @param mixed $value |
||
320 | * |
||
321 | * @return Controller |
||
322 | */ |
||
323 | public function assign($key, $value) |
||
328 | |||
329 | /** |
||
330 | * Get controller Data container |
||
331 | * |
||
332 | * @return Data |
||
333 | */ |
||
334 | 11 | public function getData() |
|
341 | |||
342 | /** |
||
343 | * Specify data which should be serialized to JSON |
||
344 | * |
||
345 | * @return Data |
||
346 | */ |
||
347 | public function jsonSerialize() |
||
351 | |||
352 | /** |
||
353 | * Magic cast to string |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | 2 | public function __toString() |
|
390 | } |
||
391 |