yarfox/attributes-collector

此包已被废弃,不再维护。未建议替代包。

PHP8 属性收集器。

dev-master 2022-07-24 07:25 UTC

This package is auto-updated.

Last update: 2023-08-24 10:14:18 UTC


README

PHP8 属性收集器。

要求

  • php>=8

安装

composer require yarfox/attributes-collector

使用

  1. 安装
  2. 将php文件AttributeConfig.php添加到您的项目中。
class AttributeConfig implements ConfigInterface
{
    #[ArrayShape(['scanDirs' => 'array'])]
    public static function getAttributeConfigs(): array
    {
        return [
            'scanDirs' => [
                __NAMESPACE__ => __DIR__,
            ],
        ];
    }
}
  1. 添加属性和属性处理器。
// Attribute
#[Attribute(Attribute::TARGET_CLASS)]
class ClassAttribute
{
    public const TEST = 'test';

    private string $test;

    public function __construct(#[ExpectedValues(valuesFromClass: ClassAttribute::class)] string $test)
    {
        $this->test = $test;
    }

    public function getTest(): string
    {
        return $this->test;
    }
}

// AttributeHandler
#[AttributeHandler(ClassAttribute::class)]
class ClassAttributeHandler extends AbstractHandler
{
    public function handle(): void
    {
        /**
         * @var $attribute ClassAttribute
         */
        var_dump($this);
        $attribute = $this->attribute;
        var_dump($attribute->getTest());
    }
}
  1. 使用属性
#[ClassAttribute(ClassAttribute::TEST)]
class ClassAttributeTest
{

}
  1. 开始扫描。
AttributeKeeper::bootloader();
AttributeKeeper::collect();

示例

attributes-collector-example