rkr/php-ioc-contract
Composer 安装命令:
composer require rkr/php-ioc-contract
包简介
README 文档
README
A contract for ioc-containers
Do not use this library as long it is not 1.0.
Abstract
This project provides a documentation for the api and the behavior of three different concerns:
- InstanceContainer - a container that returns instances associated with keys.
- ObjectFactory - an abstract factory to create new object-instances from. E.g. used in factories to create new entities.
- MethodInvoker - invoke a method, function or closure. E.g. used in dispatchers to start a subprogram.
Motivation
Lets say you wan't to build a rule-based service-dispatcher (or an http-router like silex), that invoke registered closures only if a certain condition is met:
$serviceDispatcher = new ServiceDispatcher(); $serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function () { /* do something every hour */ }); $serviceDispatcher->run();
It would be fun, if I already had the domain-objects I need to work with. This would look like this:
$serviceDispatcher = new ServiceDispatcher(); $serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) { /* do something every hour */ $businessObject->doSomething(); }); $serviceDispatcher->run();
$serviceDispatcher should not be aware of a BusinessObject directly. But the ServiceDispatcher may know of an generic way to call a callable (like a closure) and resolve those parameters by a component outside of ServiceDispatcher's scope. How this is archived is not a concern of the ServiceDispatcher. It just happens somehow.
The goal could be archived with an Dependency-Injection-Container. There are different ioc-containers out there with quite different interfaces (my current favorite is PHP-DI). So we need a common interface to pass an instance around which is aware of how to instantiate our domain-objects so that we could directly use them:
$container = new Container(require 'config/di-cfg.php'); $serviceDispatcher = new ServiceDispatcher($container); $serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) { /* do something every hour */ $businessObject->doSomething(); }); $serviceDispatcher->run();
Now the run-method in the ServiceDispatcher-implementation could simply look like this:
use Ioc\MethodInvoker; class ServiceDispatcher { /** @var MethodInvoker */ private $methodInvoker; /* ... */ /** @param MethodInvoker $methodInvoker */ public function __construct(MethodInvoker $methodInvoker) { $this->methodInvoker = $methodInvoker; } /* ... */ public function run() { foreach($this->registry as $serviceName => $service) { if($service['lockUntil'] > time()) { continue; } try { $this->methodInvoker->invoke($service['fn'], array('serviceName' => $serviceName)); } finally { $service['lockUntil'] = time() + $service['timeout']; } } } }
InstanceContainer
A InstanceContainer is mostly useful when in subjection to a di-container only a single instance of an object should be used. This is slightly different to the use of a singleton-pattern since you can have multiple di-containers with different configurations that may inject different implementations for the provided interfaces. For implementation details, look at the phpdoc-blocks.
ObjectFactory
A ObjectFactory is mostly useful in common factories to create entities. For implementation details, look at the phpdoc-blocks.
MethodInvoker
Invokes a callable method, function or closure and resolve the required parameters automatically of not already
provided. For implementation details, look at the phpdoc-blocks.
rkr/php-ioc-contract 适用场景与选型建议
rkr/php-ioc-contract 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.84k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 11 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rkr/php-ioc-contract 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rkr/php-ioc-contract 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 15
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-11