30 private $pageNavigation;
32 private $calculateTotalCount =
true;
34 public static function onFindMethodDescription($potentialAction)
36 $restManager =
new static();
39 $request = new \Bitrix\Main\HttpRequest(
40 Context::getCurrent()->getServer(),
41 [
'action' => $potentialActionData[
'method']],
47 if (empty($controllersConfig[
'controllers'][
'restIntegration'][
'enabled']))
53 list($controller) =
$router->getControllerAndAction();
60 'scope' => static::getModuleScopeAlias($potentialActionData[
'scope']),
62 $restManager,
'processMethodRequest'
77 private static function getAlternativeScope($scope): ?
array
92 if (!in_array($scope, $scopeList,
true))
94 $altScopeList = static::getAlternativeScope($scope);
95 if (is_array($altScopeList))
97 $hasScope = array_intersect($scopeList, $altScopeList);
98 if (
count($hasScope) > 0)
100 $scopeList[] = $scope;
128 $request = new \Bitrix\Main\HttpRequest(
129 Context::getCurrent()->getServer(),
130 [
'action' => $methodData[
'method']],
136 [$controller,
$action] = Resolver::getControllerAndAction(
140 Controller::SCOPE_REST
144 throw new RestException(
"Unknown {$method}. There is not controller in module {$router->getModule()}");
147 $this->calculateTotalCount =
true;
148 if ((
int)
$start === self::DONT_CALCULATE_COUNT)
150 $this->calculateTotalCount =
false;
153 $autoWirings = $this->getAutoWirings();
155 $this->registerAutoWirings($autoWirings);
157 $this->unRegisterAutoWirings($autoWirings);
159 if (
$result instanceof Engine\Response\File)
165 if (
$result instanceof HttpResponse)
167 if (
$result instanceof Errorable)
169 $errorCollection->add(
$result->getErrors());
175 if (
$result instanceof RestException)
182 $errorCollection->add($controller->getErrors());
183 if (!$errorCollection->isEmpty())
185 throw $this->createExceptionFromErrors($errorCollection->toArray());
189 return $this->processData(
$result);
198 $pageNavigation =
new PageNavigation(
'nav');
199 $pageNavigation->setPageSize(static::LIST_LIMIT);
202 $pageNavigation->setCurrentPage((
int)(
$start / static::LIST_LIMIT) + 1);
205 $this->pageNavigation = $pageNavigation;
215 private function getNavigationData(Engine\Response\DataType\Page
$page):
array
217 if (!$this->calculateTotalCount)
223 $offset = $this->pageNavigation->getOffset();
224 $total =
$page->getTotalCount();
228 if ($offset + $currentPageSize < $total)
230 $result[
'next'] = $offset + $currentPageSize;
239 private function processData(
$result)
241 if (
$result instanceof DateTime)
243 return \CRestUtil::convertDateTime(
$result);
248 return \CRestUtil::convertDate(
$result);
253 return $this->convertAjaxUriToRest(
$result);
256 if (
$result instanceof Engine\Response\DataType\Page)
258 if (method_exists(
$result,
'getId'))
267 return array_merge(
$data, $this->getNavigationData(
$result));
270 if (
$result instanceof Contract\Arrayable)
274 if (
$result instanceof \JsonSerializable)
283 if ($item instanceof Engine\Response\DataType\ContentUri)
285 $result[
$key .
"Machine"] = $this->processData($item);
286 $result[
$key] = $this->processData(
new Uri($item));
300 $newResult[
$key] = $this->processData($item);
309 private function convertAjaxUriToRest(Uri
$uri)
311 if (!(
$uri instanceof Engine\Response\DataType\ContentUri))
313 return $uri->getUri();
317 if (
$uri->getPath() !== $endPoint->getPath())
319 return $uri->getUri();
322 if (
$uri->getHost() &&
$uri->getHost() !== $endPoint->getHost())
324 return $uri->getUri();
330 return $uri->getUri();
333 return \CRestUtil::getSpecialUrl(
$params[
'action'],
$params, $this->restServer);
336 private function getRestEndPoint()
338 return \Bitrix\Main\Config\Option::get(
'rest',
'rest_server_path',
'/rest');
345 private function createExceptionFromErrors(
array $errors)
354 return new RestException($firstError->getMessage(), $firstError->getCode());
360 private function registerAutoWirings(
array $autoWirings): void
362 foreach ($autoWirings as $parameter)
364 AutoWire\Binder::registerGlobalAutoWiredParameter($parameter);
371 private function unRegisterAutoWirings(
array $autoWirings): void
373 foreach ($autoWirings as $parameter)
375 AutoWire\Binder::unRegisterGlobalAutoWiredParameter($parameter);
382 private function getAutoWirings():
array
386 'class' => \CRestServer::class,
387 'constructor' =>
function() {
391 'pageNavigation' => [
392 'class' => PageNavigation::class,
393 'constructor' =>
function() {
394 return $this->pageNavigation;
400 foreach ($buildRules as $rule)
402 $autoWirings[] =
new AutoWire\Parameter($rule[
'class'], $rule[
'constructor']);
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)