chubbyphp / chubbyphp-container
A simple PSR-11 container implementation.
Installs: 62 584
Dependents: 15
Suggesters: 1
Security: 0
Stars: 15
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- psr/container: ^2.0.2
Requires (Dev)
- chubbyphp/chubbyphp-dev-helper: dev-master
- chubbyphp/chubbyphp-mock: ^1.7.0
- infection/infection: ^0.27.8
- php-coveralls/php-coveralls: ^2.7.0
- phpstan/extension-installer: ^1.3.1
- phpstan/phpstan: ^1.10.45
- phpunit/phpunit: ^10.4.2
README
Description
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.
Requirements
- php: ^8.1
- psr/container: ^2.0.2
Installation
Through Composer as chubbyphp/chubbyphp-container.
composer require chubbyphp/chubbyphp-container "^2.2"
Usage
There are two PSR-11 implementations:
Chubbyphp\Container\Container
prototype (each get will return a new instance) and shared servicesChubbyphp\Container\MinimalContainer
shared services
MinimalContainer / Container
Factories
<?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)); }, ]);
Factory
<?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; } );
Factory with Parameter
<?php use Chubbyphp\Container\MinimalContainer; use Chubbyphp\Container\Parameter; $container = new MinimalContainer(); $container->factory('key', new Parameter('value'));
Get
<?php use App\Service\MyService; use Chubbyphp\Container\MinimalContainer; $container = new MinimalContainer(); $myService = $container->get(MyService::class);
Has
<?php use App\Service\MyService; use Chubbyphp\Container\MinimalContainer; $container = new MinimalContainer(); $container->has(MyService::class);
Container
All methods of the MinimalContainer
and the following:
Prototype Factories
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)); }, ]);
Prototype Factory
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; } );
Migration
Copyright
2024 Dominik Zogg