Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Class Finder Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Autodiscovery of CLI Commands: Auto-register Symfony Console commands by scanning App\Commands and looping over results to add to the console kernel.
  • Plugin/Extension Systems: Load classes implementing a common interface (e.g., EventSubscriberInterface) from a plugin namespace using getClasses() and ReflectionClass to verify conformance.
  • Dynamic Factory Setup: Build factories dynamically by scanning a namespace for factory classes and invoking static methods or instantiating them conditionally.
  • Integration with Laravel Service Providers: In 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.

Gotchas and Tips

  • Performance: Scanning directories is expensive—especially in deep or large trees. Avoid calling getClasses() on every request; always cache. Laravel developers can use cache()->rememberForever() or hook into config/cache clears.
  • Namespace vs. Directory Mapping: The package only works with PSR-0/4–compliant structures. Ensure your namespace prefix maps correctly to the base directory (e.g., App\ → app/). Passing an incorrect base path will yield empty results.
  • Interface/Abstract Filtering: The package returns all classes in the namespace—including invalid or incomplete ones. You must manually filter using is_subclass_of(), instanceof, or reflection (e.g., implements SomeInterface).
  • No Autoloading Dependency: Classes in the result list may not be autoloaded yet—calling new $class() may fail unless your autoloader (e.g., Composer’s) is invoked first. Use class_exists($class) or explicit require_once if needed.
  • Extend via 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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
shitoudev/permissionmanager
johannesclimacus/laravel-tui
hocvt/seotools
vitalybaev/sypexgeo
amphibee/themosis-ignition
gito-me/fast-excel
frankyso/laravel-ipaymu
geolize-studio/laravel-chile
boomerangui/chat
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core