Skip to content

Commit

Permalink
Fix delegates resetters (#288)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
yiiliveext and samdark committed Dec 3, 2021
1 parent 2a94324 commit 0b18760
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/Container.php
Expand Up @@ -155,18 +155,33 @@ public function get($id)
}
}

if ($this->useResettersFromMeta && $id === StateResetter::class) {
$resetters = [];
foreach ($this->resetters as $serviceId => $callback) {
if (isset($this->instances[$serviceId])) {
$resetters[$serviceId] = $callback;
}
}
if ($id === StateResetter::class) {
$delegatesResetter = null;
if ($this->delegates->has(StateResetter::class)) {
$resetters[] = $this->delegates->get(StateResetter::class);
$delegatesResetter = $this->delegates->get(StateResetter::class);
}

/** @var StateResetter $mainResetter */
$mainResetter = $this->instances[$id];

if ($this->useResettersFromMeta) {
/** @var StateResetter[] $resetters */
$resetters = [];
foreach ($this->resetters as $serviceId => $callback) {
if (isset($this->instances[$serviceId])) {
$resetters[$serviceId] = $callback;
}
}
if ($delegatesResetter !== null) {
$resetters[] = $delegatesResetter;
}
$mainResetter->setResetters($resetters);
} elseif ($delegatesResetter !== null) {
$resetter = new StateResetter($this->get(ContainerInterface::class));
$resetter->setResetters([$mainResetter, $delegatesResetter]);

return $resetter;
}
/** @psalm-suppress MixedMethodCall Instance of `StateResetter` */
$this->instances[$id]->setResetters($resetters);
}

/** @psalm-suppress MixedReturnStatement */
Expand Down Expand Up @@ -394,6 +409,7 @@ private function setTags(array $tags): void
)
);
}
/** @var mixed $service */
foreach ($services as $service) {
if (!is_string($service)) {
throw new InvalidConfigException(
Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/ContainerTest.php
Expand Up @@ -1191,6 +1191,63 @@ static function (ContainerInterface $container) {
$this->assertSame(42, $engine->getNumber());
}

public function testResetterInDelegatesWithCustomResetter(): void
{
$config = ContainerConfig::create()
->withDelegates([
static function (ContainerInterface $container) {
$config = ContainerConfig::create()
->withDefinitions([
EngineInterface::class => [
'class' => EngineMarkOne::class,
'setNumber()' => [42],
'reset' => function () {
$this->number = 42;
},
],
]);
return new Container($config);
},
])
->withDefinitions([
Car::class => [
'class' => Car::class,
'setColor()' => [new ColorPink()],
],
StateResetter::class => [
'class' => StateResetter::class,
'setResetters()' => [
[
Car::class => function () {
$this->color = new ColorPink();
},
],
],
],
]);
$container = new Container($config);

$engine = $container->get(EngineInterface::class);
$this->assertSame(42, $container->get(EngineInterface::class)->getNumber());

$car = $container->get(Car::class);
$this->assertInstanceOf(ColorPink::class, $container->get(Car::class)->getColor());

$engine->setNumber(45);
$this->assertSame(45, $container->get(EngineInterface::class)->getNumber());

$car->setColor(new ColorRed());
$this->assertInstanceOf(ColorRed::class, $container->get(Car::class)->getColor());

$container->get(StateResetter::class)->reset();

$this->assertSame($engine, $container->get(EngineInterface::class));
$this->assertSame(42, $engine->getNumber());

$this->assertSame($car, $container->get(Car::class));
$this->assertInstanceOf(ColorPink::class, $container->get(Car::class)->getColor());
}

public function dataResetterInProviderDefinitions(): array
{
return [
Expand Down

0 comments on commit 0b18760

Please sign in to comment.