PHP code example of swiftx / ioc

1. Go to this page and download the library: Download swiftx/ioc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swiftx / ioc example snippets


use Swiftx\Ioc\Component\Container;
$ioc = new Container();

class Demo{ }

// 创建容器
$ioc = new Container();
// 绑定服务提供者
$ioc->bindEntity('Demo',new Demo());

// 获取服务提供者
$ioc->fetch('Demo');

class Demo{ }

$server = new Demo();
$ioc->bindEntity('Demo', $server);

$server == $ioc->fetch('Demo'); // true

class Demo{ }

$factory = function(){
    return new Demo();
};

$ioc->bindFactory('Demo', $factory);

$factory() == $ioc->fetch('Demo'); // true

namespace Swiftx\Ioc\Interfaces;

/**
 * 对象生产者接口
 *
 * @author		Hismer <[email protected]>
 * @since		2015-11-08
 * @copyright	Copyright (c) 2014-2015 Swiftx Inc.
 */
interface Generator {

    /**
     * 获取是否单例
     * @param bool $value
     * @return void
     */
    public function setSingleton(bool $value);

    /**
     * 获取注入对象
     * @return mixed|null
     */
    public function fetch();

}

use Swiftx\Ioc\Component\Common\Generator;

class MyGenerator extends Generator {

    /**
     * 自定义实例创建方法
     * @return mixed
     */
    protected function produce() {
        return new Demo();
    }

}

// 创建生成器实例
$myGenerator = new MyGenerator();

// 绑定服务提供者
$ioc->bind('Demo', $myGenerator);

$factory() == $ioc->fetch('Demo'); // true

// 已定义的类
class Demo(){ }

// 自动绑定已存在的类作为服务
new Demo() == $ioc->fetch(Demo::class); // true

$ioc->registerAutoBind(function($name, $container){
    if(class_exists($name)){
        $container->bindEntity($name, new $name());
    }
    
});
$ioc->registerAutoBind(function($name, $container){

});

namespace App;
/**
 * IDemo.php文件
 * @default-implement App\Demo singleton=true
 */
interface IDemo { }

namespace App;
/**
 * Demo.php文件
 */
class Demo implements IDemo{ }

namespace App;
$ioc = new Container();
new Demo() == $ioc->fetch(IDemo::class); // true

class Demo{ }
class Demo2{ }

$ioc->bindFactory(Demo::class, new Demo());

$ioc->exists(Demo::class); // true
$ioc->exists(Demo2::class); // true
$ioc->exists('Demo3'); // false

$demo = $ioc->fetch('Demo');

class Demo1{
    public function test1(){
        return 1;
    }
}

class Demo2{
    public function test2(){
        $obj = new Demo1();
        return $obj->test1();
    }
}

$demo = new Demo2();
$demo->test2();

interface IDemo1 {
    public function test1();
}

class Demo1 implements IDemo1{
    public function test1(){
        return 1;
    }
}

class Demo2{

    protected $demo1;
    
    public function setDemo1(IDemo1 $obj){
        $this->demo1 = $obj;
    }
    
    public function test2(){
        return $this->demo1->test1();
    }
}

// 创建对象
$demo = new Demo2();

// 创建依赖
$demo1 = new Demo1();

// 组装对象
$demo->setDemo1($demo1);

$demo->test2();

interface IDemo1 {
    public function test1();
}

interface IDemo2 {
    public function test2();
}

interface IDemo3 {
    public function test3();
}

class Demo1 implements IDemo1{
    public function test1(){
        return 1;
    }
}

class Demo2 implements Demo2{

    protected $demo1;
    
    public function setDemo1(IDemo1 $obj){
        $this->demo1 = $obj;
    }
    
    public function test2(){
        return $this->demo1->test1();
    }
}

class Demo3 implements Demo3{

    protected $demo2;
    
    public function setDemo2(IDemo2 $obj){
        $this->demo2 = $obj;
    }
    
    public function test3(){
        return $this->demo2->test2();
    }
}

// 创建容器
$ioc = new Container();

// 注册Demo1为服务提供者
$ioc->bindFactory(IDemo1::class, function(){
    return new Demo1();
});

// 注册Demo2为服务提供者
$ioc->bindFactory(IDemo2::class, function() use ($ioc){
    $demo = new Demo2();
    $demo->setDemo1($ioc->fetch(IDemo1::class));
    return $demo;
});

// 注册Demo3为服务提供者
$ioc->bindFactory(IDemo2::class, function() use ($ioc){
    $demo = new Demo3();
    $demo->setDemo2($ioc->fetch(IDemo2::class));
    return $demo;
});

// 调用服务
$demo = $ioc->fetch(IDemo3::class)
$demo->test3();

namespace App;
/**
 * IDemo1.php文件
 * @default-implement App\Demo1 singleton=true
 */
interface IDemo1 {
    public function test1();
}

namespace App;
/**
 * Demo1.php文件
 */
class Demo1 implements IDemo1{
    public function test1(){
        return 1;
    }
}

namespace App;
/**
 * IDemo2.php文件
 * @default-implement App\Demo2 singleton=true
 */
abstract IDemo2 {

    /**
     * 注入Demo1
     * @var IDemo1
     * @auto-injection
     */
    protected $demo1;
    
    abstract public function test2();
    
}

namespace App;
/**
 * Demo2.php文件
 */
class Demo2 extends IDemo2{

    public function test2(){
        return $this->demo1->test1();
    }
    
}

namespace App;
/**
 * IDemo3.php文件
 * @default-implement App\Demo2 singleton=true
 */
interface IDemo3 {

    /**
     * 通过Setter方法注入
     * @param IDemo2 $value
     * @auto-injection
     */
    public function setDemo2(IDemo2 $obj);

    public function test3();
    
}

namespace App;
/**
 * Demo3.php文件
 */
class Demo3 implements Demo3{

    protected $demo2;
    
    public function setDemo2(IDemo2 $obj){
        $this->demo2 = $obj;
    }
    
    public function test3(){
        return $this->demo2->test2();
    }
}

namespace App;

// 创建容器
$ioc = new Container();

// 调用服务
$demo = $ioc->fetch(IDemo3::class)
$demo->test3();
json
"php": ">=7.1",
    "swiftx/ioc": "1.*"
}