Created
March 22, 2023 00:32
-
-
Save dg/7f02403bd36d9d1c73802a6268a4361f to your computer and use it in GitHub Desktop.
PSR-11 adapter for Nette DI Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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