kbondurant/attribute-container

基于属性的league/container容器

1.0.0 2022-02-25 22:48 UTC

This package is auto-updated.

Last update: 2024-09-03 21:31:16 UTC


README

基于属性的league/container容器。

安装

通过Composer

composer require kbondurant/attribute-container

要求

此版本支持的PHP版本如下。

  • PHP 8.0
  • PHP 8.1

使用方法

此容器允许您使用PHP属性将接口与其实现绑定

将AttributeContainer作为代理容器添加到league/container中

<?php 

declare(strict_types=1);

$container = new League\Container\Container();
$delegate  = new Kbondurant\AttributeContainer\AttributeContainer();

$container->delegate($delegate);

常规绑定

<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Kbondurant\AttributeContainer\BindTo;

#[BindTo(Bar::class)]
interface Foo
{
    //
}

单例绑定

<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use Kbondurant\AttributeContainer\BindTo;

#[BindTo(Bar::class, true)]
interface Foo
{
    //
}

自定义绑定属性

如果您不想依赖提供的BindTo属性,可以创建自己的实现BindingAttribute接口的属性类

#[Attribute]
class MyBindingAttribute implements Kbondurant\AttributeContainer\BindingAttribute
{
    /**
     * @param class-string $class
     */
    public function __construct(
        private string $class,
    ) {
    }

    public function getClass(): string
    {
        return $this->class;
    }

    public function isShared(): bool
    {
        return false;
    }
}

#[MyBindingAttribute(Bar::class)]
interface Foo
{
    //
}

测试

测试包括PHPUnit和PHPStan(级别9)。

$ composer test

贡献

欢迎Pull请求。对于主要更改,请首先打开一个问题来讨论您想更改的内容。

请确保适当更新测试。

许可

MIT