A minimal Dependency Injection Container (DIC) which implements PSR-11. DI Container Benchmark.
There is a laminas service manager adapter at chubbyphp/chubbyphp-laminas-config.
- php: ^8.2
- psr/container: ^2.0.2
Through Composer as chubbyphp/chubbyphp-container.
composer require chubbyphp/chubbyphp-container "^2.3"There are two PSR-11 implementations:
Chubbyphp\Container\Containerprototype (each get will return a new instance) and shared servicesChubbyphp\Container\MinimalContainershared services
<?php
use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
$container = new MinimalContainer();
$container->factories([
MyService::class => static function (ContainerInterface $container): MyService {
return new MyService($container->get(LoggerInterface::class));
},
]);<?php
use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
$container = new MinimalContainer();
// new
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
return new MyService($container->get(LoggerInterface::class));
});
// existing (replace)
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
return new MyService($container->get(LoggerInterface::class));
});
// existing (extend)
$container->factory(
MyService::class,
static function (ContainerInterface $container, callable $previous): MyService {
$myService = $previous($container);
$myService->setLogger($container->get(LoggerInterface::class));
return $myService;
}
);<?php
use Chubbyphp\Container\MinimalContainer;
use Chubbyphp\Container\Parameter;
$container = new MinimalContainer();
$container->factory('key', new Parameter('value'));<?php
use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
$container = new MinimalContainer();
$myService = $container->get(MyService::class);<?php
use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
$container = new MinimalContainer();
$container->has(MyService::class);All methods of the MinimalContainer and the following:
each get will return a new instance
<?php
use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
$container = new Container();
$container->prototypeFactories([
MyService::class => static function (ContainerInterface $container): MyService {
return new MyService($container->get(LoggerInterface::class));
},
]);each get will return a new instance
<?php
use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
$container = new Container();
// new
$container->prototypeFactory(
MyService::class,
static function (ContainerInterface $container): MyService {
return new MyService($container->get(LoggerInterface::class));
}
);
// existing (replace)
$container->prototypeFactory(
MyService::class,
static function (ContainerInterface $container): MyService {
return new MyService($container->get(LoggerInterface::class));
}
);
// existing (extend)
$container->prototypeFactory(
MyService::class,
static function (ContainerInterface $container, callable $previous): MyService {
$myService = $previous($container);
$myService->setLogger($container->get(LoggerInterface::class));
return $myService;
}
);2025 Dominik Zogg