ahjdev / class-finder
Find all classes from composer
1.0.1
2024-04-25 11:44 UTC
Requires
- php-64bit: >=8.1
Requires (Dev)
README
Find all classes from composer
Table of Contents
Installation
Install the package using composer:
composer require ahjdev/class-finder
Usage
- Create an Instance of
ClassFinder
(if you need to set path ofvendor
folder do it in__construct
)
<?php use AhjDev\ClassFinder\ClassFinder; $finder = new ClassFinder(); $finder = new ClassFinder('dir/to/vendor');
Getting Classes
- You can use
getClasses
method to get classes from namespace:
<?php use AhjDev\ClassFinder\ClassFinder; $finder = new ClassFinder; $finder->getClasses('AhjDev');
Filtering
- Filter result with FindType enum wich classes should return :
<?php namespace AhjDev\ClassFinder; enum FindType { const INTERFACE = 1; const TRAIT = 2; const ENUM = 4; const FINAL = 8; const ABSTRACT = 16; const SIMPLE_CLASS = 32; const CLASSES = self::FINAL | self::ABSTRACT | self::SIMPLE_CLASS; const ALL = self::INTERFACE | self::TRAIT | self::ENUM | self::CLASSES; }
<?php use AhjDev\ClassFinder\FindType; use AhjDev\ClassFinder\ClassFinder; $finder = new ClassFinder; $finder->getClasses( 'AhjDev', FindType::TRAIT | FindType::INTERFACE );
Using Callbacks
<?php use RefelctionClass; use AhjDev\ClassFinder\FindType; use AhjDev\ClassFinder\ClassFinder; $finder = new ClassFinder; $finder->getClasses( 'AhjDev', FindType::TRAIT | FindType::INTERFACE, fn (RefelctionClass $v) => $v->isInteranl() );
Easy methods
There is some easy methods that can use :
<?php use AhjDev\ClassFinder\FindType; use AhjDev\ClassFinder\ClassFinder; $finder = new ClassFinder; // Whether class is readonly $finder->isReadonly('foo', FindType::ALL); // Whether class use this trait $finder->hasTraitClass('trait', 'foo', FindType::ABSTRACT); // Whether class implements an interface $finder->implementsClass('interface', 'foo', FindType::ENUM); // Whether class is subclass of this class $finder->isSubClassOf('parent', 'namespace', FindType::FINAL); // Whether class is of this class or has this class as one of its parents $finder->isAOf('class', 'namespace', FindType::FINAL); // Just like `FindType` filters $finder->isInterface(string $namespace); $finder->isEnum(string $namespace); $finder->isTrait(string $namespace); $finder->isAbstract(string $namespace); $finder->isFinalClasses(string $namespace);