crishellco/rivet-ioc 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

crishellco/rivet-ioc

最新稳定版本:1.1.3

Composer 安装命令:

composer require crishellco/rivet-ioc

包简介

A simple auto-wiring IoC container for PHP

README 文档

README

Build Status

RivetIoc is an auto-wiring (zero configuration) IoC container for PHP to easily manage class dependencies. It features both auto-wiring of dependencies using reflection as well as manual registration for depenency injection.

Features

  • Auto-wiring (zero configuration) dependency injection
    • Recursive dependency injection
  • Manual registration for more complex dependency management
  • Locator trait which exposes commonly used RivetIoc\Ioc methods

Getting Started

Install

$ composer require crishellco/rivet-ioc

System Requirements

PHP >= 5.4.0

Documentation

Auto-wiring

Define your classes using type hints

namespace App;
class DbDriver
{
    ...
}
namespace App;
class Db
{
    protected $driver;

    public function __construct(DbDriver $driver)
    {
        $this->driver = $driver;
    }
}
namespace App\Services;
class UserService
{
    protected $db;

    public function __construct(Db $db)
    {
        $this->db = $db;
    }
}

Use RivetIoc to create a new class instance

RivetIoc will use constructor type hints to automatically create and inject dependencies

$userService = \RivetIoc\Ioc::instance()->make('App\Services\UserService');

// Or use the helper function...
$userService = rivet_make('App\Services\UserService');

Manual dependency registration

Register your dependencies in your application bootstrap

\RivetIoc\Ioc::instance()->register('App\Db', function() {
    $mysqli = new mysqli('localhost', 'username', 'password', 'mydb');
    $db = new App\Db($mysqli);

    return $db;
});

Use RivetIoc to create a new class instance

RivetIoc will use the registered closure to create and inject dependencies

$db = \RivetIoc\Ioc::instance()->make('App\Db');

// Or use the helper function...
$db = rivet_make('App\Db');

Forget a manually registered dependency

register_shutdown_function(function() {
    \RivetIoc\Ioc::instance()->forget('App\Db');
});

Locator trait

Use the RivetIoc\Traits\Locator trait in a class give access to commonly used RivetIoc\Ioc methods:

  • make
  • register
  • forget
namespace App\Services;
class UserService
{
    use \RivetIoc\Traits\Locator;

    protected $dao;

    public function __construct()
    {
        $this->dao = $this->make('App\Doa\UserDao');

        // Or use the helper function...
        $this->dao = rivet_make('App\Doa\UserDao');
    }
}

How to Contribute

Pull Requests

  1. Fork the RivetIoc repository
  2. Create a new branch for each feature or improvement
  3. Send a pull request from each feature branch to the develop branch

统计信息

  • 总下载量: 346
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-04