Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
4
14use CRestUtil;
15use CTimeZone;
16use CFile;
17use CAgent;
18
19abstract class Base
20{
21 public const CONTEXT_PREFIX = 'configuration_action';
22 public const ACTION = 'base';
23 public const ERROR_PROCESS_NOT_FOUND = 'PROCESS_NOT_FOUND';
24 public const ERROR_PROCESS_DID_NOT_CREATE = 'PROCESS_DID_NOT_CREATE';
25
26 private const SETTING_ACTION_STATUS_CODE = 'ACTION_STATUS_CODE';
27 public const STATUS_START = 'S';
28 public const STATUS_PROCESS = 'P';
29 public const STATUS_FINISH = 'F';
30 public const STATUS_ERROR = 'E';
31 public const STATUS_UNKNOWN = 'U';
32 public const STATUSES = [
38 ];
39
40
41 public const PROPERTY_STRUCTURE = 'STRUCTURE';
42 public const PROPERTY_FILES = 'FILES';
43 public const PROPERTY_MANIFEST = 'MANIFEST';
44
45 protected const MODULE_ID = 'rest';
46 protected $processId = 0;
47 protected $setting = null;
48 protected $notification = null;
49 private $context = '';
50 private $contextEntity = '';
51
52 abstract protected function run(): bool;
53 abstract protected function checkRegister($data): array;
54
60 public function __construct($processId = 0)
61 {
62 $this->setProcessId((int) $processId);
63 $this->init();
64 }
65
66 protected function init()
67 {
68 $app = $this->getSetting()->get(Setting::SETTING_APP_INFO);
69 $this->setContextEntity($app['ID'] ?? 0);
70 }
71
72 protected function reset()
73 {
74 $this->setting = null;
75 $this->notification = null;
76 }
77
84 public function setProcessId(int $processId): bool
85 {
86 if ($processId !== $this->processId)
87 {
88 $this->processId = $processId;
89 $this->reset();
90 $this->init();
91
92 return true;
93 }
94
95 return false;
96 }
97
103 public function getContext(): string
104 {
105 return !empty($this->context) ? $this->context : static::CONTEXT_PREFIX . static::ACTION . $this->processId;
106 }
107
114 public function setContext($context = ''): bool
115 {
116 if ($context !== $this->context)
117 {
118 $this->context = (string) $context;
119 $this->reset();
120 $this->init();
121
122 return true;
123 }
124
125 return false;
126 }
127
132 public function getContextEntity(): string
133 {
134 return !empty($this->contextEntity) ? $this->contextEntity : static::CONTEXT_PREFIX . static::ACTION . $this->processId;
135 }
136
143 public function setContextEntity($appId = 0): bool
144 {
145 $id = $appId > 0 ? (int) $appId : 0;
146 $this->contextEntity = Helper::getInstance()->getContextAction($id);
147
148 return true;
149 }
150
156 public function getSetting(): Setting
157 {
158 if (!$this->setting)
159 {
160 $this->setting = new Setting($this->getContext());
161 }
162
163 return $this->setting;
164 }
165
169 protected function getStructureData()
170 {
171 $result = [];
172
173 $data = $this->getSetting()->get(Structure::CODE_CUSTOM_FILE . static::ACTION);
174 if ($data['ID'] > 0)
175 {
176 $path = CFile::GetPath($data['ID']);
177 if ($path)
178 {
179 if (mb_strpos($path, 'https://') === false)
180 {
181 $path = $_SERVER['DOCUMENT_ROOT'] . $path;
182 $fileContent = File::getFileContents($path);
183 }
184 else
185 {
186 $httpClient = new HttpClient();
187 $httpClient->get($path);
188 $fileContent = $httpClient->getResult();
189 }
190
191 try
192 {
193 $result = Json::decode($fileContent);
194 }
195 catch (ArgumentException $e)
196 {
197 }
198 }
199 }
200
201 return $result;
202 }
203
216 public function register(array $data, array $additionalOptions = [], int $userId = 0, string $appCode = '', bool $byAgent = true): array
217 {
218 $result = static::checkRegister($data);
219 if (!empty($result['error']))
220 {
221 return $result;
222 }
223
224 $data = $this->prepareData($data);
225 $file = CRestUtil::saveFile(base64_encode(Json::encode($data)));
226 $file['MODULE_ID'] = static::MODULE_ID;
227 $processId = CFile::SaveFile(
228 $file,
229 'configuration/' . static::ACTION
230 );
231
232 if ($processId > 0)
233 {
234 $this->setProcessId($processId);
235 $isSave = $this->getSetting()->set(
236 Structure::CODE_CUSTOM_FILE . static::ACTION,
237 [
238 'ID' => $processId,
239 ]
240 );
241 if ($isSave)
242 {
243 $this->getSetting()->set(
245 $additionalOptions
246 );
247
248 $this->getSetting()->set(
250 $userId
251 );
252
253 if (!empty($appCode))
254 {
255 $app = AppTable::getByClientId($appCode);
256 if (is_array($app))
257 {
258 $this->getSetting()->set(
260 $app
261 );
262 }
263 }
264
265 $this->setStatus(self::STATUS_START);
266 $result['processId'] = $processId;
267 if ($byAgent)
268 {
269 CAgent::AddAgent(
270 static::class . '::runAgent(' . $processId . ');',
271 static::MODULE_ID,
272 'N',
273 60,
274 '',
275 'Y',
276 ConvertTimeStamp(time() + CTimeZone::GetOffset() + 60, 'FULL')
277 );
278 }
279 }
280 else
281 {
282 $this->setStatus(self::STATUS_ERROR);
283 }
284 }
285
286 if (!$result['processId'])
287 {
288 $result['error'] = static::ERROR_PROCESS_DID_NOT_CREATE;
289 }
290
291 return $result;
292 }
293
302 protected function prepareData($data)
303 {
304 if (isset($data[self::PROPERTY_STRUCTURE]) && is_array($data[self::PROPERTY_STRUCTURE]))
305 {
306 foreach ($data[self::PROPERTY_STRUCTURE] as $type => $item)
307 {
308 if ($type === Helper::STRUCTURE_FILES_NAME)
309 {
310 continue;
311 }
312 if (is_array($data[self::PROPERTY_STRUCTURE][$type]))
313 {
314 $list = [];
315 foreach ($data[self::PROPERTY_STRUCTURE][$type] as $value)
316 {
317 if (is_string($value))
318 {
319 $path = array_filter(explode('/', $value));
320 $list[end($path)] = $value;
321 }
322 }
323 ksort($list);
324 $data[self::PROPERTY_STRUCTURE][$type] = array_values($list);
325 }
326 }
327 }
328
329 return $data;
330 }
331
336 public function unregister(): array
337 {
338 $result = [
339 'error' => static::ERROR_PROCESS_NOT_FOUND,
340 'error_description' => 'Process doesn\'t found.',
341 ];
342 if ($this->processId > 0)
343 {
344 $data = $this->getSetting()->get(
345 Structure::CODE_CUSTOM_FILE . static::ACTION
346 );
347
348 if ($data)
349 {
350 $result = [
351 'success' => 'Y',
352 ];
353 $this->getSetting()->delete(
354 Structure::CODE_CUSTOM_FILE . static::ACTION
355 );
356 $res = CAgent::getList(
357 [],
358 [
359 'MODULE_ID' => static::MODULE_ID,
360 'NAME' => static::class . '::runAgent(' . $data['ID'] . ');',
361 ]
362 );
363 while ($agent = $res->fetch())
364 {
365 CAgent::Delete($agent['ID']);
366 }
367 }
368 }
369
370 return $result;
371 }
372
379 {
380 if (!$this->notification)
381 {
382 $this->notification = new Notification($this->getSetting());
383 }
384
385 return $this->notification;
386 }
387
393 protected function setStatus(string $status)
394 {
395 if (!in_array($status, self::STATUSES))
396 {
397 return false;
398 }
399
400 return $this->getSetting()->set(self::SETTING_ACTION_STATUS_CODE, $status);
401 }
402
408 public function getStatus()
409 {
410 return $this->getSetting()->get(self::SETTING_ACTION_STATUS_CODE) ?? self::STATUS_UNKNOWN;
411 }
412
417 public function get(): array
418 {
419 $result = [
420 'status' => $this->getStatus(),
421 ];
422 $notification = [
423 'notice' => $this->getNotificationInstance()->list(
424 [
426 ]
427 ),
428 'errors' => $this->getNotificationInstance()->list(
429 [
430 'type' => Notification::TYPE_ERROR,
431 ]
432 ),
433 'exception' => $this->getNotificationInstance()->list(
434 [
436 ]
437 ),
438 ];
439
440 return array_merge($result, array_filter($notification));
441 }
442
448 public static function runAgent(int $processId): string
449 {
450 $action = new static($processId);
451 $result = $action->run();
452
453 if (!$result)
454 {
455 $action->unregister();
456 }
457
458 return $result ? static::class . '::runAgent(' . $processId . ');' : '';
459 }
460
461}
static getByClientId($clientId)
Definition app.php:929
static runAgent(int $processId)
Definition base.php:448