Skip to content

Instantly share code, notes, and snippets.

@dg
Last active December 10, 2024 00:04
Show Gist options
  • 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 Nette\DI\Container;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
// ↓ For the sake of this example, I'm implementing these two ↓
// ↓ Exceptions here, but they should be in their own namespace, ↓
// ↓ as should Psr11ContainerAdapter for that matter: ↓
class Psr11ContainerException extends Exception implements ContainerExceptionInterface
{
}
class Psr11NotFoundException extends Psr11ContainerException implements NotFoundExceptionInterface
{
}
class Psr11ContainerAdapter implements ContainerInterface
{
public function __construct(
private Container $netteContainer
) {
}
public function get($id)
{
try {
return $this->netteContainer->getService($id);
} catch (Nette\DI\MissingServiceException $e) {
throw new Psr11NotFoundException("Service not found: {$id}", previous: $e);
} catch (Throwable $e) {
throw new Psr11ContainerException("Failed to resolve service: {$id}", previous: $e);
}
}
public function has($id)
{
return $this->netteContainer->hasService($id);
}
}
// usage:
// $psr11Adapter = new Psr11ContainerAdapter($netteContainer);
@dg
Copy link
Author

dg commented Dec 10, 2024

@michkozak thanks, I updated the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment