huge / ioc
框架 IoC 简单易用且高效。HugeIoC 允许您非常简单地管理和注入 PHP 对象。
v2.2.0
2014-09-03 11:11 UTC
Requires
- php: >=5.3.3
- doctrine/annotations: ~1.2.0
- doctrine/cache: ~1.3.0
- psr/log: 1.0.0
Requires (Dev)
- apache/log4php: ~2.3.0
- phpunit/phpunit: ~4.1.4
README
框架 IoC 简单且高效,适用于 php5。该库的基本原则是代替您管理 PHP 对象的实例。这样,您就不必在构造函数中自行管理参数实例。可以通过注解 @Autowired 注入。
安装
- 使用 composer 安装
{
"require": {
"huge/ioc": "..."
}
}
$loader = require(__DIR__.'/../../../vendor/autoload.php'); // nécessaire charger les annotations \Huge\IoC\Container\SuperIoC::registerLoader(array($loader, 'loadClass'));
## 功能
- 定义一个 Bean:@Component
- 管理多个对象容器
- 通过注解 @Autowired("ID_BEAN") 注入实例
- 通过注解 @Autowired("INTERFACE") 注入实现
- 通过注解 @Autowired("CLASSE_PARENTE") 注入子类
- 支持请求或懒加载(按需)的实例化
- 重载 IFactory 进行实例化
- 可以创建特定容器(即 SuperIoC)
- 缓存:基于 doctrine cache
- 注解基于 doctrine annotations
容器
- 扩展 \Huge\IoC\Container\SuperIoC
namespace MyApp; class GarageIoC extends \Huge\IoC\Container\SuperIoC{ public function __construct($config) { parent::__construct(__CLASS__, '1.0'); $memcache = new Memcache(); $memcache->connect($config['memcache.host'], $config['memcache.port']); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); $this->setCacheImpl($cache); $this->addDefinitions(array( array( 'class' => 'Huge\IoC\Fixtures\Contact', 'factory' => new \Huge\Io\Factory\ConstructFactory(array('DUPUIT', 'Pierre')) ) )); $this->addOtherContainers(array( new AudiIoC(), new RenaultIoC() )); } }
- 注意,在重新发布时需要更新版本(刷新缓存)
工厂
- 创建您的工厂:实现 Huge\IoC\Factory\IFactory
namespace MyApp; class MyNullFactoy implements \Huge\IoC\Factory\IFactory{ public function __construct() {} public function create($classname) { return null; } }
$c = new DefaultIoC(); $c->addDefinitions(array( array('class' => 'MyClass', 'factory' => new MyNullFactory()) ));
注入实例
在您的 Bean 中注入其他 Bean
use Huge\IoC\Annotations\Component; use Huge\IoC\Annotations\Autowired; /** * @Component */ class MyController{ /** * @Autowired("MyApp\MyCustomIoC") */ private $ioc; /** * @Autowired("Huge\IoC\Fixtures\Contact"); */ private $daoContact; /** * @Autowired("Huge\IoC\Factory\ILogFactory") * @var \Huge\IoC\Factory\ILogFactory */ private $loggerFactory; /** * Nécessaire au conteneur pour setter la valeur */ public function setIoc($ioc){ $this->ioc = $ioc; } public function setDaoContact($contact){ $this->daoContact = $contact; } public function getLoggerFactory() { return $this->loggerFactory; } public function setLoggerFactory(\Huge\IoC\Factory\ILogFactory $loggerFactory) { $this->loggerFactory = $loggerFactory; } }
## 限制
- Doctrine 缓存
- Doctrine 注解
- 基于 Psr\Log 接口的 Logger
缓存
使用 Doctrine\Common\Cache\Cache 的实现
$c = new DefaultIoC('default', '1.0'); $c->setCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
注意,Bean 的定义被缓存,因此,RUN 期间提供的参数也被缓存。
Logger
- 实现组件工厂:Huge\IoC\Factory\ILogFactory
$ioc = new DefaultIoC(); $ioc->setLogger(new MyApp\Log4phpLoggerImpl('DefaultIoC')); $ioc->addDefinitions(array( array( 'class' => 'MyApp\Log4phpFactoryImpl', 'factory' => SimpleFactory::getInstance() // retourne un singleton (optimisation) ) ));