mizmoz / container
Mizmoz DI 容器
2.0.0
2024-09-26 23:37 UTC
Requires
- php: >=8.3
- psr/container: ^2.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11.0
Suggests
- mizmoz/app: Required for app registration
- symfony/console: Required for running the phpstorm meta data command
README
目标
- 轻量级,懒加载所有内容。
- 使用 PHP-FIG PSR-11 v2 接口
- 尽可能自动解析
入门
Composer 安装
composer require mizmoz/container
基本用法
use Mizmoz\Container; $container = new Container; // Add an item to the container $container->add(MyClass::class, function () { return new MyClass('some', 'params'); }); // get the item $myClass = $container->get(MyClass::class); // Add a shared item $container->addShared(MyClass::class, function () { return new MyClass(); }, Container::SHARED); // get the shared item $container->get(MyClass::class) === $container->get(MyClass::class); // check if an item exists in the container $container->has(MyClass::class); // add an alias for easy access to items $container->addAlias(SomeLogger::class, 'log');
类解析
提供基本的构造函数解析。
// Pass the resolver in to the container $container = new Container(new Resolver); // silly example $date = $container->get(DateTime::class); // add entries to the container to have interfaces resolved $container->add(SomeInterface::class, function () { return new Some(); }); // add a raw value to the container, these are always treated as shared objects. $container->addValue('config', new Config()); // now auto resolve the class that requires the SomeInterface entry $container->get(SomeUser::class);
容器助手
使用 ManageContainerTrait
自动将容器设置在通过 get($id)
返回的类上
API
添加条目 (string $id, callable $entry, string $type = Container::EXCLUSIVE)
添加共享条目 (string $id, callable $entry)
添加独占条目 (string $id, callable $entry)
添加值 (string $id, $entry)
添加服务提供者 (Contract\ServiceProvider $serviceProvider)
添加别名 (string $id, string $alias)
mixed get(string $id)
bool has(string $id)
待办事项
- 自动解析器改进
- 添加解析基本参数的能力,例如 Resolve $secret in Connect(string $secret)
- 添加事件通知与 Mizmoz\Event