Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

yarfox/attributes-collector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

attributes-collector (annotations-collector)

PHP8 attribute collector.

Required

  • php>=8

Install

composer require yarfox/attributes-collector

Usage

  1. Install
  2. Add php file AttributeConfig.php to your project.
class AttributeConfig implements ConfigInterface
{
    #[ArrayShape(['scanDirs' => 'array'])]
    public static function getAttributeConfigs(): array
    {
        return [
            'scanDirs' => [
                __NAMESPACE__ => __DIR__,
            ],
        ];
    }
}
  1. Add attribute and attribute handler.
// 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. Use Attribute
#[ClassAttribute(ClassAttribute::TEST)]
class ClassAttributeTest
{

}
  1. Start scan.
AttributeKeeper::bootloader();
AttributeKeeper::collect();

Example

attributes-collector-example