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 |
11
|
|
|
* por la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, |
12
|
|
|
* o (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 |
20
|
|
|
* GNU junto a este programa. |
21
|
|
|
* |
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Derafu\Lib\Core\Common\Abstract; |
26
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Common\Contract\ApplicationInterface; |
28
|
|
|
use Derafu\Lib\Core\Common\Contract\ServiceRegistryInterface; |
29
|
|
|
use Derafu\Lib\Core\ServiceRegistry; |
30
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Clase base para la clase principal de la aplicación. |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractApplication implements ApplicationInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Clase para el registro de los servicios de la aplicación. |
39
|
|
|
*/ |
40
|
|
|
protected string $serviceRegistry = ServiceRegistry::class; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Prefijo con el que deben ser nombrados todos los servicios asociados a la |
44
|
|
|
* aplicación. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected string $servicesPrefix = 'derafu.lib.'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Instancia de la clase para el patrón singleton. |
52
|
|
|
* |
53
|
|
|
* @var self |
54
|
|
|
*/ |
55
|
|
|
private static self $instance; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Contenedor de servicios de la aplicación. |
59
|
|
|
* |
60
|
|
|
* @var ContainerBuilder |
61
|
|
|
*/ |
62
|
|
|
private ContainerBuilder $container; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Constructor de la aplicación. |
66
|
|
|
* |
67
|
|
|
* Debe ser privado para respetar el patrón singleton. |
68
|
|
|
*/ |
69
|
7 |
|
private function __construct(?string $serviceRegistry) |
70
|
|
|
{ |
71
|
7 |
|
$this->container = new ContainerBuilder(); |
72
|
7 |
|
$this->getServiceRegistry($serviceRegistry)->register($this->container); |
73
|
7 |
|
$this->container->compile(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
7 |
|
public static function getInstance(?string $serviceRegistry = null): self |
80
|
|
|
{ |
81
|
7 |
|
if (!isset(self::$instance)) { |
82
|
7 |
|
$class = static::class; |
83
|
7 |
|
self::$instance = new $class($serviceRegistry); |
84
|
|
|
} |
85
|
|
|
|
86
|
7 |
|
return self::$instance; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
6 |
|
public function getService(string $service): object |
93
|
|
|
{ |
94
|
6 |
|
$service = $this->normalizeServiceName($service); |
95
|
|
|
|
96
|
6 |
|
return $this->container->get($service); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function hasService(string $service): bool |
103
|
|
|
{ |
104
|
|
|
$service = $this->normalizeServiceName($service); |
105
|
|
|
|
106
|
|
|
return $this->container->has($service); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Obtiene el registry de servicios. |
111
|
|
|
*/ |
112
|
7 |
|
private function getServiceRegistry( |
113
|
|
|
?string $class = null |
114
|
|
|
): ServiceRegistryInterface { |
115
|
7 |
|
$class = $class ?? $this->serviceRegistry; |
116
|
7 |
|
$serviceRegistry = new $class(); |
117
|
|
|
|
118
|
7 |
|
return $serviceRegistry; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Normaliza el nombre del servicio. |
123
|
|
|
* |
124
|
|
|
* Solo se normaliza si el nombre es el código del servicio y no tiene el |
125
|
|
|
* prefijo requerido. Si se solicita el servicio a través del nombre de una |
126
|
|
|
* clase (FQCN) no se normalizará. |
127
|
|
|
* |
128
|
|
|
* @param string $service |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
6 |
|
private function normalizeServiceName(string $service): string |
132
|
|
|
{ |
133
|
6 |
|
if (str_contains($service, '\\')) { |
134
|
1 |
|
return $service; |
135
|
|
|
} |
136
|
|
|
|
137
|
5 |
|
if (str_starts_with($service, $this->servicesPrefix)) { |
138
|
|
|
return $service; |
139
|
|
|
} |
140
|
|
|
|
141
|
5 |
|
return $this->servicesPrefix . $service; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|