Skip to content

Commit

Permalink
Combine extend in SetupGacela
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Nov 14, 2022
1 parent de6296b commit def3b70
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Framework/Bootstrap/SetupGacela.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public function combine(self $other): self
$this->combineProjectNamespaces($other);
$this->combineConfigKeyValues($other);
$this->combineEventDispatcher($other);
$this->combineServicesToExtend($other);

return $this;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -460,23 +474,23 @@ private function setGenericListeners(?array $listeners): self
}

/**
* @param ?array<class-string,list<callable>> $listeners
* @param ?array<string,list<Closure>> $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<string,list<Closure>> $list
* @param ?array<class-string,list<callable>> $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;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/Framework/Bootstrap/SetupGacelaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit def3b70

Please sign in to comment.