nezamy/di
Composer 安装命令:
composer require nezamy/di
包简介
Dependency Injection and container
README 文档
README
Dependency Injection and container
Installation via composer
composer require nezamy/di
Then load composer autoload
require __DIR__ . '/vendor/autoload.php';
Usage
Let's say we have a Book class, and we need to call getName method.
Whatever the method is static or not.
class Book { private string $name = 'First Book'; public function getName(): string { return $this->name; } } $resolver = new Just\DI\Resolver; $resolver->resolve($resolver->prepare([Book::class, 'getName'])); //Or $resolver->resolve([new Book, 'getName']); // the both returns 'First Book'
Maybe you call Book without new instance you should use prepare method like the first one.
Call method with parameter
Now we have new method with one parameter
class Book { private string $name = 'First Book'; public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = $name; } } $container = Just\DI\Container::instance(); $container->setVar('name', 'PHP'); $book = new Book(); $resolver = new Just\DI\Resolver; $resolver->resolve([$book, 'setName']); $book->getName(); // will return 'PHP'
We call th setName without the parameter, but we set name in our container above as a global variable.
The resolver will search in the container and if got a variable match the same name of the parameter then pass it, if not will pass null.
Object type parameter & singleton
$function = function(Book $book){ return $book->getName(); }; $container = Container::instance(); $resolver = new Resolver; $name = $resolver->resolve($function); //here $name returns 'First Book' because it's initial value $book = new Book(); $book->setName('Test Book'); // for define a singleton object $container->set(Book::class, $book); $resolver = new Resolver; $name = $resolver->resolve($function); $this->assertSame('Test Book', $name); //$name now is equal 'Test Book'
Call a method in a class has constructor, and the constructor needs two parameters
class User{ private string $name; private string $email; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } public function getName(): string { return $this->name; } public function getEmail(): string { return $this->email; } } $container = Just\DI\Container::instance(); $container->setVar('name', 'Mahmoud Elnezamy'); $container->setVar('email', 'mahmoud@nezamy.com'); $resolver = new Just\DI\Resolver; $name = $resolver->resolve($resolver->prepare([User::class, 'getName'])); // name here will return 'Mahmoud Elnezamy'
Magic Call
class User{ private string $name; private string $email; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } public function getName(): string { return $this->name; } } class Book { public function Auther(User $user){ return $user->getName(); } } $container = Just\DI\Container::instance(); $container->setVar('user', ['Mahmoud', 'email@domain.com']); $container->setMagicCall(User::class, function ($attr, $value){ return new User(...$value); }); $resolver = new Just\DI\Resolver; $book = $resolver->resolve( $resolver->prepare([Book::class, 'Auther']) ); //$book will return 'Mahmoud'
API
$container = \Just\DI\Container::instance(); $container->setVar('name', 'value'); $container->getVar('name'); $container->hasVar('name'); $container->importVars([ 'name' => 'name here', 'id' => '1' ]); $container->set('className', new stdClass()); //Maybe the new instance do some processing or load some configurations or connect with database. //and you won't to make the instance until the first use or call $container->set('className', function (){ return new stdClass(); }); $container->get('className'); $container->has('className'); // define some singleton objects $container->import([ Request::class => new Request(...), Response::class => new Response(...), DB::class => new DB('user', 'pass',...) ]); $container->setMagicCall('UserModel', function ($attr, $value){ if($attr == 'id'){ return new UserModel($value); } return null; }); $container->getMagicCall('UserModel'); $container->hasMagicCall('UserModel');
nezamy/di 适用场景与选型建议
nezamy/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 178 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「dependency」 「injection」 「justframework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nezamy/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nezamy/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nezamy/di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
Http Microframework for Hack
PHP Dependency Injection Container
统计信息
- 总下载量: 178
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-02