Skip to content

Commit 1b184d2

Browse files
committed
fix: do not remove delegators on service creation
Signed-off-by: mkrizan <krizan.martin2@gmail.com>
1 parent a699682 commit 1b184d2

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/ServiceManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,9 @@ private function createDelegatorFromName(string $name, ?array $options = null):
601601
$resolvedDelegators = [];
602602
foreach ($this->delegators[$name] as $index => $delegatorFactory) {
603603
/** @psalm-suppress ArgumentTypeCoercion https://github.com/vimeo/psalm/issues/9680 */
604-
$delegatorFactory = $this->resolveDelegatorFactory($delegatorFactory);
605-
$this->delegators[$name][$index] = $delegatorFactory;
606-
$creationCallback =
604+
$delegatorFactory = $this->resolveDelegatorFactory($delegatorFactory);
605+
$resolvedDelegators[$index] = $delegatorFactory;
606+
$creationCallback =
607607
static fn(): mixed => $delegatorFactory($initialCreationContext, $name, $creationCallback, $options);
608608
}
609609

test/ServiceManagerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace LaminasTest\ServiceManager;
66

77
use DateTime;
8+
use Laminas\ContainerConfigTest\TestAsset\Delegator;
89
use Laminas\ContainerConfigTest\TestAsset\DelegatorFactory;
910
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
1011
use Laminas\ServiceManager\Factory\FactoryInterface;
@@ -396,6 +397,30 @@ public function testDuplicateDelegatorsAreRemoved(): void
396397
);
397398
}
398399

400+
public function testDelegatorsAreUsedOnRepeatedCreate(): void
401+
{
402+
$dependencies = [
403+
'delegators' => [
404+
DateTime::class => [
405+
DelegatorFactory::class,
406+
DelegatorFactory::class,
407+
],
408+
],
409+
'invokables' => [
410+
DateTime::class => DateTime::class,
411+
],
412+
];
413+
$serviceManager = new ServiceManager($dependencies);
414+
415+
$first = $serviceManager->build(DateTime::class);
416+
$second = $serviceManager->build(DateTime::class);
417+
418+
/** @psalm-suppress DocblockTypeContradiction*/
419+
self::assertInstanceOf(Delegator::class, $first);
420+
/** @psalm-suppress DocblockTypeContradiction*/
421+
self::assertInstanceOf(Delegator::class, $second);
422+
}
423+
399424
public function testResolvedAliasFromAbstractFactory(): void
400425
{
401426
$abstractFactory = $this->createMock(AbstractFactoryInterface::class);

0 commit comments

Comments
 (0)