spiral/attributes
Spiral Attributes is a lightweight PHP 8 attributes toolkit for reading, filtering, and working with attributes via reflection. Used by the Spiral framework, it provides a small, focused API for attribute discovery and metadata handling.
Start by requiring the package via Composer:
composer require spiral/attributes
Then, use the Reader class to scan attributes on classes, methods, properties, or parameters. The core pattern is simple:
use Spiral\Attributes\Reader;
$reader = new Reader();
$reflection = new ReflectionClass(MyClass::class);
$attributes = $reader->getClassAttributes($reflection);
For first use, inspect a simple class with an attribute (e.g., #[Route('/api')]) to verify attribute discovery works—check that getClassAttributes() returns an array of AttributeMetadata instances. This is the foundation for most integrations.
Reader in service providers or middleware to scan controllers, jobs, or repositories for custom attributes (e.g., #[Cacheable], #[Permission]), then trigger logic based on metadata.$reader->getAttribute($reflection, Attribute::class, $container)—this resolves constructor parameters via a PSR-11 container.src/Actions/ directory) with reader->iterateClassAttributes() for batch configuration or route building.filterByTarget() or filterByAttributeClass() to handle #[ValidationRule(...)] on properties differently than on parameters.symfony/cache) by serializing AttributeMetadata objects—Reader supports __serialize()/__unserialize() out of the box.#[AllowDynamicProperties] won’t be captured on older versions).#[Validate(...)] #[Validate(...)]), use reader->getAllAttributes() instead of getAttribute() to avoid overwrites.#[Target(TARGET_CLASS)])—mismatches silently return empty results. Use Attribute::getTargets() to debug.getAttribute(), ensure all constructor parameters of the attribute class are resolvable; otherwise, throw a ContainerExceptionInterface.Reader behavior by extending it and injecting custom ResolverInterface implementations for advanced constructor resolution (e.g., non-service parameters or computed defaults).reader->getClassAttributes($reflection, true) with the second parameter true to include inherited attributes.How can I help you explore Laravel packages today?