Skip to content

Commit

Permalink
Also consider parent classes as aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Jun 11, 2021
1 parent 5e75e38 commit 950e9bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions core-bundle/src/Routing/ResponseContext/ResponseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ public function add(object $service): self
{
$this->container->set(\get_class($service), $service);

// Automatically add aliases for all interfaces (last one added automatically wins by overriding here)
foreach ((new \ReflectionClass($service))->getInterfaceNames() as $interfaceName) {
$ref = new \ReflectionClass($service);

// Automatically add aliases for all interfaces and parents (last one added automatically wins by overriding here)
foreach ($ref->getInterfaceNames() as $interfaceName) {
$this->container->set($interfaceName, $service);
}

while ($ref = $ref->getParentClass()) {
$this->container->set($ref->getName(), $service);
}

return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Contao\NewsBundle\ContaoNewsBundle;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

class ResponseContextTest extends TestCase
Expand Down Expand Up @@ -46,7 +47,7 @@ public function testGettingANonExistentServiceThrows(): void
$context->get(HtmlHeadBag::class);
}

public function testInterfacesAreAutomaticallyAddedAsAliases(): void
public function testInterfacesAndParentsAreAutomaticallyAddedAsAliases(): void
{
$context = new ResponseContext();

Expand All @@ -59,12 +60,14 @@ public function testInterfacesAreAutomaticallyAddedAsAliases(): void

$this->assertSame($coreBundle, $context->get(ContaoCoreBundle::class));
$this->assertSame($coreBundle, $context->get(BundleInterface::class));
$this->assertSame($coreBundle, $context->get(Bundle::class));

$context->add($newsBundle);

$this->assertSame($coreBundle, $context->get(ContaoCoreBundle::class));
$this->assertSame($newsBundle, $context->get(ContaoNewsBundle::class));
$this->assertSame($newsBundle, $context->get(BundleInterface::class)); // NewsBundle was added later
$this->assertSame($newsBundle, $context->get(Bundle::class)); // NewsBundle was added later
}

public function testHeaderBagIsInitializedCompletelyEmpty(): void
Expand Down

0 comments on commit 950e9bb

Please sign in to comment.