37 $server =
new Server($params[
'server']);
49 $context->initialize($request, $response, $server, [
'env' => $params[
'env']]);
64 $this->context->getRequest()->decodeJson();
74 private function getSourceParametersList()
76 if (!$this->context->getServer()->get(
'HTTP_BX_AJAX_QB'))
79 $this->context->getRequest()->getPostList(),
80 $this->context->getRequest()->getQueryList(),
85 Web\Json::decode($this->context->getRequest()->getPost(
'bx_data'))
98 $router =
new Router($this->context->getRequest());
102 [$controller, $actionName] =
$router->getControllerAndAction();
105 throw new SystemException(
'Could not find controller for the request', self::EXCEPTION_UNKNOWN_CONTROLLER);
110 catch (\Throwable $e)
112 $errorCollection =
new ErrorCollection();
114 $this->processRunError($e, $errorCollection);
115 $this->finalizeControllerResult($controller ??
null,
null, $errorCollection);
131 if (is_string($controller))
133 $controller = ControllerBuilder::build($controller, [
135 'currentUser' => CurrentUser::get(),
139 $this->registerAutoWirings();
141 $result = $controller->run($action, $this->getSourceParametersList());
142 $errorCollection->add($controller->getErrors());
144 catch (\Throwable $e)
146 $this->processRunError($e, $errorCollection);
150 $this->finalizeControllerResult($controller, $result, $errorCollection);
159 private function finalizeControllerResult($controller, $result,
ErrorCollection $errorCollection): void
161 $response = $this->buildResponse($result, $errorCollection);
162 $response = $this->context->getResponse()->copyHeadersTo($response);
166 $controller->finalizeResponse($response);
169 $this->context->setResponse($response);
172 $this->
end(0, $response);
175 private function shouldWriteToLogException(\Throwable $e): bool
177 if ($e instanceof AutoWire\BinderArgumentException)
182 $unnecessaryCodes = [
184 Router::EXCEPTION_INVALID_COMPONENT_INTERFACE,
185 Router::EXCEPTION_INVALID_COMPONENT,
186 Router::EXCEPTION_INVALID_AJAX_MODE,
187 Router::EXCEPTION_NO_MODULE,
188 Router::EXCEPTION_INVALID_MODULE_NAME,
189 Router::EXCEPTION_INVALID_COMPONENT_NAME,
190 Router::EXCEPTION_NO_COMPONENT,
191 Router::EXCEPTION_NO_COMPONENT_AJAX_CLASS,
194 if ($e instanceof SystemException && in_array($e->getCode(), $unnecessaryCodes,
true))
202 private function processRunError(\Throwable $e, ErrorCollection $errorCollection): void
204 $errorCollection[] =
new Error($e->getMessage(), $e->getCode());
205 $exceptionHandling = Configuration::getValue(
'exception_handling');
206 $debugMode = !empty($exceptionHandling[
'debug']);
209 $errorCollection[] =
new Error(Diag\ExceptionHandlerFormatter::format($e));
210 if ($e->getPrevious())
212 $errorCollection[] =
new Error(Diag\ExceptionHandlerFormatter::format($e->getPrevious()));
216 if ($debugMode || $this->shouldWriteToLogException($e))
222 private function registerAutoWirings()
224 AutoWire\Binder::registerGlobalAutoWiredParameter(
new AutoWire\Parameter(
225 PageNavigation::class,
227 $pageNavigation =
new PageNavigation(
'nav');
229 ->setPageSizes(range(1, 50))
233 return $pageNavigation;
237 AutoWire\Binder::registerGlobalAutoWiredParameter(
new AutoWire\Parameter(
240 return new JsonPayload();
244 AutoWire\Binder::registerGlobalAutoWiredParameter(
new AutoWire\Parameter(
247 return CurrentUser::get();
261 private function buildResponse($actionResult, ErrorCollection $errorCollection)
263 if ($actionResult instanceof HttpResponse)
265 return $actionResult;
268 if (!$errorCollection->isEmpty())
274 AjaxJson::STATUS_ERROR,
279 return new AjaxJson($actionResult);
setContext(Context $context)
end($status=0, Response $response=null)
runController($controller, $action)
initializeContext(array $params)