Skip to content

Commit

Permalink
Merge pull request #78 from boesing/bugfix/resolved-aliases-non-share…
Browse files Browse the repository at this point in the history
…d-service

Regression in 3.6.x regarding aliased services in combination with non-shared services
  • Loading branch information
Ocramius authored Jan 25, 2021
2 parents 11ea2dd + 0cd0a07 commit bf5fe52
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public function get($name)
// We now deal with requests which may be aliases.
$resolvedName = isset($this->aliases[$name]) ? $this->aliases[$name] : $name;

// Update shared service information as we checked if the alias was shared before.
if ($resolvedName !== $name) {
$sharedService = $this->shared[$resolvedName] ?? $sharedService;
}

// The following is only true if the requested service is a shared alias.
$sharedAlias = $sharedService && isset($this->services[$resolvedName]);

Expand Down
88 changes: 88 additions & 0 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,92 @@ static function (object $service) use (&$initializerTwoCalled): object {
self::assertTrue($initializerOneCalled, 'First initializer was not called');
self::assertTrue($initializerTwoCalled, 'Second initializer was not called');
}

/**
* @param array<string,mixed> $config
* @param non-empty-string $serviceName
* @param non-empty-string $alias
* @dataProvider aliasedServices
*/
public function testWontShareServiceWhenRequestedByAlias(array $config, string $serviceName, string $alias): void
{
$serviceManager = new ServiceManager($config);
$service = $serviceManager->get($serviceName);
$serviceFromAlias = $serviceManager->get($alias);
$serviceFromServiceNameAfterUsingAlias = $serviceManager->get($serviceName);

self::assertNotSame($service, $serviceFromAlias);
self::assertNotSame($service, $serviceFromServiceNameAfterUsingAlias);
self::assertNotSame($serviceFromAlias, $serviceFromServiceNameAfterUsingAlias);
}

/**
* @return array<non-empty-string,array{0:array<string,mixed>,1:non-empty-string,2:non-empty-string}>
*/
public function aliasedServices(): array
{
return [
'invokables' => [
[
'invokables' => [
stdClass::class => stdClass::class,
],
'aliases' => [
'object' => stdClass::class,
],
'shared' => [
stdClass::class => false,
],
],
stdClass::class,
'object',
],
'factories' => [
[
'factories' => [
stdClass::class => static function (): stdClass {
return new stdClass();
},
],
'aliases' => [
'object' => stdClass::class,
],
'shared' => [
stdClass::class => false,
],
],
stdClass::class,
'object',
],
'abstract factories' => [
[
'abstract_factories' => [
new class implements AbstractFactoryInterface {

public function canCreate(\Interop\Container\ContainerInterface $container, $requestedName)
{
return $requestedName === stdClass::class;
}

public function __invoke(
\Interop\Container\ContainerInterface $container,
$requestedName,
array $options = null
) {
return new stdClass();
}
}
],
'aliases' => [
'object' => stdClass::class,
],
'shared' => [
stdClass::class => false,
],
],
stdClass::class,
'object',
],
];
}
}

0 comments on commit bf5fe52

Please sign in to comment.