A lightweight, PSR-11 compatible dependency injection container implementing Solo\Contracts\Container\WritableContainerInterface.
You can install the package via composer:
composer require solophp/container- PHP 8.1 or higher
- Composer 2.0 or higher
use Solo\Container\Container;
// Create a new container
$container = new Container();
// Register a service
$container->set('database', function($container) {
return new Database('localhost', 'mydb', 'user', 'pass');
});
// Bind an interface to a concrete implementation
$container->bind(LoggerInterface::class, FileLogger::class);
// Get a service
$db = $container->get('database');- Implements
WritableContainerInterfacefromsolophp/contracts(PSR-11 compatible) - Automatic dependency resolution
- Interface binding
- Singleton instances
- Constructor injection
- Service factories
The container can automatically resolve class dependencies:
class UserRepository
{
public function __construct(
private Database $database,
private LoggerInterface $logger
) {}
}
// The container will automatically resolve Database and LoggerInterface
$userRepo = $container->get(UserRepository::class);$container = new Container([
'config' => fn() => new Config('config.php'),
'cache' => fn($c) => new Cache($c->get('config')),
]);$container->bind(LoggerInterface::class, FileLogger::class);
$container->bind(CacheInterface::class, RedisCache::class);composer testCheck code style:
composer csFix code style:
composer cs-fixThe container throws two types of exceptions:
Solo\Container\Exceptions\NotFoundException: When a requested service is not foundSolo\Container\Exceptions\ContainerException: When there's an error resolving a service
The MIT License (MIT). Please see License File for more information.