Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
stepper.php
1<?php
3
6
13{
14 protected ?string $processToken;
15
16 protected bool $isNewProcess = true;
17
18 protected bool $isProcessCompleted = false;
19
20 protected int $processedItems = 0;
21
22 protected int $totalItems = 0;
23
25
26
33 protected function onBeforeRun()
34 {
35 if ($this instanceof IProcessParameters)
36 {
37 $this->keepField([
38 'processedItems',
39 'totalItems',
40 ]);
41
43 $this->processToken = $this->getController()->getRequest()->get('PROCESS_TOKEN');
44
45 $progressData = $this->getProgressParameters();
46 if (\count($progressData) > 0)
47 {
48 $this->isNewProcess = (empty($progressData['processToken']) || $progressData['processToken'] !== $this->processToken);
49 if (!$this->isNewProcess)
50 {
51 // restore state
53 }
54 }
55
56 $this->keepField('processToken');
57 }
58
59 if (empty($this->processToken))
60 {
61 $this->addError(new Main\Error('Process token is not specified.'));
62 }
63
64 return \count($this->getErrors()) === 0;
65 }
66
75 protected function performStep($action, array $params = [])
76 {
77 if ($this->isNewProcess)
78 {
79 $this->processedItems = 0;
80 $this->totalItems = 0;
81
82 if ($this instanceof IProcessParameters)
83 {
85 }
86 }
87
88 $this->startTimer();
89
90 if ($action instanceof \Closure)
91 {
92 $result = $action->call($this, $params);
93 }
94 elseif (\is_callable($action))
95 {
96 $result = \call_user_func($action, $params);
97 }
98 elseif (\is_string($action) && \is_callable([$this, $action]))
99 {
100 $result = \call_user_func([$this, $action], $params);
101 }
102 else
103 {
104 $this->addError(new Main\Error('Wrong action parameter!'));
105 }
106
107 if ($this->hasErrors())
108 {
109 $result['STATUS'] = Translate\Controller\STATUS_COMPLETED;
110 }
111 elseif ($this->hasProcessCompleted())
112 {
113 $result['STATUS'] = Translate\Controller\STATUS_COMPLETED;
114 }
115 else
116 {
117 $result['STATUS'] = Translate\Controller\STATUS_PROGRESS;
118 }
119
120 if ($this instanceof IProcessParameters)
121 {
122 // Save progress
123 $this->saveProgressParameters();
124 }
125
126 return $result;
127 }
128
136 public function declareAccomplishment(bool $flag = true): void
137 {
138 $this->isProcessCompleted = $flag;
139 }
140
146 public function hasProcessCompleted(): bool
147 {
148 return $this->isProcessCompleted;
149 }
150
151
156 public function hasErrors(): bool
157 {
159 if ($this->errorCollection instanceof Main\ErrorCollection)
160 {
161 return $this->errorCollection->isEmpty() !== true;
162 }
163
164 return false;
165 }
166
172 public function instanceTimer(): Translate\Controller\Timer
173 {
174 if ($this->timer === null)
175 {
176 $this->timer = new Translate\Controller\Timer();
177 }
178
179 return $this->timer;
180 }
181
189 public function startTimer(): void
190 {
191 $this->instanceTimer()->startTimer((int)\START_EXEC_TIME);
192 }
193
201 public function hasTimeLimitReached(): bool
202 {
203 return $this->instanceTimer()->hasTimeLimitReached();
204 }
205}
Translate Controller Timer $timer
Definition stepper.php:24
performStep($action, array $params=[])
Definition stepper.php:75
declareAccomplishment(bool $flag=true)
Definition stepper.php:136
addError(Main\Error $error)
Definition error.php:22