Skip to content

Commit

Permalink
bug #34370 [DI] fix detecting singly implemented interfaces (nicolas-…
Browse files Browse the repository at this point in the history
…grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[DI] fix detecting singly implemented interfaces

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34361
| License       | MIT
| Doc PR        | -

Commits
-------

eccb937 [DI] fix detecting singly implemented interfaces
  • Loading branch information
fabpot committed Nov 14, 2019
2 parents 29a2f17 + eccb937 commit afdb89f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Expand Up @@ -112,7 +112,7 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
continue;
}
foreach (class_implements($class, false) as $interface) {
$this->singlyImplemented[$interface] = isset($this->singlyImplemented[$interface]) ? false : $class;
$this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class;
}
}
}
Expand Down
Expand Up @@ -91,6 +91,8 @@ public function testRegisterClasses()
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));

$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*');
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*'); // loading twice should not be an issue
$loader->registerAliasesForSinglyImplementedInterfaces();

$this->assertEquals(
['service_container', Bar::class],
Expand Down Expand Up @@ -119,6 +121,7 @@ public function testRegisterClassesWithExclude()
// load everything, except OtherDir/AnotherSub & Foo.php
'Prototype/{%other_dir%/AnotherSub,Foo.php}'
);
$loader->registerAliasesForSinglyImplementedInterfaces();

$this->assertTrue($container->has(Bar::class));
$this->assertTrue($container->has(Baz::class));
Expand Down Expand Up @@ -148,6 +151,8 @@ public function testRegisterClassesWithExcludeAsArray()
'Prototype/OtherDir/AnotherSub/DeeperBaz.php',
]
);
$loader->registerAliasesForSinglyImplementedInterfaces();

$this->assertTrue($container->has(Foo::class));
$this->assertTrue($container->has(Baz::class));
$this->assertFalse($container->has(Bar::class));
Expand All @@ -162,6 +167,7 @@ public function testNestedRegisterClasses()
$prototype = new Definition();
$prototype->setPublic(true)->setPrivate(true);
$loader->registerClasses($prototype, 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/*');
$loader->registerAliasesForSinglyImplementedInterfaces();

$this->assertTrue($container->has(Bar::class));
$this->assertTrue($container->has(Baz::class));
Expand Down Expand Up @@ -193,6 +199,7 @@ public function testMissingParentClass()
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\\',
'Prototype/%bad_classes_dir%/*'
);
$loader->registerAliasesForSinglyImplementedInterfaces();

$this->assertTrue($container->has(MissingParent::class));

Expand All @@ -211,6 +218,7 @@ public function testRegisterClassesWithBadPrefix()

// the Sub is missing from namespace prefix
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/Sub/*');
$loader->registerAliasesForSinglyImplementedInterfaces();
}

public function testRegisterClassesWithIncompatibleExclude()
Expand All @@ -226,6 +234,7 @@ public function testRegisterClassesWithIncompatibleExclude()
'Prototype/*',
'yaml/*'
);
$loader->registerAliasesForSinglyImplementedInterfaces();
}
}

Expand All @@ -240,10 +249,4 @@ public function supports($resource, $type = null): bool
{
return false;
}

public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
{
parent::registerClasses($prototype, $namespace, $resource, $exclude);
$this->registerAliasesForSinglyImplementedInterfaces();
}
}

0 comments on commit afdb89f

Please sign in to comment.