|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Bootstrap\Setup; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Bootstrap\SetupGacela; |
|
8
|
|
|
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Merges two SetupGacela instances together with conditional logic based on change tracking. |
|
12
|
|
|
* |
|
13
|
|
|
* @psalm-suppress MixedArgumentTypeCoercion |
|
14
|
|
|
*/ |
|
15
|
|
|
final class SetupMerger |
|
16
|
|
|
{ |
|
17
|
|
|
public function __construct( |
|
18
|
|
|
private readonly SetupGacela $original, |
|
19
|
|
|
) { |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function merge(SetupGacela $other): SetupGacela |
|
23
|
|
|
{ |
|
24
|
|
|
$this->overrideResetInMemoryCache($other); |
|
25
|
|
|
$this->overrideFileCacheSettings($other); |
|
26
|
|
|
|
|
27
|
|
|
$this->mergeExternalServices($other); |
|
28
|
|
|
$this->mergeProjectNamespaces($other); |
|
29
|
|
|
$this->mergeConfigKeyValues($other); |
|
30
|
|
|
$this->mergeEventDispatcher($other); |
|
31
|
|
|
$this->mergeServicesToExtend($other); |
|
32
|
|
|
$this->mergePlugins($other); |
|
33
|
|
|
$this->mergeGacelaConfigsToExtend($other); |
|
34
|
|
|
|
|
35
|
|
|
return $this->original; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function overrideResetInMemoryCache(SetupGacela $other): void |
|
39
|
|
|
{ |
|
40
|
|
|
if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) { |
|
41
|
|
|
$this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache()); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function overrideFileCacheSettings(SetupGacela $other): void |
|
46
|
|
|
{ |
|
47
|
|
|
if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) { |
|
48
|
|
|
$this->original->setFileCacheEnabled($other->isFileCacheEnabled()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) { |
|
52
|
|
|
$this->original->setFileCacheDirectory($other->getFileCacheDirectory()); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function mergeExternalServices(SetupGacela $other): void |
|
57
|
|
|
{ |
|
58
|
|
|
if ($other->isPropertyChanged(SetupGacela::externalServices)) { |
|
59
|
|
|
$this->original->mergeExternalServices($other->externalServices()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function mergeProjectNamespaces(SetupGacela $other): void |
|
64
|
|
|
{ |
|
65
|
|
|
if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) { |
|
66
|
|
|
$this->original->mergeProjectNamespaces($other->getProjectNamespaces()); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function mergeConfigKeyValues(SetupGacela $other): void |
|
71
|
|
|
{ |
|
72
|
|
|
if ($other->isPropertyChanged(SetupGacela::configKeyValues)) { |
|
73
|
|
|
$this->original->mergeConfigKeyValues($other->getConfigKeyValues()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function mergeEventDispatcher(SetupGacela $other): void |
|
78
|
|
|
{ |
|
79
|
|
|
if ($other->canCreateEventDispatcher()) { |
|
80
|
|
|
if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) { |
|
81
|
|
|
$eventDispatcher = $this->original->getEventDispatcher(); |
|
82
|
|
|
} else { |
|
83
|
|
|
$eventDispatcher = new ConfigurableEventDispatcher(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @var ConfigurableEventDispatcher $eventDispatcher */ |
|
87
|
|
|
$eventDispatcher->registerGenericListeners((array)$other->getGenericListeners()); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) { |
|
90
|
|
|
foreach ($listeners as $callable) { |
|
91
|
|
|
$eventDispatcher->registerSpecificListener($event, $callable); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} else { |
|
95
|
|
|
$eventDispatcher = $this->original->getEventDispatcher(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->original->setEventDispatcher($eventDispatcher); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function mergeServicesToExtend(SetupGacela $other): void |
|
102
|
|
|
{ |
|
103
|
|
|
if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) { |
|
104
|
|
|
foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) { |
|
105
|
|
|
$this->original->addServicesToExtend($serviceId, $otherServiceToExtend); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function mergePlugins(SetupGacela $other): void |
|
111
|
|
|
{ |
|
112
|
|
|
if ($other->isPropertyChanged(SetupGacela::plugins)) { |
|
113
|
|
|
$this->original->mergePlugins($other->getPlugins()); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function mergeGacelaConfigsToExtend(SetupGacela $other): void |
|
118
|
|
|
{ |
|
119
|
|
|
if ($other->isPropertyChanged(SetupGacela::gacelaConfigsToExtend)) { |
|
120
|
|
|
$this->original->mergeGacelaConfigsToExtend($other->getGacelaConfigsToExtend()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|