1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Derafu: Biblioteca PHP (Núcleo). |
7
|
|
|
* Copyright (C) Derafu <https://www.derafu.org> |
8
|
|
|
* |
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
13
|
|
|
* |
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
18
|
|
|
* |
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de GNU |
20
|
|
|
* junto a este programa. |
21
|
|
|
* |
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Derafu\Lib\Core\Foundation\Abstract; |
26
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Common\Trait\ConfigurableTrait; |
28
|
|
|
use Derafu\Lib\Core\Foundation\Contract\HandlerInterface; |
29
|
|
|
use Derafu\Lib\Core\Foundation\Contract\JobInterface; |
30
|
|
|
use Derafu\Lib\Core\Foundation\Contract\StrategyInterface; |
31
|
|
|
use Derafu\Lib\Core\Foundation\Contract\WorkerInterface; |
32
|
|
|
use Derafu\Lib\Core\Foundation\Exception\HandlerException; |
33
|
|
|
use Derafu\Lib\Core\Foundation\Exception\JobException; |
34
|
|
|
use Derafu\Lib\Core\Foundation\Exception\StrategyException; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Clase base para los workers de la aplicación. |
38
|
|
|
*/ |
39
|
|
|
abstract class AbstractWorker extends AbstractService implements WorkerInterface |
40
|
|
|
{ |
41
|
|
|
use ConfigurableTrait; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Trabajos que el worker implementa. |
45
|
|
|
* |
46
|
|
|
* @var JobInterface[] |
47
|
|
|
*/ |
48
|
|
|
protected array $jobs; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handlers que el worker implementa. |
52
|
|
|
* |
53
|
|
|
* @var HandlerInterface[] |
54
|
|
|
*/ |
55
|
|
|
protected array $handlers; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Estrategias que el worker implementa. |
59
|
|
|
* |
60
|
|
|
* @var StrategyInterface[] |
61
|
|
|
*/ |
62
|
|
|
protected array $strategies; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Constructor del worker. |
66
|
|
|
* |
67
|
|
|
* @param array $jobs Jobs que este worker implementa. |
68
|
|
|
* @param array $handlers Handlers que este worker implementa. |
69
|
|
|
* @param array $strategies Estrategias que este worker implementa. |
70
|
|
|
*/ |
71
|
89 |
|
public function __construct( |
72
|
|
|
iterable $jobs = [], |
73
|
|
|
iterable $handlers = [], |
74
|
|
|
iterable $strategies = [] |
75
|
|
|
) { |
76
|
89 |
|
$this->jobs = is_array($jobs) |
77
|
89 |
|
? $jobs |
78
|
|
|
: iterator_to_array($jobs) |
79
|
89 |
|
; |
80
|
|
|
|
81
|
89 |
|
$this->handlers = is_array($handlers) |
82
|
89 |
|
? $handlers |
83
|
|
|
: iterator_to_array($handlers) |
84
|
89 |
|
; |
85
|
|
|
|
86
|
89 |
|
$this->strategies = is_array($strategies) |
87
|
87 |
|
? $strategies |
88
|
2 |
|
: iterator_to_array($strategies) |
89
|
89 |
|
; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
1 |
|
public function getName(): string |
96
|
|
|
{ |
97
|
1 |
|
$regex = "/\\\\Package\\\\([A-Za-z0-9_]+)\\\\Component\\\\([A-Za-z0-9_]+)\\\\Worker\\\\([A-Za-z0-9_]+)Worker/"; |
98
|
|
|
|
99
|
1 |
|
$class = (string) $this; |
100
|
1 |
|
if (preg_match($regex, $class, $matches)) { |
101
|
1 |
|
return $matches[1] . ' ' . $matches[2] . ' ' . $matches[3]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return parent::getName(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
*/ |
110
|
|
|
public function getJob(string $job): JobInterface |
111
|
|
|
{ |
112
|
|
|
if (!isset($this->jobs[$job])) { |
113
|
|
|
throw new JobException(sprintf( |
114
|
|
|
'No se encontró el trabajo %s en el worker %s (%s).', |
115
|
|
|
$job, |
116
|
|
|
$this->getName(), |
117
|
|
|
$this->getId(), |
118
|
|
|
)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->jobs[$job]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function getJobs(): array |
128
|
|
|
{ |
129
|
|
|
return $this->jobs; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function getHandler(string $handler): HandlerInterface |
136
|
|
|
{ |
137
|
|
|
if (!isset($this->handlers[$handler])) { |
138
|
|
|
throw new HandlerException(sprintf( |
139
|
|
|
'No se encontró el manejador (handler) %s en el worker %s (%s).', |
140
|
|
|
$handler, |
141
|
|
|
$this->getName(), |
142
|
|
|
$this->getId(), |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->handlers[$handler]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function getHandlers(): array |
153
|
|
|
{ |
154
|
|
|
return $this->handlers; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
2 |
|
public function getStrategy(string $strategy): StrategyInterface |
161
|
|
|
{ |
162
|
2 |
|
$strategies = [$strategy]; |
163
|
2 |
|
if (!str_contains($strategy, '.')) { |
164
|
2 |
|
$strategies[] = 'default.' . $strategy; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
foreach ($strategies as $name) { |
168
|
2 |
|
if (isset($this->strategies[$name])) { |
169
|
2 |
|
return $this->strategies[$name]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
throw new StrategyException(sprintf( |
174
|
|
|
'No se encontró la estrategia %s en el worker %s (%s).', |
175
|
|
|
$strategy, |
176
|
|
|
$this->getName(), |
177
|
|
|
$this->getId(), |
178
|
|
|
)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritDoc} |
183
|
|
|
*/ |
184
|
|
|
public function getStrategies(): array |
185
|
|
|
{ |
186
|
|
|
return $this->strategies; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|