|
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; |
|
26
|
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Foundation\Contract\ConfigurationInterface; |
|
28
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface; |
|
29
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
30
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
31
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Clase para asignar cómo configurar los servicios previo a la compilación. |
|
35
|
|
|
*/ |
|
36
|
|
|
class ServiceConfigurationCompilerPass implements CompilerPassInterface |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Procesar servicios. |
|
40
|
|
|
* |
|
41
|
|
|
* @param ContainerBuilder $container |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
5 |
|
public function process(ContainerBuilder $container): void |
|
45
|
|
|
{ |
|
46
|
5 |
|
$configuration = $container->get(ConfigurationInterface::class); |
|
47
|
|
|
|
|
48
|
5 |
|
foreach ($container->getDefinitions() as $id => $definition) { |
|
49
|
5 |
|
if (method_exists($definition->getClass(), 'setConfiguration')) { |
|
50
|
|
|
$config = $this->resolveConfiguration($definition, $configuration); |
|
|
|
|
|
|
51
|
|
|
if ($config) { |
|
52
|
|
|
$definition->addMethodCall('setConfiguration', [$config]); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Entrega la configuración de una definición si existe, sino arreglo vacio. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Definition $definition |
|
62
|
|
|
* @param ConfigurationInterface $configuration |
|
63
|
|
|
* @return array|DataContainerInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
private function resolveConfiguration( |
|
66
|
|
|
Definition $definition, |
|
67
|
|
|
ConfigurationInterface $configuration |
|
68
|
|
|
): array|DataContainerInterface { |
|
69
|
|
|
$config = []; |
|
70
|
|
|
$tags = $definition->getTags(); |
|
71
|
|
|
|
|
72
|
|
|
if (!empty($tags['service:package'])) { |
|
73
|
|
|
$package = $tags['service:package'][0]['name']; |
|
74
|
|
|
$config = $configuration->getPackageConfiguration($package); |
|
75
|
|
|
} elseif (!empty($tags['service:component'])) { |
|
76
|
|
|
$package = $tags['service:component'][0]['package']; |
|
77
|
|
|
$component = $tags['service:component'][0]['name']; |
|
78
|
|
|
$config = $configuration->getPackageConfiguration($package); |
|
79
|
|
|
$config = $config['components'][$component] ?? []; |
|
80
|
|
|
} elseif (!empty($tags['service:worker'])) { |
|
81
|
|
|
$package = $tags['service:worker'][0]['package']; |
|
82
|
|
|
$component = $tags['service:worker'][0]['component']; |
|
83
|
|
|
$worker = $tags['service:worker'][0]['name']; |
|
84
|
|
|
$config = $configuration->getPackageConfiguration($package); |
|
85
|
|
|
$config = $config['components'][$component]['workers'][$worker] ?? []; |
|
86
|
|
|
} elseif (!empty($tags['service:command'])) { |
|
87
|
|
|
$package = $tags['service:command'][0]['package']; |
|
88
|
|
|
$component = $tags['service:command'][0]['component']; |
|
89
|
|
|
$command = $tags['service:command'][0]['name']; |
|
90
|
|
|
$config = $configuration->getPackageConfiguration($package); |
|
91
|
|
|
$config = $config['components'][$component]['commands'][$command] ?? []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $config; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|