jersoncarin/container

轻量级依赖注入容器

dev-master 2021-02-25 17:34 UTC

This package is auto-updated.

Last update: 2024-08-26 01:18:11 UTC


README

为酷孩子们提供的轻量级依赖注入服务容器

特性

  • 自动装配
  • 自动构造函数注入
  • 支持闭包/可调用对象和类
  • 易于使用

安装

Composer

composer require jersoncarin/container

Git Clone

git clone https://github.com/jersoncarin/container.git

# and add manually

如何使用

// Create instance of the container
$container = new Bulk\Components\Container\Container;

// Setting to container

// Set a object to a container
// it can be a closure/callable or a class
$container->set('key',User::class);

// Set a class instance object
$user = new User
$container->instance('key',$user);

// Set a object to a container but once only
// it can be a closure/callable or a class
// Note Once a once method is make/get, 
// the same object instance will be returned on subsequent calls into the container
$container->once('key',Request::class);

// Getting/Making from container

// Service Container implements PSR-11 Container Interface
// This will throw an exception if binding is not exist or has error thrown
$instance = $container->get('key');
// Return bool
$isExists = $container->has('key');

// make method accepts a parameters, it's just like get
// but the differents is if binding is not exist to the container
// it will automatically create and register to the container
$instance = $container->make('key',['param1','param2']);

// Calling a class or a closure/callable function
// This method call the callback or method from the class
// it will accept Closure/Callable function and method (PUBLIC)
// If you calling the class/method you can use 'User@show', or array base ['User::class','show'],
// [$this,'show'],[$instance,'show'], and a closure or a string callable function
// and it's accept a parameters
$callback = $container->call([User::class,'show'],'param1','param2');

示例

// User class
class User
{
    //
}

// Direct Configuration

// If a class has no dependencies or only depends on other concrete classes/interfaces, the container does not need to be instructed on how to resolve that class.
// call() automatically resolve the class if it's exist and inject to your method/function/closure parameters

$container->call(function(User $user) {
    var_dump($user);
});

// Simple bindings
// Note you can bind with interfaces

$container->set('user',User::class);

// or using closure
$container->set('user',function() {
    return new User;
});

// and also it accept an instance object, although you can use instance()
$user = new User;
$container->set('user',$user);

// Using instance (No Di apply);
$container->instance('user',$user);

// Binding once
// Note that this function is like set()
// but have differences you can see above
$container->once('user',User::class);


// Resolving

// Using PSR-11 Compliance Container Interface
$user = $container->get('user');

// Using make method
$user = $container->make('user');
// also you may passed param arguments
$user = $container->make('user','someparam1','someparam2');

// Automatic Injection or Autowiring
// You may typehint a class and it will automatically resolved from the container

class Crush
{
    public function __construct(User $user) {
        //
    }
}

// Make it
$container->make(Crush::Class);

// Calling a callback or method from the class with DI applies
// call() method call the callback or a class method with injected parameters
// you may typehint the class and it will automatically resolved the class

class BestFriend
{
    public function __construct(Crush $crush) {
        //
    }
    
    public function sayHi(User $user) {
        //
    }
}

// Calling sayHi method from the BestFriend class
$container->call([BestFriend::class,'sayHi']);
// Also you may pass a optional parameters
$container->call([BestFriend::class,'sayHi'],'param1','param2');

// Removing bindings from the container
$container->remove('user');

扩展

服务容器提供了一个可扩展的方法,您可以通过此方法扩展容器

// You may use extend() method 
Container::extend('method_name',function() {
    // You can access container instance using $this
    $this->call(...);
});

// you can call your method like the normal method
// Note you can't use $this on static call
// also you can pass a optional parameters

// Using static method
Container::method_name(...$params)

// Using instance method
$container = new Bulk\Components\Container\Container;

$myMethod = $container->method_name(...$params);

作者

  • Jerson Carin

许可证

GPL-3.0

您可以查看许可证文件以获取更多信息