haydenpierce/class-finder
Lightweight PHP utility to locate and list classes by scanning directories and Composer autoload data. Helpful for plugin discovery, reflection-based tooling, and dynamic registration tasks in frameworks like Laravel or Symfony.
Start by installing the package via Composer:
composer require haydenpierce/class-finder
Then instantiate the ClassFinder class, passing it the namespace to scan and (optionally) a base directory path. The simplest use case is discovering all classes in a namespace—e.g., for command or event handler registration:
use HaydenPierce\ClassFinder\ClassFinder;
$finder = new ClassFinder('App\Commands');
$classes = $finder->getClasses(); // Returns fully qualified class names
Check the README.md for basic usage, and review the ClassFinder constructor signature and getClasses() return types.
App\Commands and looping over results to add to the console kernel.EventSubscriberInterface) from a plugin namespace using getClasses() and ReflectionClass to verify conformance.register(), scan a custom namespace (e.g., App\Listeners) and bind listeners via Event::listen() using reflection to extract handled events.Always cache discovered classes in production (e.g., Laravel’s cache or config cache) to avoid repeated filesystem scanning.
getClasses() on every request; always cache. Laravel developers can use cache()->rememberForever() or hook into config/cache clears.App\ → app/). Passing an incorrect base path will yield empty results.is_subclass_of(), instanceof, or reflection (e.g., implements SomeInterface).new $class() may fail unless your autoloader (e.g., Composer’s) is invoked first. Use class_exists($class) or explicit require_once if needed.FilterClassFinder: For advanced filtering (e.g., only abstract classes or classes with a #[Attribute]), extend ClassFinder or use the FilterClassFinder decorator to pre-filter candidates before returning.How can I help you explore Laravel packages today?