netsells / attribute-finder
A simple package that allows you to get all classes that have a given attribute in a specified directory
Installs: 1 508
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^8.0
- symfony/finder: ^5.1
Requires (Dev)
- phpunit/phpunit: ^9.4
This package is auto-updated.
Last update: 2024-10-27 18:59:41 UTC
README
Attribute finder privides an easy way of getting a list of classes that have a specific attribute within a specified directory. Attribute finder will return a Generator
of ReflectionClass
instances for each class found that contains the given attribute.
Installation
using composer:
composer require netsells/attribute-finder
Usage
The AttributeFinder
class needs to be initialised with the path to the directory you wish to scan. You can then call the
getClassesWithAttribute()
which takes a single argument of the attribute name you're trying to seach for. getClassesWithAttribute()
will then return an Generator
instance containing a ReflectionClass
instance for each class that has the given attribute in the specified directory.
use Netsells\AttributeFinder\AttributeFinder; $finder = new AttributeFinder(__DIR__); $classes = $finder->getClassesWithAttribute(TestAttribute::class);
Once you have the Generator
instance you are free to iterate over it and retrieve the attributes
foreach ($classes as $class) { $attribute = $class->getAttributes(TestAttribute::class)[0]; $attributeInstance = $attribute->newInstance(); // Do whatever you want with the given attribute instance }