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\Package\Prime\Component\Entity\Worker; |
26
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Foundation\Abstract\AbstractWorker; |
28
|
|
|
use Derafu\Lib\Core\Foundation\Contract\FactoryInterface; |
29
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Entity\Contract\ManagerWorkerInterface; |
30
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Entity\Contract\RepositoryInterface; |
31
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Entity\Entity\Entity; |
32
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Entity\Exception\ManagerException; |
33
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Entity\Repository\Repository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Worker "prime.entity.manager". |
37
|
|
|
*/ |
38
|
|
|
class ManagerWorker extends AbstractWorker implements ManagerWorkerInterface |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Esquema de configuración del worker. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected array $configurationSchema = [ |
46
|
|
|
'entity' => [ |
47
|
|
|
'types' => 'array', |
48
|
|
|
'default' => [], |
49
|
|
|
'schema' => [ |
50
|
|
|
'normalizationName' => [ |
51
|
|
|
'types' => 'string', |
52
|
|
|
'default' => 'name', |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Listado de fuentes de datos de repositorios de entidades. |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private array $sources; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Listado de repositorios que ya han sido cargados desde sus fuentes de |
67
|
|
|
* datos. |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private array $repositories = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor del worker. |
75
|
|
|
* |
76
|
|
|
* @param array $sources |
77
|
|
|
*/ |
78
|
|
|
public function __construct(array $sources = []) |
79
|
|
|
{ |
80
|
|
|
$this->sources = $sources; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function getRepository( |
87
|
|
|
string $source, |
88
|
|
|
?FactoryInterface $factory = null |
89
|
|
|
): RepositoryInterface { |
90
|
|
|
// Si el repositorio no está cargado se trata de cargar. |
91
|
|
|
if (!isset($this->repositories[$source])) { |
92
|
|
|
// Si no hay fuente de datos para el repositorio se genera un error. |
93
|
|
|
if (!isset($this->sources[$source])) { |
94
|
|
|
throw new ManagerException(sprintf( |
95
|
|
|
'No existe una fuente de datos configurada para crear un repositorio de %s.', |
96
|
|
|
$source |
97
|
|
|
)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Se tratará de crear el repositorio a partir de la fuente de datos |
101
|
|
|
// asignada. |
102
|
|
|
$this->repositories[$source] = new Repository( |
103
|
|
|
$this->resolveEntityClass($source), |
104
|
|
|
$this->sources[$source], |
105
|
|
|
$this->getConfiguration()->get('entity.normalizationName'), |
106
|
|
|
$factory |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Retornar el repositorio solicitado. |
111
|
|
|
return $this->repositories[$source]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Determina la entidad que se debe utilizar a partir del ID del origen del |
116
|
|
|
* repositorio. |
117
|
|
|
* |
118
|
|
|
* @param string $source |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private function resolveEntityClass(string $source): string |
122
|
|
|
{ |
123
|
|
|
if (class_exists($source)) { |
124
|
|
|
return $source; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return Entity::class; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|