cwm/hypo

PHP依赖注入容器

1.3.0 2015-04-04 02:29 UTC

This package is auto-updated.

Last update: 2024-09-15 01:46:41 UTC


README

Hypo 是一个适用于 PHP 5.4+ 的依赖注入容器。它提供了一个简单、流畅的 API 用于配置。目前,Hypo 仅支持构造函数注入。

安装

Hypo 可以通过 Composer 容易地添加到您的项目中。

"require": {
    "cwm/hypo": "1.2.2"
},

示例

注册一个类

class Crypt {
    public function crypt($password) {
        return md5($password);
    }
}

$container = new Container();
$container->register('Crypt');

$crypt = $container->resolve('Crypt');
echo $crypt->crypt('testing');

为接口注册一个类

interface ICrypt {
    public function crypt($password);
}

class Crypt implements ICrypt {
    public function crypt($password) {
        return md5($password);
    }
}

$container = new Container();
$container
    ->register('Crypt')
    ->with('ICrypt');

$crypt = $container->resolve('ICrypt');

为多个接口注册一个类

interface ICrypt {
    public function crypt($password);
}

interface IPasswordResetter {
    public function reset($user, $password);
}

class Crypt implements ICrypt, IPasswordResetter {
    public function crypt($password) {
        return md5($password);
    }

    public function reset($user, $password) {
        $user->setPassword($this->crypt($password));
    }
}

$container = new Container();
$container
    ->register('Crypt')
    ->with(array('ICrypt', 'IPasswordResetter'));

为所有实现的接口注册一个类

interface ICrypt {
    public function crypt($password);
}

interface IPasswordResetter {
    public function reset($user, $password);
}

class Crypt implements ICrypt, IPasswordResetter {
    public function crypt($password) {
        return md5($password);
    }

    public function reset(User $user, $password) {
        $user->setPassword($this->crypt($password));
    }
}

$container = new Container();
$container
    ->register('Crypt')
    ->withImplementedInterfaces();

注册实例

先前实例化的对象可以使用 registerInstance() 方法进行注册。默认情况下,它们将用于请求实例化的类。

$container = new Container();
$container
    ->register($instanceOfCrypt)
    ->withImplementedInterfaces();

配置构造函数参数

如果参数是类或接口,则类型提示的参数将自动解析。这可以通过使用命名依赖功能来覆盖(见下一节)。另一方面,如果原始参数没有默认值,则必须指定它们。

interface ICrypt {
    public function crypt($password);
}

class BCrypt implements ICrypt {
    public function __construct($workfactor) {
        ...
    }
}

$container = new Container();
$container
    ->register('BCrypt')
    ->with('ICrypt')
    ->withParameters(array(
        'workfactor' => 10
    ));

通过闭包进行自定义构建

如果容器不适合处理对象的构建,则可以使用流畅 API 上的 constructedBy() 方法进行自定义。闭包接收类名,并期望返回该类的实例。

$container = new Container();
$container
    ->register('Crypt')
    ->with('ICrypt')
    ->constructedBy(function($className) {
        return new $className(rand());
    });

命名注册

当为相同的实现注册多个类时,可以为每个类分配一个唯一的名称。可以通过使用 resolveName() 方法来使用此名称解析类。

interface ICrypt {
    public function crypt($password);
}

class BCrypt implements ICrypt {
    public function __construct($workfactor) {
        ...
    }
}

class MD5Crypt implements ICrypt {
    ...
}

$container = new Container();
$container
    ->register('BCrypt')
    ->with('ICrypt')
    ->withParameters(array(
        'workfactor' => 10
    ))
    ->withName('bcrypt');

$container->resolveName('bcrypt');

此外,当配置参数时,也可以使用 NamedDependency 类。

class UserService {
    public function __construct(ICrypt $crypt) {
        ...
    }
}

$container
    ->register('UserService')
    ->withParameters(array(
        'crypt' => new NamedDependency('bcrypt')
    ));

配置对象生命周期

可以使用 asSingleton() 方法设置单例。还提供了 asTransient(),但它不是必须指定的,因为所有注册默认都是瞬时的。

interface IUserRepo {
    ...
}

class UserRepo implements IUserRepo {
    ...
}

$container = new Container();
$container
    ->register('UserRepo')
    ->with('IUserRepo')
    ->asSingleton();

许可证

版权(c)2013 Chase Miller

特此授予任何获得本软件及其相关文档副本(“软件”)的人免费处理该软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供的人这样做,前提是

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论此类责任是根据合同、侵权或其他方式产生的,与软件或软件的使用或其他交易有关。