league/construct-finder
Locate PHP code constructs (classes, interfaces, traits, enums) within one or more directories. Returns construct objects or just names, with type-specific finders and support for excluding files via simple wildcard patterns.
league/construct-finder package is a lightweight utility for reflection-based discovery of PHP constructs (classes, interfaces, traits, enums). It is ideal for:
ReflectionClass may introduce performance costs in high-traffic applications. Benchmarking is recommended for production workloads.AppServiceProvider boot).Cache facade for dynamic discovery.| Risk Area | Mitigation Strategy |
|---|---|
| Performance | Profile reflection calls; cache results aggressively if used frequently. |
| Namespace Collisions | Validate discovered classes against Laravel’s autoloader to avoid duplicates. |
| Enum Support | Test thoroughly with PHP 8.1+ enums (package supports them, but edge cases may exist). |
| Dependency Bloat | Minimal (~1MB), but justify inclusion if only used in a single feature. |
| Future Laravel Changes | Monitor Laravel’s Illuminate\Support\Manager or Illuminate\Container for built-in alternatives. |
Why not use Laravel’s built-in discovery?
Where will this be used?
register() in a service provider).Performance Trade-offs:
Maintenance Burden:
Alternatives:
Illuminate\Support\Facades\File + glob() suffice for simple cases?spatie/laravel-package-tools) that already solves this?Mockery.composer.json:
"require": {
"league/construct-finder": "^1.0"
}
App\Contracts).register() to bind discovered classes:
$finder = new League\ConstructFinder\Finder();
$classes = $finder->findClassesInNamespace('App\Services');
foreach ($classes as $class) {
$this->app->bind($class, $class);
}
bootstrap/cache/ or use Laravel’s Cache facade:
$cacheKey = 'discovered_classes';
$classes = Cache::remember($cacheKey, now()->addHours(1), function () {
return $finder->findClassesInNamespace('App\Modules');
});
Reflection must be enabled (standard).README or internal wiki.league/construct-finder). Updates are rare (low maintenance burden).\Log::debug('Discovered classes:', ['classes' => $classes]);
memory_get_usage().| Scenario | Impact | Mitigation |
|---|---|---|
| Missing Classes | Runtime errors if unbound. | Graceful fallbacks or retries. |
| Namespace Pollution | Duplicate bindings in container. | Validate against Laravel’s autoloader. |
| Reflection Blocked | Security restrictions (e.g., open_basedir). | Use allowlists or fallback to glob. |
| Cache Staleness | Outdated class lists. | Invalidate cache on composer dump-autoload. |
| PHP Version Mismatch | Enums/traits not supported. | Enforce PHP 8.1+ in CI. |
ReflectionClass limitations.composer.json.How can I help you explore Laravel packages today?