Skip to content

Commit

Permalink
Going back to a regular array and removed the remove() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Jun 11, 2021
1 parent 950e9bb commit fbe8206
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
30 changes: 11 additions & 19 deletions core-bundle/src/Routing/ResponseContext/ResponseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,36 @@ final class ResponseContext
public const REQUEST_ATTRIBUTE_NAME = '_contao_response_context';

/**
* @var Container
* @var array
*/
private $container;
private $services;

/**
* @var PartialResponseHeaderBag|null
*/
private $headerBag;

public function __construct()
{
$this->container = new Container();
}

public function remove(string $serviceId): self
{
$this->container->set($serviceId, null);

return $this;
}

public function add(object $service): self
{
$this->container->set(\get_class($service), $service);
$this->services[\get_class($service)] = $service;

$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);
$this->services[$interfaceName] = $service;
}

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

return $this;
}

public function has(string $serviceId): bool
{
return $this->container->has($serviceId);
return isset($this->services[$serviceId]);
}

/**
Expand All @@ -76,7 +64,11 @@ public function has(string $serviceId): bool
*/
public function get(string $serviceId)
{
return $this->container->get($serviceId, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
if (!$this->has($serviceId)) {
throw new \InvalidArgumentException(sprintf('Service "%s" does not exist.', $serviceId));
}

return $this->services[$serviceId];
}

public function getHeaderBag(): PartialResponseHeaderBag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
use Contao\CoreBundle\Routing\ResponseContext\ResponseContext;
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
{
public function testCanAddRemoveAndGetServices(): void
public function testCanAddAndGetServices(): void
{
$context = new ResponseContext();

Expand All @@ -33,15 +32,11 @@ public function testCanAddRemoveAndGetServices(): void

$this->assertTrue($context->has(HtmlHeadBag::class));
$this->assertInstanceOf(HtmlHeadBag::class, $context->get(HtmlHeadBag::class));

$context->remove(HtmlHeadBag::class);

$this->assertFalse($context->has(HtmlHeadBag::class));
}

public function testGettingANonExistentServiceThrows(): void
{
$this->expectException(ServiceNotFoundException::class);
$this->expectException(\InvalidArgumentException::class);

$context = new ResponseContext();
$context->get(HtmlHeadBag::class);
Expand Down

0 comments on commit fbe8206

Please sign in to comment.