rkr / attribute-provider
A lightweight PHP utility for simplifying the retrieval of attributes using reflection.
dev-main
2025-03-08 21:41 UTC
Requires
- php: >= 8.1
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: *
This package is auto-updated.
Last update: 2025-03-08 21:41:55 UTC
README
A lightweight PHP utility for simplifying the retrieval of attributes using reflection.
Installation
composer require rkr/attribute-provider
Example Usage
use Kir\Attributes\ClassAttributeProviderFactory; use Kir\Attributes\Subjects\ClassAttribute; use Kir\Attributes\Subjects\MethodAttribute; use Kir\Attributes\Subjects\PropertyAttribute; use Kir\Attributes\Subjects\TestClass1; $factory = new ClassAttributeProviderFactory(); $provider = $factory->create(TestClass1::class); $reflectionAttributes = $provider->getAllReflectionAttributes(ClassAttribute::class); foreach ($reflectionAttributes as $reflectionAttribute) { $instance = $reflectionAttribute->newInstance(); printf("Attribute %s of %s -> %s: %s%s", $instance::class, TestClass1::class, $instance->key, $instance->value, PHP_EOL); } $reflectionAttributes = $provider->properties()->getFirstAttributesFromPropertiesWithPropertyNameAsKey(PropertyAttribute::class); foreach ($reflectionAttributes as $propertyName => $instance) { printf("Attribute %s of %s -> %s: %s%s", $instance::class, $propertyName, $instance->key, $instance->value, PHP_EOL); } $reflectionAttributes = $provider->methods()->getFirstAttributesFromMethodsWithMethodNameAsKey(MethodAttribute::class); foreach ($reflectionAttributes as $propertyName => $instance) { printf("Attribute %s of %s -> %s: %s%s", $instance::class, $propertyName, $instance->key, $instance->value, PHP_EOL); }