koind/dic
Composer 安装命令:
composer create-project koind/dic
包简介
A very simple and convenient lib for working with Dependency Injection Container for PHP.
README 文档
README
A very simple and convenient lib for working with Dependency Injection Container for PHP
Installation
Run the following command from you terminal:
composer require "koind/dic: ^1.0"
or add this to require section in your composer.json file:
"koind/dic": "^1.0"
then run composer update
Usage
As an example, we will use the library multi-basket, to start we install it:
composer require "koind/multi-basket: ^1.0"
Now the implementation itself:
<?php use Koind\Container; use koind\storage\SessionStorage; require __DIR__ . '/vendor/autoload.php'; // bootstrap $container = new Container(); $container->set('koind\storage\StorageInterface', function (Container $container) { return new SessionStorage('cart'); }); $container->set('koind\cost\CalculatorInterface', 'koind\cost\SimpleCost'); $container->setShared('cart', 'koind\Cart'); // implementation logic $cart = $container->get('cart'); $cart->add(5, 6, 100);
Available Methods
The following methods are available:
public function set(string $name, $value): void public function setShared(string $name, $value): void public function get(string $name): object
Example usage
Method set()
Writes an anonymous function that returns an instance of the object.
$container->set('koind\storage\StorageInterface', function (Container $container) { return new SessionStorage('cart'); }); // Get component SessionStorage by name: $container->get('koind\storage\StorageInterface');
You can also specify the interface and class that implements this interface:
$container->set('koind\cost\CalculatorInterface', 'koind\cost\SimpleCost'); // Get component SimpleCost by name: $container->get('koind\storage\CalculatorInterface');
Note: when you add a component through the set() method, each time this component is called by the get() method, a new instance of this component will be created.
Method setShared()
Writes an anonymous function that returns an instance of the object. The component is created once and placed in the buffer at each request to receive the component, it is taken from the buffer.
$container->setShared('koind\storage\StorageInterface', function (Container $container) { return new SessionStorage('cart'); }); // Get component SessionStorage by name: $container->get('koind\storage\StorageInterface');
You can also specify the interface and class that implements this interface:
$container->setShared('koind\cost\CalculatorInterface', 'koind\cost\SimpleCost'); // Get component SimpleCost by name: $container->get('koind\storage\CalculatorInterface');
Note. When you add a component through the setShared() method, each time this component is called by the get() method, only once is created, placed in the buffer, and returned. Subsequent requests for get the component are returned from the buffer.
Method get()
If the component is in the buffer returns from the buffer in the remaining cases creates.
$container->get('koind\storage\CalculatorInterface');
Tests
Run the following command from you terminal:
phpunit
koind/dic 适用场景与选型建议
koind/dic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 koind/dic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 koind/dic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-08