1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Bootstrap; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher; |
8
|
|
|
use Gacela\Framework\Event\Dispatcher\NullEventDispatcher; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @psalm-suppress MixedArgumentTypeCoercion |
12
|
|
|
*/ |
13
|
|
|
final class SetupCombinator |
14
|
|
|
{ |
15
|
|
|
public function __construct(private SetupGacela $original) |
16
|
|
|
{ |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function combine(SetupGacela $other): SetupGacela |
20
|
|
|
{ |
21
|
|
|
$this->overrideResetInMemoryCache($other); |
22
|
|
|
$this->overrideFileCacheSettings($other); |
23
|
|
|
|
24
|
|
|
$this->combineExternalServices($other); |
25
|
|
|
$this->combineProjectNamespaces($other); |
26
|
|
|
$this->combineConfigKeyValues($other); |
27
|
|
|
$this->combineEventDispatcher($other); |
28
|
|
|
$this->combineServicesToExtend($other); |
29
|
|
|
|
30
|
|
|
return $this->original; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function overrideResetInMemoryCache(SetupGacela $other): void |
34
|
|
|
{ |
35
|
|
|
if ($other->isPropertyChanged(SetupGacela::shouldResetInMemoryCache)) { |
36
|
|
|
$this->original->setShouldResetInMemoryCache($other->shouldResetInMemoryCache()); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function overrideFileCacheSettings(SetupGacela $other): void |
41
|
|
|
{ |
42
|
|
|
if ($other->isPropertyChanged(SetupGacela::fileCacheEnabled)) { |
43
|
|
|
$this->original->setFileCacheEnabled($other->isFileCacheEnabled()); |
44
|
|
|
} |
45
|
|
|
if ($other->isPropertyChanged(SetupGacela::fileCacheDirectory)) { |
46
|
|
|
$this->original->setFileCacheDirectory($other->getFileCacheDirectory()); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function combineExternalServices(SetupGacela $other): void |
51
|
|
|
{ |
52
|
|
|
if ($other->isPropertyChanged(SetupGacela::externalServices)) { |
53
|
|
|
$this->original->combineExternalServices($other->externalServices()); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function combineProjectNamespaces(SetupGacela $other): void |
58
|
|
|
{ |
59
|
|
|
if ($other->isPropertyChanged(SetupGacela::projectNamespaces)) { |
60
|
|
|
$this->original->combineProjectNamespaces($other->getProjectNamespaces()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function combineConfigKeyValues(SetupGacela $other): void |
65
|
|
|
{ |
66
|
|
|
if ($other->isPropertyChanged(SetupGacela::configKeyValues)) { |
67
|
|
|
$this->original->combineConfigKeyValues($other->getConfigKeyValues()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function combineEventDispatcher(SetupGacela $other): void |
72
|
|
|
{ |
73
|
|
|
if ($other->canCreateEventDispatcher()) { |
74
|
|
|
if ($this->original->getEventDispatcher() instanceof ConfigurableEventDispatcher) { |
75
|
|
|
$eventDispatcher = $this->original->getEventDispatcher(); |
76
|
|
|
} else { |
77
|
|
|
$eventDispatcher = new ConfigurableEventDispatcher(); |
78
|
|
|
} |
79
|
|
|
/** @var ConfigurableEventDispatcher $eventDispatcher */ |
80
|
|
|
$eventDispatcher->registerGenericListeners((array)$other->getGenericListeners()); |
81
|
|
|
|
82
|
|
|
foreach ($other->getSpecificListeners() ?? [] as $event => $listeners) { |
83
|
|
|
foreach ($listeners as $callable) { |
84
|
|
|
$eventDispatcher->registerSpecificListener($event, $callable); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
$eventDispatcher = new NullEventDispatcher(); |
89
|
|
|
} |
90
|
|
|
$this->original->setEventDispatcher($eventDispatcher); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function combineServicesToExtend(SetupGacela $other): void |
94
|
|
|
{ |
95
|
|
|
if ($other->isPropertyChanged(SetupGacela::servicesToExtend)) { |
96
|
|
|
foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) { |
97
|
|
|
$this->original->addServicesToExtend($serviceId, $otherServiceToExtend); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|