From def3b7092a57caa43885f88dd1046f80d6673115 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Mon, 14 Nov 2022 22:47:38 +0100 Subject: [PATCH] Combine extend in SetupGacela --- src/Framework/Bootstrap/SetupGacela.php | 30 ++++++++++++++----- .../Framework/Bootstrap/SetupGacelaTest.php | 27 +++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/Framework/Bootstrap/SetupGacela.php b/src/Framework/Bootstrap/SetupGacela.php index 31f66a1b..42313d1e 100644 --- a/src/Framework/Bootstrap/SetupGacela.php +++ b/src/Framework/Bootstrap/SetupGacela.php @@ -341,6 +341,7 @@ public function combine(self $other): self $this->combineProjectNamespaces($other); $this->combineConfigKeyValues($other); $this->combineEventDispatcher($other); + $this->combineServicesToExtend($other); return $this; } @@ -425,6 +426,19 @@ private function combineEventDispatcher(self $other): void } } + private function combineServicesToExtend(self $other): void + { + if ($other->isPropertyChanged('servicesToExtend')) { + foreach ($other->getServicesToExtend() as $serviceId => $otherServiceToExtend) { + $this->servicesToExtend[$serviceId] ??= []; + $this->servicesToExtend[$serviceId] = array_merge( + $this->servicesToExtend[$serviceId], + $otherServiceToExtend + ); + } + } + } + private function canCreateEventDispatcher(): bool { return $this->areEventListenersEnabled @@ -460,23 +474,23 @@ private function setGenericListeners(?array $listeners): self } /** - * @param ?array> $listeners + * @param ?array> $list */ - private function setSpecificListeners(?array $listeners): self + private function setServicesToExtend(?array $list): self { - $this->markPropertyChanged('specificListeners', $listeners); - $this->specificListeners = $listeners ?? self::DEFAULT_SPECIFIC_LISTENERS; + $this->markPropertyChanged('servicesToExtend', $list); + $this->servicesToExtend = $list ?? self::DEFAULT_SERVICES_TO_EXTEND; return $this; } /** - * @param ?array> $list + * @param ?array> $listeners */ - private function setServicesToExtend(?array $list): self + private function setSpecificListeners(?array $listeners): self { - $this->markPropertyChanged('extendGlobalServices', $list); - $this->servicesToExtend = $list ?? self::DEFAULT_SERVICES_TO_EXTEND; + $this->markPropertyChanged('specificListeners', $listeners); + $this->specificListeners = $listeners ?? self::DEFAULT_SPECIFIC_LISTENERS; return $this; } diff --git a/tests/Unit/Framework/Bootstrap/SetupGacelaTest.php b/tests/Unit/Framework/Bootstrap/SetupGacelaTest.php index 73e147dc..b9984317 100644 --- a/tests/Unit/Framework/Bootstrap/SetupGacelaTest.php +++ b/tests/Unit/Framework/Bootstrap/SetupGacelaTest.php @@ -4,6 +4,7 @@ namespace GacelaTest\Unit\Framework\Bootstrap; +use ArrayObject; use Gacela\Framework\Bootstrap\GacelaConfig; use Gacela\Framework\Bootstrap\SetupGacela; use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher; @@ -174,4 +175,30 @@ public function test_combine_external_services(): void 'service3' => new stdClass(), ], $setup->externalServices()); } + + public function test_combine_extend_service(): void + { + $setup = SetupGacela::fromGacelaConfig( + (new GacelaConfig()) + ->extendService('service', static fn (ArrayObject $ao) => $ao->append(1)) + ); + + $setup2 = SetupGacela::fromGacelaConfig( + (new GacelaConfig()) + ->extendService('service', static fn (ArrayObject $ao) => $ao->append(2)) + ->extendService('service-2', static fn (ArrayObject $ao) => $ao->append(3)) + ); + + $setup->combine($setup2); + + self::assertEquals([ + 'service' => [ + static fn (ArrayObject $ao) => $ao->append(1), + static fn (ArrayObject $ao) => $ao->append(2), + ], + 'service-2' => [ + static fn (ArrayObject $ao) => $ao->append(3), + ], + ], $setup->getServicesToExtend()); + } }