项目作者: anhoder

项目描述 :
PHP8 annotation collector.
高级语言: PHP
项目地址: git://github.com/anhoder/annotations-collector.git
创建时间: 2021-01-13T05:35:02Z
项目社区:https://github.com/anhoder/annotations-collector

开源协议:MIT License

下载


attributes-collector (annotations-collector)

PHP8 attribute collector.

Required

  • php>=8

Install

  1. composer require yarfox/attributes-collector

Usage

  1. Install
  2. Add php file AttributeConfig.php to your project.
  1. class AttributeConfig implements ConfigInterface
  2. {
  3. #[ArrayShape(['scanDirs' => 'array'])]
  4. public static function getAttributeConfigs(): array
  5. {
  6. return [
  7. 'scanDirs' => [
  8. __NAMESPACE__ => __DIR__,
  9. ],
  10. ];
  11. }
  12. }
  1. Add attribute and attribute handler.
  1. // Attribute
  2. #[Attribute(Attribute::TARGET_CLASS)]
  3. class ClassAttribute
  4. {
  5. public const TEST = 'test';
  6. private string $test;
  7. public function __construct(#[ExpectedValues(valuesFromClass: ClassAttribute::class)] string $test)
  8. {
  9. $this->test = $test;
  10. }
  11. public function getTest(): string
  12. {
  13. return $this->test;
  14. }
  15. }
  16. // AttributeHandler
  17. #[AttributeHandler(ClassAttribute::class)]
  18. class ClassAttributeHandler extends AbstractHandler
  19. {
  20. public function handle(): void
  21. {
  22. /**
  23. * @var $attribute ClassAttribute
  24. */
  25. var_dump($this);
  26. $attribute = $this->attribute;
  27. var_dump($attribute->getTest());
  28. }
  29. }
  1. Use Attribute
  1. #[ClassAttribute(ClassAttribute::TEST)]
  2. class ClassAttributeTest
  3. {
  4. }
  1. Start scan.
  1. AttributeKeeper::bootloader();
  2. AttributeKeeper::collect();

Example

attributes-collector-example