kbondurant/self-provider-container 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kbondurant/self-provider-container

最新稳定版本:1.0.1

Composer 安装命令:

composer require kbondurant/self-provider-container

包简介

Declare your definitions from within your service itself

README 文档

README

A delegate container for league/container.

Declare your definitions directly from your service just before you retrieve it for the first time

Installation

Via Composer

composer require kbondurant/self-provider-container

Requirements

The following versions of PHP are supported by this version.

  • PHP 8.0
  • PHP 8.1

Usage

You should add the SelfProviderContainer before the ReflectionContainer if you use it too.

use Kbondurant\SelfProviderContainer\SelfProviderContainer;
use League\Container\Container;
use League\Container\ReflectionContainer;

$container = new Container();

$container->delegate(new SelfProviderContainer());
$container->delegate(new ReflectionContainer());

Now you can implement the ServiceProvider interface (or any interface that extends it) and add your definitions in the register method. When you will try to retrieve your LeagueRouter from the container for the first time it will first call the register method before even trying to instantiate it.

class LeagueRouter implements ServiceProvider
{
    public function __construct(
        private Router $router,
    ) {
    }

    /**
     * @param \League\Container\DefinitionContainerInterface $container
     * @return void
     */
    public static function register(mixed $container): void
    {
        $container->add(Router::class, fn () => new Router())
            ->setShared(true);
    }
}

By default, an instance of League\Container\DefinitionContainerInterface will be passed to the register::method, but you have the possibility to change that by passing your own container instance to the SelfProviderContainer

use Acme\Container\MyOwnContainer;
use Kbondurant\SelfProviderContainer\SelfProviderContainer;
use League\Container\Container;
use League\Container\ReflectionContainer;

$container = new Container();

$container->delegate(new SelfProviderContainer(new MyOwnContainer()));
$container->delegate(new ReflectionContainer());

An instance of Acme\Container\MyOwnContainer will now be passed to the register method

class LeagueRouter implements ServiceProvider
{
    public function __construct(
        private Router $router,
    ) {
    }

    /**
     * @param \Acme\Container\MyOwnContainer $container
     * @return void
     */
    public static function register(mixed $container): void
    {
        $container->singleton(Router::class, fn () => new Router());
    }
}

Limitations

You should use this pattern only if the service(s) that you are about to register is injected in one class only (adapters, etc...), otherwise you might redeclare the same service many times which might give you slow performances or even bugs with shared definitions

统计信息

  • 总下载量: 16.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-27