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\OptionsAwareTrait; |
28
|
|
|
use Derafu\Lib\Core\Foundation\Contract\StrategyInterface; |
29
|
|
|
use Derafu\Lib\Core\Foundation\Contract\WorkerInterface; |
30
|
|
|
use Derafu\Lib\Core\Foundation\Exception\StrategyException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Clase base para los workers de la aplicación. |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractWorker extends AbstractService implements WorkerInterface |
36
|
|
|
{ |
37
|
|
|
use OptionsAwareTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Reglas de esquema del worker. |
41
|
|
|
* |
42
|
|
|
* El esquema se debe definir en cada worker. El formato del esquema es el |
43
|
|
|
* utilizado por Symfony\Component\OptionsResolver\OptionsResolver. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected array $optionsSchema = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Estrategias que el worker implementa. |
51
|
|
|
* |
52
|
|
|
* @var StrategyInterface[] |
53
|
|
|
*/ |
54
|
|
|
protected array $strategies; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor del worker. |
58
|
|
|
* |
59
|
|
|
* @param array $strategies Estrategias que este worker implementa. |
60
|
|
|
*/ |
61
|
87 |
|
public function __construct(iterable $strategies = []) |
62
|
|
|
{ |
63
|
87 |
|
$this->strategies = is_array($strategies) |
64
|
87 |
|
? $strategies |
65
|
|
|
: iterator_to_array($strategies) |
66
|
87 |
|
; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function getName(): string |
73
|
|
|
{ |
74
|
1 |
|
$regex = "/\\\\Package\\\\([A-Za-z0-9_]+)\\\\Component\\\\([A-Za-z0-9_]+)\\\\Worker\\\\([A-Za-z0-9_]+)Worker/"; |
75
|
|
|
|
76
|
1 |
|
$class = (string) $this; |
77
|
1 |
|
if (preg_match($regex, $class, $matches)) { |
78
|
1 |
|
return $matches[1] . ' ' . $matches[2] . ' ' . $matches[3]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return parent::getName(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getStrategy(string $strategy): StrategyInterface |
88
|
|
|
{ |
89
|
|
|
if (!str_contains($strategy, '.')) { |
90
|
|
|
$strategy = 'default.' . $strategy; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!isset($this->strategies[$strategy])) { |
94
|
|
|
throw new StrategyException(sprintf( |
95
|
|
|
'No se encontró la estrategia %s en el worker %s (%s).', |
96
|
|
|
$strategy, |
97
|
|
|
$this->getName(), |
98
|
|
|
$this->getId(), |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->strategies[$strategy]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getStrategies(): array |
109
|
|
|
{ |
110
|
|
|
return $this->strategies; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|