Weave Code
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

kcs/class-finder

Discover and filter PHP classes and namespaces in your project using Composer’s autoloader with PSR resolution. Iterate found classes and reflections, then narrow results by interfaces, subclasses, annotations, PHP 8 attributes, directories, namespaces, or custom callbacks.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: The kcs/class-finder package provides utility classes for dynamic class/namespace discovery, which is valuable in Laravel for:
    • Service/Provider Registration: Auto-discovering classes (e.g., event listeners, commands, or service bindings) without manual configuration.
    • Plugin/System Extensibility: Enabling modular architectures where third-party packages or custom modules register classes dynamically.
    • Legacy Migration: Identifying unused or deprecated classes in large codebases.
  • Laravel Synergy: Complements Laravel’s service container, autoloading, and package discovery mechanisms (e.g., Illuminate\Support\ServiceProvider::boot() or Illuminate\Foundation\Application::register()).
  • Alternatives: Overlaps with Laravel’s built-in ClassFinder (e.g., Illuminate\Filesystem\Filesystem::glob()) or Composer’s autoloader, but offers higher-level abstractions (e.g., filtering by traits, interfaces, or annotations).

Integration Feasibility

  • Core Compatibility: Written in PHP 8.1+, leverages modern features (e.g., Stringable, Attribute support). Works seamlessly with Laravel 10+.
  • Dependency Lightweight: No external dependencies; MIT-licensed and stateless.
  • Testing: Minimal test coverage (31 stars but no dependents suggests niche adoption). Risk: Untested edge cases (e.g., recursive directory traversal, symlink handling).
  • Performance: Uses SplFileInfo and ReflectionClass, which are CPU-intensive for large codebases. Benchmark against Laravel’s native glob() or spatie/laravel-package-tools.

Technical Risk

Risk Area Severity Mitigation
False Positives/Negatives High Validate against Laravel’s app_path(), config_path(), etc., to avoid scanning vendor/node_modules.
Recursive Scanning Medium Limit depth or use Filesystem::allFiles() for safer traversal.
Reflection Overhead Medium Cache results (e.g., via Illuminate\Support\Facades\Cache) for repeated calls.
Annotation Parsing Low Only use if leveraging Attribute (PHP 8+) or a third-party parser like phpDocumentor.
Laravel-Specific Quirks Low Test with Laravel’s bootstrap/app.php and config/app.php overrides.

Key Questions

  1. Why not use Laravel’s native tools?

    • Does the package offer unique filters (e.g., by namespace depth, file extension, or custom metadata) not available in glob()?
    • Example: "Find all classes in App\Modules\* that implement ShouldRegister::class."
  2. Performance Trade-offs

    • How often will this run? (e.g., once at boot vs. per-request).
    • Can results be cached or precomputed during deployment?
  3. Maintenance Burden

    • Will this replace or duplicate existing discovery logic (e.g., in AppServiceProvider)?
    • How will it handle dynamic class loading (e.g., plugins loaded at runtime)?
  4. Testing Strategy

    • Are there unit tests for edge cases (e.g., circular symlinks, non-PHP files)?
    • Should integration tests cover Laravel’s service container interactions?
  5. Future-Proofing

    • Does the package support PHP 8.3+ features (e.g., never return types)?
    • Is there a roadmap for Laravel-specific enhancements (e.g., Illuminate\Contracts integration)?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Service Providers: Replace manual bind()/singleton() calls with dynamic discovery (e.g., auto-register all *Service classes in app/Modules/).
    • Artisan Commands: Discover and register commands without Console/Kernel.php edits.
    • Events/Listeners: Auto-wire listeners based on annotations or interface implementation.
    • Packages: Enable modular discovery for third-party packages (e.g., scan vendor/package/src/ for *ServiceProvider classes).
  • Complementary Tools:
    • Pair with spatie/laravel-package-tools for package bootstrapping.
    • Use with nunomaduro/collision for conflict detection during discovery.

Migration Path

Phase Action Tools/Leverage
Assessment Audit current discovery logic (e.g., AppServiceProvider, config/app.php). php artisan package:discover (Laravel)
Pilot Replace one manual registration (e.g., commands) with dynamic discovery. ClassFinder::findClasses() + Reflection
Validation Test with CI/CD (e.g., GitHub Actions) to ensure no regressions. pest or phpunit
Rollout Gradually migrate other areas (e.g., event listeners, middleware). Feature flags (Laravel Nova/Forge)
Optimization Cache results or lazy-load discovery. Illuminate\Support\Facades\Cache

Compatibility

  • Laravel Versions: Tested on Laravel 10+ (PHP 8.1+). For older versions, polyfills may be needed (e.g., Stringable).
  • Composer Autoload: Ensure composer dump-autoload is run post-installation.
  • Filesystem Permissions: Scanning requires read access to target directories (e.g., storage/, app/).
  • IDE Support: Configure PHPStorm/WebStorm to index discovered classes for autocomplete.

Sequencing

  1. Pre-Integration:

    • Add to composer.json:
      "require": {
          "alekitto/class-finder": "^1.0"
      }
      
    • Run composer install --optimize-autoloader.
  2. Discovery Implementation:

    • Example: Auto-register commands in AppServiceProvider:
      use Alekitto\ClassFinder\ClassFinder;
      
      public function boot()
      {
          $commands = ClassFinder::findClasses(
              app_path('Console/Commands'),
              ['*Command']
          )->map(fn ($class) => new $class);
      
          $this->commands($commands);
      }
      
  3. Post-Integration:

    • Add tests for discovery logic (e.g., mock ClassFinder).
    • Document new conventions (e.g., "All services must be in App\Services\*").

Operational Impact

Maintenance

  • Pros:
    • Reduces boilerplate: Eliminates manual bind() calls or config/app.php edits.
    • Centralized control: Discovery logic lives in one place (e.g., AppServiceProvider).
  • Cons:
    • Tight coupling: Changes to discovery rules may break dependent systems.
    • Debugging complexity: Harder to trace why a class wasn’t discovered (e.g., wrong namespace, permission issues).
  • Mitigations:
    • Logging: Add debug logs for discovery failures:
      ClassFinder::findClasses()->each(fn ($class) => \Log::debug("Discovered: $class"));
      
    • Validation: Use assert() or custom exceptions for critical classes.

Support

  • Common Issues:
    • "Class X not discovered": Verify namespace, file permissions, and autoloader.
    • Performance: Large codebases may cause slow boot times (mitigate with caching).
  • Documentation:
    • Add a README section in your repo explaining:
      • Discovery rules (e.g., "Only classes in App\Modules\* are scanned").
      • How to exclude files/directories.
    • Example:
      ## Class Discovery
      Classes are discovered recursively from `app/Modules/`. To exclude a directory:
      ```php
      ClassFinder::exclude('app/Modules/Deprecated');
      
      
      

Scaling

  • Performance Bottlenecks:
    • Recursive scanning: O(n) complexity for large directories (e.g., vendor/).
    • Reflection overhead: Each new ReflectionClass() is expensive.
  • Optimizations:
    • Cache results for non-changing directories (e.g., vendor/):
      $classes = Cache::remember('discovered_classes', now()->addHours(1), function () {
          return ClassFinder::findClasses(app_path('Modules'));
      });
      
    • Parallelize: Use spatie/async to scan directories concurrently.
    • Lazy loading: Load classes on-demand
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity