Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
scopemanager.php
1<?php
2
3namespace Bitrix\Rest\Engine;
4
10use CRestProvider;
11use CRestUtil;
12
18{
19 public const CACHE_TIME = 604800;// 86400 * 7
20 public const CACHE_DIR = '/rest/scope/';
21 private const CACHE_KEY = 'list';
22 private const METHOD_DELIMITER = '.';
23 private const VENDOR_DELIMITER = ':';
24
26 private static $instance;
27 private $scopeList;
28 private $methodInfoList = [];
29
30 private function __construct()
31 {
32 $this->load();
33 }
34
38 public static function getInstance() : ScopeManager
39 {
40 if (self::$instance === null)
41 {
42 self::$instance = new ScopeManager();
43 }
44
45 return self::$instance;
46 }
47
48 private function load() : bool
49 {
50 $this->scopeList = [];
51 $cache = Cache::createInstance();
52 if ($cache->initCache(self::CACHE_TIME, self::CACHE_KEY, self::CACHE_DIR))
53 {
54 $this->scopeList = $cache->getVars();
55 }
56 elseif ($cache->startDataCache())
57 {
58 $provider = new CRestProvider();
59 $scopeList = $provider->getDescription();
60 foreach ($scopeList as $code => $value)
61 {
62 $this->scopeList[$code] = $code;
63 }
64
65 $installedModuleList = ModuleManager::getInstalledModules();
66 foreach ($installedModuleList as $moduleId => $moduleDescription)
67 {
68 if (!isset($description[$moduleId]))
69 {
70 $controllersConfig = Configuration::getInstance($moduleId);
71
72 if (!empty($controllersConfig['controllers']['restIntegration']['enabled']))
73 {
74 if (
75 !isset($controllersConfig['controllers']['restIntegration']['hideModuleScope'])
76 || !$controllersConfig['controllers']['restIntegration']['hideModuleScope']
77 )
78 {
79 $this->scopeList[$moduleId] = $moduleId;
80 }
81
82 if (
83 isset($controllersConfig['controllers']['restIntegration']['scopes'])
84 && is_array($controllersConfig['controllers']['restIntegration']['scopes'])
85 )
86 {
87 $this->scopeList = array_merge(
88 $this->scopeList,
89 array_fill_keys(
90 $controllersConfig['controllers']['restIntegration']['scopes'],
91 $moduleId
92 )
93 );
94 }
95 }
96 }
97 }
98
99 unset($this->scopeList[CRestUtil::GLOBAL_SCOPE]);
100
101 $cache->endDataCache($this->scopeList);
102 }
103
104 return true;
105 }
106
107 public function reset() : bool
108 {
109 $this->methodInfoList = [];
110 $this->load();
111
112 return true;
113 }
114
115 public static function cleanCache() : bool
116 {
117 return Cache::clearCache(true, self::CACHE_DIR);
118 }
119
120 public function getAlias($code) : ?string
121 {
122 return $this->scopeList[$code];
123 }
124
125 public function listScope() : array
126 {
127 return array_keys($this->scopeList);
128 }
129
130 public function getAliasList() : array
131 {
132 return array_unique(array_values($this->scopeList));
133 }
134
135 public function getList() : array
136 {
137 $langScope = Application::getDocumentRoot() . BX_ROOT . '/modules/rest/scope.php';
138 Loc::loadMessages($langScope);
139 $result = [];
140 foreach ($this->listScope() as $code)
141 {
142 $key = mb_strtoupper($code);
143 if (mb_strtoupper($key) === 'LOG')
144 {
145 $name = Loc::getMessage('REST_SCOPE_LOG_MSGVER_1');
146 $description = Loc::getMessage('REST_SCOPE_LOG_DESCRIPTION_MSGVER_1');
147 }
148 else
149 {
150 $name = Loc::getMessage('REST_SCOPE_' . $key);
151 $description = Loc::getMessage('REST_SCOPE_' . $key . '_DESCRIPTION');
152 }
153
154 $result[$code] = [
155 'code' => $code,
156 'title' => ($name) ? $name . ' (' . $code . ')' : $code,
157 'description' => $description
158 ];
159 }
160
161 return $result;
162 }
163
164 public function getMethodInfo(?string $method) : array
165 {
166 if (!$this->methodInfoList[$method])
167 {
168 $scope = '';
169 $module = '';
170 $scopeFind = '';
171 $actionParts = explode(self::METHOD_DELIMITER, $method);
172
173 foreach ($actionParts as $partScope)
174 {
175 $scopeFind .= ($scopeFind !== '' ? self::METHOD_DELIMITER : '') . $partScope;
176 $moduleFind = $this->getAlias($scopeFind);
177 if ($moduleFind)
178 {
179 $module = $moduleFind;
180 $scope = $scopeFind;
181 }
182 }
183
184 if (!$scope || !$module)
185 {
186 $scope = array_shift($actionParts);
187 $module = $scope;
188 }
189 elseif ($module !== $scope)
190 {
191 $method = $module . self::METHOD_DELIMITER . $method;
192 }
193
197 if (
198 mb_strpos($method, self::VENDOR_DELIMITER) === false
199 && mb_strpos($module, self::METHOD_DELIMITER) !== false
200 )
201 {
202 $moduleParts = explode(self::METHOD_DELIMITER, $module);
203 array_pop($moduleParts);
204 $vendor = implode(self::METHOD_DELIMITER, $moduleParts);
205
206 $method = preg_replace('/^' . $vendor . self::METHOD_DELIMITER . '/', $vendor . self::VENDOR_DELIMITER, $method);
207 }
208
209 $this->methodInfoList[$method] = [
210 'moduleId' => $module,
211 'scope' => $scope,
212 'method' => $method,
213 ];
214 }
215
216 return $this->methodInfoList[$method];
217 }
218
219 public static function onChangeRegisterModule() : void
220 {
221 static::cleanCache();
222 }
223}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29