1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
httpapplication.php
См. документацию.
1<?php
8namespace Bitrix\Main;
9
10use Bitrix\Main\Config\Configuration;
11use Bitrix\Main\Engine\AutoWire;
12use Bitrix\Main\Engine\Controller;
13use Bitrix\Main\Engine\ControllerBuilder;
14use Bitrix\Main\Engine\CurrentUser;
15use Bitrix\Main\Engine\Response\AjaxJson;
16use Bitrix\Main\Engine\Router;
17use Bitrix\Main\Engine\JsonPayload;
18use Bitrix\Main\Security\Sign\BadSignatureException;
19use Bitrix\Main\UI\PageNavigation;
20
25{
26 public const EXCEPTION_UNKNOWN_CONTROLLER = 221001;
27
33 protected function initializeContext(array $params)
34 {
35 $context = new HttpContext($this);
36
37 $server = new Server($params['server']);
38
40 $server,
41 $params['get'],
42 $params['post'],
43 $params['files'],
44 $params['cookie']
45 );
46
47 $response = new HttpResponse();
48
49 $context->initialize($request, $response, $server, ['env' => $params['env']]);
50
51 $this->setContext($context);
52 }
53
55 {
57 }
58
62 public function start()
63 {
64 $this->context->getRequest()->decodeJson();
65 }
66
70 public function finish()
71 {
72 }
73
74 private function getSourceParametersList()
75 {
76 if (!$this->context->getServer()->get('HTTP_BX_AJAX_QB'))
77 {
78 return array(
79 $this->context->getRequest()->getPostList(),
80 $this->context->getRequest()->getQueryList(),
81 );
82 }
83
84 return array(
85 Web\Json::decode($this->context->getRequest()->getPost('bx_data'))
86 );
87 }
88
94 public function run()
95 {
96 try
97 {
98 $router = new Router($this->context->getRequest());
99
102 [$controller, $actionName] = $router->getControllerAndAction();
103 if (!$controller)
104 {
105 throw new SystemException('Could not find controller for the request', self::EXCEPTION_UNKNOWN_CONTROLLER);
106 }
107
108 $this->runController($controller, $actionName);
109 }
110 catch (\Throwable $e)
111 {
112 $errorCollection = new ErrorCollection();
113
114 $this->processRunError($e, $errorCollection);
115 $this->finalizeControllerResult($controller ?? null, null, $errorCollection);
116 }
117 }
118
124 final public function runController($controller, $action): void
125 {
126 $result = null;
127 $errorCollection = new ErrorCollection();
128
129 try
130 {
131 if (is_string($controller))
132 {
133 $controller = ControllerBuilder::build($controller, [
134 'scope' => Controller::SCOPE_AJAX,
135 'currentUser' => CurrentUser::get(),
136 ]);
137 }
138
139 $this->registerAutoWirings();
140
141 $result = $controller->run($action, $this->getSourceParametersList());
142 $errorCollection->add($controller->getErrors());
143 }
144 catch (\Throwable $e)
145 {
146 $this->processRunError($e, $errorCollection);
147 }
148 finally
149 {
150 $this->finalizeControllerResult($controller, $result, $errorCollection);
151 }
152 }
153
159 private function finalizeControllerResult($controller, $result, ErrorCollection $errorCollection): void
160 {
161 $response = $this->buildResponse($result, $errorCollection);
162 $response = $this->context->getResponse()->copyHeadersTo($response);
163
164 if ($controller)
165 {
166 $controller->finalizeResponse($response);
167 }
168
169 $this->context->setResponse($response);
170
171 //todo exit code in Response?
172 $this->end(0, $response);
173 }
174
175 private function shouldWriteToLogException(\Throwable $e): bool
176 {
177 if ($e instanceof AutoWire\BinderArgumentException)
178 {
179 return false;
180 }
181
182 if ($e instanceof BadSignatureException)
183 {
184 return false;
185 }
186
187 $unnecessaryCodes = [
188 self::EXCEPTION_UNKNOWN_CONTROLLER,
189 Router::EXCEPTION_INVALID_COMPONENT_INTERFACE,
190 Router::EXCEPTION_INVALID_COMPONENT,
191 Router::EXCEPTION_INVALID_AJAX_MODE,
192 Router::EXCEPTION_NO_MODULE,
193 Router::EXCEPTION_INVALID_MODULE_NAME,
194 Router::EXCEPTION_INVALID_COMPONENT_NAME,
195 Router::EXCEPTION_NO_COMPONENT,
196 Router::EXCEPTION_NO_COMPONENT_AJAX_CLASS,
197 ];
198
199 if ($e instanceof SystemException)
200 {
201 if (\in_array($e->getCode(), $unnecessaryCodes, true))
202 {
203 return false;
204 }
205
206 if ($this->isDebugMode() && ($e->getCode() === Router::EXCEPTION_NO_CONFIGURATION))
207 {
208 return true;
209 }
210 }
211
212 return true;
213 }
214
215 private function isDebugMode(): bool
216 {
217 $exceptionHandling = Configuration::getValue('exception_handling');
218
219 return !empty($exceptionHandling['debug']);
220 }
221
222 private function processRunError(\Throwable $e, ErrorCollection $errorCollection): void
223 {
224 $errorCollection[] = new Error($e->getMessage(), $e->getCode());
225
226 $debugMode = $this->isDebugMode();
227 if ($debugMode)
228 {
229 $errorCollection[] = new Error(Diag\ExceptionHandlerFormatter::format($e));
230 if ($e->getPrevious())
231 {
232 $errorCollection[] = new Error(Diag\ExceptionHandlerFormatter::format($e->getPrevious()));
233 }
234 }
235
236 if ($debugMode || $this->shouldWriteToLogException($e))
237 {
238 $this->getExceptionHandler()->writeToLog($e);
239 }
240 }
241
242 private function registerAutoWirings()
243 {
244 AutoWire\Binder::registerGlobalAutoWiredParameter(new AutoWire\Parameter(
245 PageNavigation::class,
246 static function() {
247 $pageNavigation = new PageNavigation('nav');
248 $pageNavigation
249 ->setPageSizes(range(1, 50))
250 ->initFromUri()
251 ;
252
253 return $pageNavigation;
254 }
255 ));
256
257 AutoWire\Binder::registerGlobalAutoWiredParameter(new AutoWire\Parameter(
258 JsonPayload::class,
259 static function() {
260 return new JsonPayload();
261 }
262 ));
263
264 AutoWire\Binder::registerGlobalAutoWiredParameter(new AutoWire\Parameter(
265 CurrentUser::class,
266 static function() {
267 return CurrentUser::get();
268 }
269 ));
270 }
271
281 private function buildResponse($actionResult, ErrorCollection $errorCollection)
282 {
283 if ($actionResult instanceof HttpResponse)
284 {
285 return $actionResult;
286 }
287
288 if (!$errorCollection->isEmpty())
289 {
290 //todo There is opportunity to create DenyError() and recognize AjaxJson::STATUS_DENIED by this error.
291
292 return new AjaxJson(
293 $actionResult,
294 AjaxJson::STATUS_ERROR,
295 $errorCollection
296 );
297 }
298
299 return new AjaxJson($actionResult);
300 }
301}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
setContext(Context $context)
Определения application.php:615
getExceptionHandler()
Определения application.php:585
end($status=0, Response $response=null)
Определения application.php:305
createExceptionHandlerOutput()
Определения httpapplication.php:54
runController($controller, $action)
Определения httpapplication.php:124
initializeContext(array $params)
Определения httpapplication.php:33
const EXCEPTION_UNKNOWN_CONTROLLER
Определения httpapplication.php:26
Определения server.php:11
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
trait Error
Определения error.php:11
$router
Определения routing_index.php:31
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$response
Определения result.php:21
$action
Определения file_dialog.php:21