1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
restmanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Rest\Engine;
4
5use Bitrix\Main\Config\Configuration;
6use Bitrix\Main\Context;
7use Bitrix\Main\Engine;
8use Bitrix\Main\Engine\AutoWire;
9use Bitrix\Main\Engine\Controller;
10use Bitrix\Main\Engine\Resolver;
11use Bitrix\Main\Errorable;
12use Bitrix\Main\Error;
13use Bitrix\Main\ErrorCollection;
14use Bitrix\Main\HttpResponse;
15use Bitrix\Main\Type\Contract;
16use Bitrix\Main\Type\Date;
17use Bitrix\Main\Type\DateTime;
18use Bitrix\Main\UI\PageNavigation;
19use Bitrix\Main\Web\Uri;
20use Bitrix\Rest\Engine\ScopeManager;
21use Bitrix\Rest\RestException;
22
24{
25 public const DONT_CALCULATE_COUNT = -1;
26
28 protected $restServer;
30 private $pageNavigation;
32 private $calculateTotalCount = true;
33
34 public static function onFindMethodDescription($potentialAction)
35 {
36 $restManager = new static();
37 $potentialActionData = ScopeManager::getInstance()->getMethodInfo($potentialAction);
38
39 $request = new \Bitrix\Main\HttpRequest(
40 Context::getCurrent()->getServer(),
41 ['action' => $potentialActionData['method']],
42 [], [], []
43 );
44
46 $controllersConfig = Configuration::getInstance($router->getModule());
47 if (empty($controllersConfig['controllers']['restIntegration']['enabled']))
48 {
49 return false;
50 }
51
53 list($controller) = $router->getControllerAndAction();
54 if (!$controller || $controller instanceof Engine\DefaultController)
55 {
56 return false;
57 }
58
59 return [
60 'scope' => static::getModuleScopeAlias($potentialActionData['scope']),
61 'callback' => [
62 $restManager, 'processMethodRequest'
63 ]
64 ];
65 }
66
67 public static function getModuleScopeAlias($moduleId)
68 {
69 if($moduleId === 'tasks')
70 {
71 return 'task';
72 }
73
74 return $moduleId;
75 }
76
77 private static function getAlternativeScope($scope): ?array
78 {
79 if ($scope === \Bitrix\Rest\Api\User::SCOPE_USER)
80 {
81 return [
84 ];
85 }
86
87 return null;
88 }
89
90 public static function fillAlternativeScope($scope, $scopeList)
91 {
92 if (!in_array($scope, $scopeList, true))
93 {
94 $altScopeList = static::getAlternativeScope($scope);
95 if (is_array($altScopeList))
96 {
97 $hasScope = array_intersect($scopeList, $altScopeList);
98 if (count($hasScope) > 0)
99 {
100 $scopeList[] = $scope;
101 }
102 }
103 }
104
105 return $scopeList;
106 }
107
119 public function processMethodRequest(array $params, $start, \CRestServer $restServer)
120 {
121 $start = intval($start);
122 $this->initialize($restServer, $start);
123
124 $errorCollection = new ErrorCollection();
125 $method = $restServer->getMethod();
126 $methodData = ScopeManager::getInstance()->getMethodInfo($method);
127
128 $request = new \Bitrix\Main\HttpRequest(
129 Context::getCurrent()->getServer(),
130 ['action' => $methodData['method']],
131 [], [], []
132 );
134
136 [$controller, $action] = Resolver::getControllerAndAction(
137 $router->getVendor(),
138 $router->getModule(),
139 $router->getAction(),
140 Controller::SCOPE_REST
141 );
142 if (!$controller)
143 {
144 throw new RestException("Unknown {$method}. There is not controller in module {$router->getModule()}");
145 }
146
147 $this->calculateTotalCount = true;
148 if ((int)$start === self::DONT_CALCULATE_COUNT)
149 {
150 $this->calculateTotalCount = false;
151 }
152
153 $autoWirings = $this->getAutoWirings();
154
155 $this->registerAutoWirings($autoWirings);
156 $result = $controller->run($action, [$params, ['__restServer' => $restServer, '__calculateTotalCount' => $this->calculateTotalCount]]);
157 $this->unRegisterAutoWirings($autoWirings);
158
159 if ($result instanceof Engine\Response\File)
160 {
161 $result->send();
162 return;
163 }
164
165 if ($result instanceof HttpResponse)
166 {
167 if ($result instanceof Errorable)
168 {
169 $errorCollection->add($result->getErrors());
170 }
171
172 $result = $result->getContent();
173 }
174
175 if ($result instanceof RestException)
176 {
177 throw $result;
178 }
179
180 if ($result === null)
181 {
182 $errorCollection->add($controller->getErrors());
183 if (!$errorCollection->isEmpty())
184 {
185 throw $this->createExceptionFromErrors($errorCollection->toArray());
186 }
187 }
188
189 return $this->processData($result);
190 }
191
196 private function initialize(\CRestServer $restServer, int $start): void
197 {
198 $pageNavigation = new PageNavigation('nav');
199 $pageNavigation->setPageSize(static::LIST_LIMIT);
200 if ($start > 0)
201 {
202 $pageNavigation->setCurrentPage((int)($start / static::LIST_LIMIT) + 1);
203 }
204
205 $this->pageNavigation = $pageNavigation;
206 $this->restServer = $restServer;
207 }
208
215 private function getNavigationData(Engine\Response\DataType\Page $page): array
216 {
217 if (!$this->calculateTotalCount)
218 {
219 return [];
220 }
221
222 $result = [];
223 $offset = $this->pageNavigation->getOffset();
224 $total = $page->getTotalCount();
225
226 $currentPageSize = count($page->getItems());
227
228 if ($offset + $currentPageSize < $total)
229 {
230 $result['next'] = $offset + $currentPageSize;
231 }
232
233 $result['total'] = $total;
234
235 return $result;
236 }
237
238
239 private function processData($result)
240 {
241 if ($result instanceof DateTime)
242 {
243 return \CRestUtil::convertDateTime($result);
244 }
245
246 if ($result instanceof Date)
247 {
248 return \CRestUtil::convertDate($result);
249 }
250
251 if ($result instanceof Uri)
252 {
253 return $this->convertAjaxUriToRest($result);
254 }
255
256 if ($result instanceof Engine\Response\DataType\Page)
257 {
258 if (method_exists($result, 'getId'))
259 {
260 $data = [$result->getId() => $this->processData($result->getIterator())];
261 }
262 else
263 {
264 $data = $this->processData($result->getIterator());
265 }
266
267 return array_merge($data, $this->getNavigationData($result));
268 }
269
270 if ($result instanceof Contract\Arrayable)
271 {
272 $result = $result->toArray();
273 }
274 if ($result instanceof \JsonSerializable)
275 {
276 $result = $result->jsonSerialize();
277 }
278
279 if (is_array($result))
280 {
281 foreach ($result as $key => $item)
282 {
283 if ($item instanceof Engine\Response\DataType\ContentUri)
284 {
285 $result[$key . "Machine"] = $this->processData($item);
286 $result[$key] = $this->processData(new Uri($item));
287 }
288 else
289 {
290 $result[$key] = $this->processData($item);
291 }
292
293 }
294 }
295 elseif ($result instanceof \Traversable)
296 {
297 $newResult = [];
298 foreach ($result as $key => $item)
299 {
300 $newResult[$key] = $this->processData($item);
301 }
302
303 $result = $newResult;
304 }
305
306 return $result;
307 }
308
309 private function convertAjaxUriToRest(Uri $uri)
310 {
311 if (!($uri instanceof Engine\Response\DataType\ContentUri))
312 {
313 return $uri->getUri();
314 }
315
316 $endPoint = Engine\UrlManager::getInstance()->getEndPoint(Engine\UrlManager::ABSOLUTE_URL);
317 if ($uri->getPath() !== $endPoint->getPath())
318 {
319 return $uri->getUri();
320 }
321
322 if ($uri->getHost() && $uri->getHost() !== $endPoint->getHost())
323 {
324 return $uri->getUri();
325 }
326
327 parse_str($uri->getQuery(), $params);
328 if (empty($params['action']))
329 {
330 return $uri->getUri();
331 }
332
333 return \CRestUtil::getSpecialUrl($params['action'], $params, $this->restServer);
334 }
335
336 private function getRestEndPoint()
337 {
338 return \Bitrix\Main\Config\Option::get('rest', 'rest_server_path', '/rest');
339 }
340
345 private function createExceptionFromErrors(array $errors)
346 {
347 if(!$errors)
348 {
349 return null;
350 }
351
352 $firstError = reset($errors);
353
354 return new RestException($firstError->getMessage(), $firstError->getCode());
355 }
356
360 private function registerAutoWirings(array $autoWirings): void
361 {
362 foreach ($autoWirings as $parameter)
363 {
364 AutoWire\Binder::registerGlobalAutoWiredParameter($parameter);
365 }
366 }
367
371 private function unRegisterAutoWirings(array $autoWirings): void
372 {
373 foreach ($autoWirings as $parameter)
374 {
375 AutoWire\Binder::unRegisterGlobalAutoWiredParameter($parameter);
376 }
377 }
378
382 private function getAutoWirings(): array
383 {
384 $buildRules = [
385 'restServer' => [
386 'class' => \CRestServer::class,
387 'constructor' => function() {
388 return $this->restServer;
389 },
390 ],
391 'pageNavigation' => [
392 'class' => PageNavigation::class,
393 'constructor' => function() {
394 return $this->pageNavigation;
395 },
396 ],
397 ];
398
399 $autoWirings = [];
400 foreach ($buildRules as $rule)
401 {
402 $autoWirings[] = new AutoWire\Parameter($rule['class'], $rule['constructor']);
403 }
404
405 return $autoWirings;
406 }
407}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
static getInstance()
Определения application.php:98
const ABSOLUTE_URL
Определения urlmanager.php:14
const SCOPE_USER_BASIC
Определения user.php:18
const SCOPE_USER_BRIEF
Определения user.php:19
const DONT_CALCULATE_COUNT
Определения restmanager.php:25
static getModuleScopeAlias($moduleId)
Определения restmanager.php:67
static fillAlternativeScope($scope, $scopeList)
Определения restmanager.php:90
static getInstance()
Определения scopemanager.php:38
Определения rest.php:896
$data['IS_AVAILABLE']
Определения .description.php:13
</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
$start
Определения get_search.php:9
$moduleId
$errors
Определения iblock_catalog_edit.php:74
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
Определения action.php:3
Определения handlers.php:8
Определения event.php:2
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$router
Определения routing_index.php:31
$page
Определения order_form.php:33
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$method
Определения index.php:27
$action
Определения file_dialog.php:21