pollen-solutions/container

花粉解决方案 - 容器组件 - PSR-11 兼容的依赖注入容器。

v1.0.7 2022-09-21 00:00 UTC

This package is auto-updated.

Last update: 2024-09-30 01:30:25 UTC


README

Latest Stable Version MIT Licensed PHP Supported Versions

花粉解决方案 容器 组件是一个PSR-11 兼容的依赖注入容器。

安装

composer require pollen-solutions/container

基本用法

use Pollen\Container\Container;

$container = new Container();

class Foo {
    public $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
}

class Bar {}

$container->add(Foo::class, function () use ($container){
    return new Foo($container->get(Bar::class));
});
$container->add(Bar::class);

$foo = $container->get(Foo::class);

var_dump($foo instanceof Foo);
var_dump($foo->bar instanceof Bar); 

服务提供者

use Pollen\Container\Container;
use Pollen\Container\ServiceProvider;

// Classes definitions
class Foo {
    public $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
}

class Bar {}

// Service Provider definition
class FooBarServiceProvider extends ServiceProvider
{
    protected $provides = [
        Foo::class,
        Bar::class
    ];

    public function register(): void
    {
        $this->getContainer()->add(Foo::class, function () {
            return new Foo($this->getContainer()->get(Bar::class));
        });

        $this->getContainer()->add(Bar::class);
    }
}

// Service Provider declaration
$container = new Container();

$container->addServiceProvider(new FooBarServiceProvider());

$foo = $container->get(Foo::class);

// Test
var_dump($foo instanceof Foo);
var_dump($foo->bar instanceof Bar);

可启动服务提供者

// Classes definitions
class Foo {
    public $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }

    public function onBoot(): void
    {
        var_dump(sprintf('%s booted through Service Provider!', __CLASS__));
    }
}

class Bar {}

// Service Provider definition
class FooBarServiceProvider extends BootableServiceProvider
{
    protected $provides = [
        Foo::class,
        Bar::class
    ];

    public function boot(): void
    {
        /** @var Foo::class $foo */
        $foo = $this->getContainer()->get(Foo::class);
        $foo->onBoot();
    }

    public function register(): void
    {
        $this->getContainer()->add(Foo::class, function () {
            return new Foo($this->getContainer()->get(Bar::class));
        });

        $this->getContainer()->add(Bar::class);
    }
}

// Service Provider declaration
$container = new Container();
$serviceProviders = [];

$container->addServiceProvider($serviceProviders[] = new FooBarServiceProvider());

// Boot all Bootable service providers
foreach ($serviceProviders as $serviceProvider) {
    if ($serviceProvider instanceof BootableServiceProviderInterface) {
        $serviceProvider->boot();
    }
}
exit;

代理容器

use Pollen\Container\Container;

$mainContainer = new Container();
$delegateContainer = new Container();

$mainContainer->delegate($delegateContainer);

class Foo {
    public $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
}

class Bar {}

$mainContainer->add(Foo::class, function () use ($mainContainer){
    return new Foo($mainContainer->get(Bar::class));
});

$delegateContainer->add(Bar::class);

$foo = $mainContainer->get(Foo::class);

var_dump($foo instanceof Foo);
var_dump($foo->bar instanceof Bar);

自动连接

use Pollen\Container\Container;

interface FooInterface {}
class Foo implements FooInterface {
    public $bar;

    public function __construct(BarInterface $bar)
    {
        $this->bar = $bar;
    }
}

interface BarInterface {}
class Bar implements BarInterface {}

$container = new Container();
$container->enableAutoWiring();

$container->add(FooInterface::class, Foo::class);
$container->add(BarInterface::class, Bar::class);

$foo = $container->get(Foo::class);

var_dump($foo instanceof Foo);
var_dump($foo->bar instanceof Bar);