Skip to content

Instantly share code, notes, and snippets.

@dg
Created March 22, 2023 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dg/7f02403bd36d9d1c73802a6268a4361f to your computer and use it in GitHub Desktop.
Save dg/7f02403bd36d9d1c73802a6268a4361f to your computer and use it in GitHub Desktop.
PSR-11 adapter for Nette DI Container
<?php
declare(strict_types=1);
use Psr\Container\ContainerInterface;
use Nette\DI\Container;
class Psr11ContainerAdapter implements ContainerInterface
{
public function __construct(
private Container $netteContainer,
) {
}
public function get($id)
{
if (!$this->netteContainer->hasService($id)) {
throw new Psr\Container\NotFoundException(sprintf('Service not found: %s', $id));
}
return $this->netteContainer->getService($id);
}
public function has($id)
{
return $this->netteContainer->hasService($id);
}
}
// usage:
// $psr11Adapter = new Psr11ContainerAdapter($netteContainer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment