chubbyphp / chubbyphp-container
一个简单的PSR-11容器实现。
2.2.0
2023-11-27 20:24 UTC
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
描述
一个最小的依赖注入容器(DIC),实现了PSR-11。 DI Container Benchmark。
在 chubbyphp/chubbyphp-laminas-config 有一个 laminas 服务管理器适配器。
需求
- php: ^8.1
- psr/container: ^2.0.2
安装
通过 Composer 安装 chubbyphp/chubbyphp-container。
composer require chubbyphp/chubbyphp-container "^2.2"
使用方法
有两个PSR-11实现
Chubbyphp\Container\Container
原型(每次获取都会返回一个新的实例)和共享服务Chubbyphp\Container\MinimalContainer
共享服务
MinimalContainer / Container
工厂
<?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);
容器
MinimalContainer
的所有方法以及以下
原型工厂
每次获取都会返回一个新的实例
<?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)); }, ]);
原型工厂
每次获取都会返回一个新的实例
<?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; } );
迁移
版权
2024 Dominik Zogg