定制 terrydjony/ion 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

terrydjony/ion

Composer 安装命令:

composer require terrydjony/ion

包简介

Ion is a minimalist inversion of control container using automatic dependency injection

README 文档

README

A minimalist inversion of control container using automatic dependency injection

Installation

To install ION Container, you need to add it to your composer.json file

{
      "require": "terrydjony/ion"
}

Or, by using terminal

composer require terrydjony/ion

Usage

First, you need to instantiate the container

use Ion\Container;

$ion = new Container();

Instantiating Object

To instantiate any objects, you should use make($class, array $args = array()), instead of new keyword

Notice that the parameter two $args is optional For example, if you want to instantiate stdClass class.

$stdClass = $ion->make('stdClass');

// $stdClass is an instance of stdClass

Registering Service

You can register the way to instantiate some class via register($key, $closure) Here is an example of registering PDO class with some defined arguments

$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
};

$pdo = $ion->make('PDO');

// $pdo is an instance of PDO class with the arguments as defined before

The above example will register PDO service. So, if you instantiate it via $ion->make('PDO'), you don't need to supply any arguments since all the arguments have been defined before.

However, it will be nice to have a database config.

$config = array('dsn'=>'mysql:host=localhost;dbname=databasename','username'=>'root','password'=>'');

$ion->register('PDO', function() use ($config) {
    return new PDO($config['dsn'],$config['username'],$username['password']);
});

$pdo = $ion->make('PDO');
And, notice that the first parameter of register is a key. The key can be anything, but it's recommended to use the class name as the key.

$ion->register('DAL', function() {
    return new PDO($config['dsn'],$config['username'],$config['password']);
};

$dal = $ion->make('DAL');
// $dal is a PDO object

Notice that if you register a service, it'll be a shared instance. If you want to register a factory, use registerFactory

Registering Factory Services

You can register a factory using registerFactory($key, $closure)

$ion->registerFactory('CarFactory', function() {
    return new Car();
};

$car1 = $ion->make('CarFactory');
$car2 = $ion->make('CarFactory');

// $car1 and $car2 are different objects
If you want to use the supplied arguments, you can use getArgs() method.

$this->ion->registerFactory('User', function($ion) {
  return new User($ion->getArgs());
});

$user1 = $this->ion->make('User',array('Foo'));
// $user1 is an instance with argument Foo

Registering Parameters

You can also register parameters using setParam($param, $value) and get the value via param($param_name)

$ion->setParam('db_dsn','mysql:host=localhost;dbname=databasename');
$ion->setParam('db_user','root');
$ion->setParam('db_password','');

$ion->register('PDO', function($ion) {
    return new PDO($ion->param('db_dsn'), $ion->param('db_user'), $ion->param('db_pass');
}

Forcing New Instance

You can force a new instance using makeNew($key, $args=array()).

// register a PDO service before

$pdo1 = $ion->makeNew('PDO');
$pdo2 = $ion->makeNew('PDO');

// $pdo1 and $pdo2 are different objects no matter how the PDO service is registered 

Defining Default Class For Interface

You can bind an interface to a class. So, if a class depends on that interface, that class will represent that interface

If the class must be instantiated with some arguments, you should register it first. Then, you give the key as the parameter two of bindInterface

But, if the class can be instantiated with no arguments, you can just specify the class name.

class A
{
    public function __construct(DatabaseAdapterInterface $dba, StandardClassInterface $stdClass, StandardInterface $obj)
    {

    }
}

$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
}

// Binds DatabaseAdapterInterface to PDO class which has been registered
$ion->bindInterface('DatabaseAdapterInterface', 'PDO');

// Binds StandardClassInterface to stdClass which needs no arguments for construction
$ion->bindInterface('StandardClassInterface', 'stdClass');

$a = $ion->make('A');

Automatic Dependency Injection

Suppose you want to instantiate a class A that needs class B which needs class C

I mean something like

class A
{
  public function __construct(B $b)
  {

  }
}

class B
{
  public function __construct(C $c)
  {

  }
}

class C
{
  public function __construct()
  {
    
  }
}

And, to make a class A instance, you just need to:

$a = $ion->make('A');

And, voila! You get the class A instance!

If you have a dependency that needs some parameters, you can register the dependency first before making object that needs that dependency

And, if the class needs some arguments, you can supply an array of arguments as parameter two of make or makeNew method.

class A
{
    public function __construct(B $b, $name)
    {
        echo "Hello ".$name
    }
}

class B
{
    public function __construct(DatabaseAdapterInterface $dba)
    {

    }
}


$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
});
$ion->bindInterface('DatabaseAdapterInterface','PDO');

$a = $ion->make('A',array('World'));

The class A instance is made automatically.

terrydjony/ion 适用场景与选型建议

terrydjony/ion 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dependency injection」 「container」 「di」 「ioc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 terrydjony/ion 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 terrydjony/ion 我们能提供哪些服务?
定制开发 / 二次开发

基于 terrydjony/ion 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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