Skip to content

Commit

Permalink
Resolve Circular dependency and Exceptions in Symfony container (#15)
Browse files Browse the repository at this point in the history
fix: resolve circular dependencies
  • Loading branch information
patrickkusebauch committed Mar 28, 2024
1 parent b1e520f commit aecac14
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
$services
->set(LayerResolver::class)
->args([
'$layers' => param('layers'),
'$layersConfig' => param('layers'),
]);
$services->alias(LayerResolverInterface::class, LayerResolver::class);
$services
Expand Down
36 changes: 24 additions & 12 deletions src/Core/Layer/LayerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,29 @@ class LayerResolver implements LayerResolverInterface
/**
* @var array<string, Collectable[]>
*/
private array $layers;
private array $layers = [];

private bool $initialized = false;

/**
* @var array<string, array<string, bool>>
*/
private array $resolved = [];

/**
* @param array<array{name?: string, collectors: array<array<string, string|array<string, string>>>}> $layers
*
* @throws InvalidLayerDefinitionException
* @param array<array{name?: string, collectors?: array<array<string, string|array<string, string>>>}> $layersConfig
*/
public function __construct(private readonly CollectorResolverInterface $collectorResolver, array $layers)
{
$this->initializeLayers($layers);
}
public function __construct(
private readonly CollectorResolverInterface $collectorResolver,
private readonly array $layersConfig,
) {}

public function getLayersForReference(TokenReferenceInterface $reference): array
{
if (false === $this->initialized) {
$this->initializeLayers();
}

$tokenName = $reference->getToken()->toString();
if (array_key_exists($tokenName, $this->resolved)) {
return $this->resolved[$tokenName];
Expand Down Expand Up @@ -64,6 +68,10 @@ public function getLayersForReference(TokenReferenceInterface $reference): array

public function isReferenceInLayer(string $layer, TokenReferenceInterface $reference): bool
{
if (false === $this->initialized) {
$this->initializeLayers();
}

$tokenName = $reference->getToken()->toString();
if (array_key_exists($tokenName, $this->resolved) && [] !== $this->resolved[$tokenName]) {
return array_key_exists($layer, $this->resolved[$tokenName]);
Expand All @@ -86,18 +94,20 @@ public function isReferenceInLayer(string $layer, TokenReferenceInterface $refer

public function has(string $layer): bool
{
if (false === $this->initialized) {
$this->initializeLayers();
}

return array_key_exists($layer, $this->layers);
}

/**
* @param array<array{name?: string, collectors?: array<array<string, string|array<string, string>>>}> $layers
*
* @throws InvalidLayerDefinitionException
*/
private function initializeLayers(array $layers): void
private function initializeLayers(): void
{
$this->layers = [];
foreach ($layers as $layer) {
foreach ($this->layersConfig as $layer) {
if (!array_key_exists('name', $layer)) {
throw InvalidLayerDefinitionException::missingName();
}
Expand All @@ -120,5 +130,7 @@ private function initializeLayers(array $layers): void
if ([] === $this->layers) {
throw InvalidLayerDefinitionException::layerRequired();
}

$this->initialized = true;
}
}
3 changes: 3 additions & 0 deletions src/Core/Layer/LayerResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ public function getLayersForReference(TokenReferenceInterface $reference): array
*/
public function isReferenceInLayer(string $layer, TokenReferenceInterface $reference): bool;

/**
* @throws InvalidLayerDefinitionException
*/
public function has(string $layer): bool;
}
4 changes: 3 additions & 1 deletion tests/Core/Layer/LayerResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ public function testInvalidLayerConfigs(array $layers, string $exception, string
$this->expectException($exception);
$this->expectExceptionMessage($expectedMessage);

new LayerResolver(
$resolver = new LayerResolver(
$this->createMock(CollectorResolverInterface::class),
$layers
);

$resolver->has('Anything');
}

public function testHas(): void
Expand Down

0 comments on commit aecac14

Please sign in to comment.