anhoder / annotations-collector
1.1.2
2022-07-23 12:02 UTC
Requires
- php: >=8.0
- php-parallel-lint/php-console-color: ^1.0
- yarfox/container: ^1.0.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
PHP8 属性收集器。
所需条件
- php>=8
安装
composer require yarfox/attributes-collector
使用方法
- 安装
- 将 php 文件
AttributeConfig.php添加到您的项目中。
class AttributeConfig implements ConfigInterface { #[ArrayShape(['scanDirs' => 'array'])] public static function getAttributeConfigs(): array { return [ 'scanDirs' => [ __NAMESPACE__ => __DIR__, ], ]; } }
- 添加属性和属性处理器。
// 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()); } }
- 使用属性
#[ClassAttribute(ClassAttribute::TEST)] class ClassAttributeTest { }
- 开始扫描。
AttributeKeeper::bootloader(); AttributeKeeper::collect();