netsells/attribute-finder

A simple package that allows you to get all classes that have a given attribute in a specified directory

2.1.0 2021-07-14 12:52 UTC

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
}