Skip to content

Commit 0116886

Browse files
committed
Add append and prepend methods o AggregateContainer
1 parent 2c1492c commit 0116886

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/AggregateContainer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ public function __construct(ContainerInterface ...$containers)
6565
];
6666
}
6767

68+
/**
69+
* Appends a container to the end of the aggregated list.
70+
*
71+
* This method MAY be used to dynamically expand the resolution pool.
72+
*
73+
* @param ContainerInterface $container the container to append
74+
*/
75+
public function append(ContainerInterface $container): void
76+
{
77+
$this->containers[] = $container;
78+
}
79+
80+
/**
81+
* Prepends a container to the beginning of the aggregated list.
82+
*
83+
* This method MAY be used to prioritize a container during resolution.
84+
*
85+
* @param ContainerInterface $container the container to prepend
86+
*/
87+
public function prepend(ContainerInterface $container): void
88+
{
89+
array_unshift($this->containers, $container);
90+
}
91+
6892
/**
6993
* Determines whether a service identifier can be resolved.
7094
*

src/Factory/ContainerFactory.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ public function __invoke(ContainerInterface ...$containers): ContainerInterface
7373
$dependencies = $dependencies->toArray();
7474
}
7575

76-
// Add an autowire-based container using provided dependencies.
77-
$containers[] = new Container($dependencies);
78-
79-
return new AggregateContainer(
76+
$container = new AggregateContainer(
8077
new ConfigContainer($this->config),
8178
...$containers,
8279
);
80+
81+
// Add an autowire-based container using provided dependencies.
82+
$container->append(new Container(definitions: $dependencies, wrapperContainer: $container));
83+
84+
return $container;
8385
}
8486
}

tests/AggregateContainerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,45 @@ public function testGetThrowsWhenNoContainerCanResolve(): void
158158

159159
$container->get($key);
160160
}
161+
162+
#[Test]
163+
public function testAppendAddsContainerAtTheEnd(): void
164+
{
165+
$key = uniqid('svc_', true);
166+
$value = uniqid('val_', true);
167+
168+
$first = $this->prophesize(ContainerInterface::class);
169+
$first->has($key)->willReturn(false);
170+
171+
$second = $this->prophesize(ContainerInterface::class);
172+
$second->has($key)->willReturn(true);
173+
$second->get($key)->willReturn($value);
174+
175+
$container = new AggregateContainer($first->reveal());
176+
$container->append($second->reveal());
177+
178+
self::assertTrue($container->has($key));
179+
self::assertSame($value, $container->get($key));
180+
}
181+
182+
#[Test]
183+
public function testPrependAddsContainerAtTheBeginning(): void
184+
{
185+
$key = uniqid('service_', true);
186+
$value = uniqid('val_', true);
187+
188+
$first = $this->prophesize(ContainerInterface::class);
189+
$first->has($key)->willReturn(true);
190+
$first->get($key)->willReturn($value);
191+
192+
$second = $this->prophesize(ContainerInterface::class);
193+
$second->has($key)->willReturn(true);
194+
$second->get($key)->willReturn('incorrect');
195+
196+
$container = new AggregateContainer($second->reveal());
197+
$container->prepend($first->reveal());
198+
199+
self::assertTrue($container->has($key));
200+
self::assertSame($value, $container->get($key));
201+
}
161202
}

0 commit comments

Comments
 (0)