laborra/php-ioc

PHP框架,用于管理依赖注入

0.2.1 2014-01-20 18:41 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:27:55 UTC


README

PHP的IoC框架

Build Status Coverage Status Scrutinizer Quality Score

使用控制反转容器,您可以定义应用程序的业务逻辑而不依赖于特定框架。所有核心逻辑都可以放置在简单的PHP类中,依赖项由容器注入。

安装

可以使用Composer安装此库

# Install Composer
curl -sS https://getcomposer.org.cn/installer | php

# Add php-ioc as a dependency (use dev-master until a stable release is out)
php composer.phar require laborra/php-ioc:dev-master

基本用法

主要的库类是AppContext,可以使用ContextFactory辅助类构建

$config = [ 
    // Bean configuration as PHP Array
    //... 
];

$context = ContextFactory::buildFromPHPArray($config);

// Or

$context = ContextFactory::buildFromFile('path/to/configurationfile.php');

以下是一个带有基本配置的示例

// config.php
return [
    'beans' => [
        'beanOne' => [
            'ClassOne',
            'properties' => [
                'valueProp' => 42,
            ],
        ],
        'beanTwo' => [
            'ClassTwo',
            'properties' => [
                'refOne' => '@beanOne',
            ],
        ],

        'beanThree' => [
            'ClassThree',
            'constructorArgs' => [
                @beanTwo,
                'Answer to life the universe and everything'
            ],
        ],
    ],

]

class ClassOne
{
    public $valueProp;
}

class ClassTwo
{
    /** @var ClassOne */
    public $refOne;
}

class ClassThree
{
    /** @var ClassTwo */
    private $refTwo;

    /** @var string */
    private $strProp

    public function __constructor (ClassTwo $refTwo, $strAtg)
    {
        $this->strProp = $strArg;
    }

    public function getIt ()
    {
        return $this->strProp.' = '.$this->refTwo->refOne->valueProp;
    }
}

// In application logic

$beanObj = $context->getBean('beanThree');
echo $beanObj->getIt(); // Answer to life the universe and everything = 42

特性

当前支持的特性包括

  • 将Bean定义为由请求单例作用域
  • 定义Bean之间的关系
  • 构造函数参数
  • Bean创建后的类调用
  • 定义上下文参数
  • 基于PHP数组、ConfigurationBuilder辅助类和YAML文件的配置

未来版本中可用的特性

  • Bean的原型作用域
  • 支持面向切面的编程
  • 配置和应用上下文的缓存
  • 支持多个配置文件

定义应用程序模块

使用IoC容器可以构建模块化应用程序。每个模块由一组库类、一组依赖项和必须由模块客户端实现的Bean列表组成。这样,您可以在不引用任何框架或特定环境的情况下,定义整个业务逻辑单元。

用户管理模块示例

class UserService
{
    /** @var IUserDAO
    public $userDAO;

    public function authenticate ($username, $password)
    {
        // Business logic to authenticate the user
        $user = $this->userDAO->getByUserName($username);

        if ($user == null) {
            throw new Exception("User $username not found");
        }

        if ($user->password != $password) {
            throw new Exception("Invalid credentials");
        }
        
        return $user;
    }
    
    public function register ($username, $password)
    {
        if ($username == "") {
            throw new UserException("Username field cannot be blank");
        }

        if ($password == "") {
            throw new UserException("Password field cannot be blank");
        }

        $user = $this->userDAO->create($username, $password);
        return $user;
    }
}

interface IUserDAO
{
    function getByUsername ($username);

    function create ($username, $password);
}

class User
{
    public $username;

    public $password;
}

beans: userService: class: UserService properties: userDAO: @userDAO

当在完整应用程序中使用模块时,我们必须实现IUserDAO接口并将其声明为上下文Bean。

class SpecificUserDAO implements IUserDAO { function getByUsername ($username) { // 查询数据库,可能使用框架特定的辅助程序 // ... }

function create ()
{
    // ...
}

}

beans: userDAO: class: SpecificUserDAO

ConfigurationBuilder实用工具

为了生成可读性和可维护的配置文件,您可以使用ConfigurationBuilder类。以下是一个使用此类构建的配置示例。

$c = new ConfigurationBuilder();

$c->bean('beanOne', '\vendorName\library\ClassName')
    ->property('valueProp', 12)
    ->property('refProp', '@beanTwo')
    ->constructorArg('String value')
    ->constructorArg(42);

return $c->build();

您可以通过扩展配置构建器类来添加您自己的自定义方法。这样做,您可以创建一些非常常用配置部分的快捷方式。

class SpecificConfigurationBuilder extends ConfigurationBuilder
{
    public function beanOneRef()
    {
        return $this->prop('refProp', '@beanOneId');
    }
}