public function index($minified) { /** @var MMinifiedDatum $minify */ if ($minify = $minified[0] ?? null) { $this->response->setStatusCode(200); $this->response->setHeader('Content-Type', preg_match('/\\.js$/', $minify->name) ? 'application/javascript' : 'text/css'); $this->response->setContent($minify->content); } else { $this->response->setStatusCode(404); } return $this->response; }
public function run() { try { $this->boot(); $event = new RequestEvent($this->request); $this->dispatcher->fire(RequestEvent::REQUEST_HANDLE, $event); $this->response = $event->getResponse(); } catch (AuthError $e) { $this->response->setStatusCode(401); } catch (ResourceNotFoundException $e) { $this->response->setStatusCode(404); } catch (\Throwable $e) { http_response_code(500); $this->response->setStatusCode(500); if ($e instanceof PrintableError) { $this->response->setContent($e->getMessage()); } else { if (preg_match("/Duplicate entry '(.*?)' for key '(.*?)'/", $e->getMessage(), $matches)) { $errorStr = sprintf("DUPLICATE: %s ('%s' is already in use).", ucfirst($matches[2]), $matches[1]); } else { if ($this->debug->enabled()) { throw $e; } else { $event = new ExceptionEvent($e); $this->dispatcher->fire(ExceptionEvent::EXCEPTION_UNHANDLED, $event); } if (preg_match("/Unknown column 'user_id'.*from (.*?) /", $e->getMessage(), $matches)) { $errorStr = sprintf("Permission::SAME_USER requires a `user_id` column (which is missing in table {$matches['1']})"); } else { $errorStr = 'Internal server error'; } } $this->response->setContent($errorStr); } } if ($this->response->getStatusCode() === 200) { $this->dispatcher->fire(ResponseEvent::RESPONSE_RENDER, new ResponseEvent($this->response)); } else { $this->dispatcher->fire(ResponseEvent::RESPONSE_ERROR, new ResponseEvent($this->response)); } }
public function execute(ControllerEvent $event) { try { $params = $event->getParams(true) ?? []; $ajaxReq = $params['_contentType'] === 'ajax'; if ($params['_method'] === 'GET' && ($parents = $params['_parents'] ?? null)) { $alias = $ajaxReq ? $params['alias'] : null; if ($models = $this->modelLoader->loadModels($parents, $alias)) { $event->setParam('_models', $models); foreach ($models as $key => $value) { $event->setParam($key, $value); } } foreach ($parents as $key => $value) { $event->setParam("_{$key}", $models[$key] ?? []); } } $args = array_merge([':event' => $event], $event->getParams() ?? []); $action = $this->getController($event->getController()); $final = true; $response = $output = $this->injector->execute($action, $args); if ($response instanceof Redirection) { $this->dispatcher->fire(RedirectEvent::REDIRECT, new RedirectEvent($response)); //chance to do something with redirection $response->redirect(); //exists } else { if ($response instanceof HttpResponseEx) { $event->setResponse($this->response); } else { if ($response instanceof View) { if ($ajaxReq && !empty($alias) && !empty($models)) { $output = '{}'; /** @var CollectionEx $model */ foreach ($models as $tlp => $model) { if ($array = $model->toArray()) { if ($child = $tlp === $alias ? $array : $this->findChildByAlias($array, $alias)) { $output = json_encode($child, JSON_PRETTY_PRINT); } } } } else { /** @var View $view */ $view = $response; $final = $response->isFinal(); $this->viewParser->setHelpers($view->getHelpers()); $this->viewParser->setLayout($view->getLayout()); $this->viewParser->setAdditionalLayoutFiles($view->getAdditionalLayoutFiles()); $this->viewParser->setVars(array_merge($event->getParams(true) ?? [], $view->getVars() ?? [])); if ($content = $view->getContent()) { if (!empty($view->getViewFile())) { trigger_error('You should only specify either content or view file'); } $this->viewParser->setContent($content); } elseif ($templatePath = $view->getViewFile()) { $this->viewParser->loadViewFile($templatePath, $view->isPathLayouts()); } elseif (empty($view->getViewFile())) { if (is_string($event->getController())) { @(list($class) = explode('@', $event->getController(), 2)); $this->viewParser->loadViewFile($class, $view->isPathLayouts()); } else { throw new ControllerError("View must be explicitly set (when Controller is not a string)"); } } $output = $this->viewParser->render(); } } $this->response->setStatusCode(200); $this->response->setContent(is_string($output) ? $output : ''); $this->response->setFinal($final); $event->setResponse($this->response); } } } catch (InjectionException $e) { throw new ControllerError(sprintf("Unable to run controller: %s [%s]", $e->getMessage(), var_export($event->getController(), true))); } }