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

Discovery Laravel Package

psr-discovery/discovery

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • PSR Compliance: The package aligns perfectly with Laravel’s existing PSR standards (e.g., PSR-11 Container, PSR-3 Logging, PSR-18 HTTP Clients). Laravel’s core and ecosystem (e.g., HTTP clients like Guzzle, caches like Redis, event dispatchers like Laravel Events) already implement these interfaces, making this a natural fit.
  • Decoupling Benefit: Eliminates hard dependencies on specific implementations (e.g., GuzzleHttp\Client vs. Symfony\Panther\Client), reducing vendor lock-in and simplifying library/SDK development.
  • Lazy Initialization: Useful for optional services (e.g., logging, caching) where dependencies may not always be present in all environments (e.g., tests vs. production).
  • Laravel-Specific Considerations:
    • Service Container: Laravel’s IoC container (PSR-11) is already discovery-capable, but this package could standardize discovery for external PSR implementations (e.g., third-party HTTP clients).
    • Facade Pattern: Could complement Laravel’s facades (e.g., Log::channel()) by dynamically resolving PSR-3 loggers or PSR-6 caches without manual binding.

Integration Feasibility

  • Low Friction: Compatible with Composer and Laravel’s autoloading. No major refactoring required for existing PSR-compliant code.
  • Meta-Packages: Laravel could leverage the meta-packages (e.g., psr-discovery/http-client) to simplify dependency management for SDKs or plugins.
  • Testing: Ideal for test doubles (e.g., mock HTTP clients in unit tests) without polluting production dependencies.
  • Potential Overhead: Minimal runtime cost (discovery is lazy and cached), but adds a layer of indirection. Benchmarking may be needed for performance-critical paths (e.g., high-frequency HTTP requests).

Technical Risk

  • Discovery Order: First-match wins may lead to unexpected behavior if multiple implementations exist (e.g., two PSR-18 clients). Laravel’s container already handles this via bindings; this package could conflict if not configured carefully.
  • Singleton Assumptions: The package assumes singletons for discovered instances. Laravel’s container manages lifecycles differently (e.g., contextual bindings), which could cause issues if not aligned.
  • Error Handling: Failures (e.g., no implementations found) throw exceptions. Laravel’s error handling (e.g., Handler::render) must be extended to gracefully manage these cases.
  • Backward Compatibility: If Laravel hardcodes implementations (e.g., new GuzzleClient()), this package may not be leveraged. Requires cultural shift toward PSR interfaces.

Key Questions

  1. Use Cases:
    • Where in Laravel’s stack would this provide the most value? (e.g., HTTP clients in Illuminate\Http\Client, logging in Illuminate\Support\Facades\Log?)
    • Would it replace Laravel’s existing service resolution (e.g., app()->make()) or complement it?
  2. Conflict Resolution:
    • How would Laravel’s container prioritize bindings vs. this package’s discovery? (e.g., if both a binding and a discoverable class exist for Psr\Log\LoggerInterface).
  3. Performance:
    • What’s the impact of discovery on cold starts (e.g., in Laravel Octane or serverless environments)?
  4. Adoption Barriers:
    • How would this integrate with Laravel’s existing facades and helpers (e.g., cache(), event())?
  5. Testing:
    • Does this enable easier test stubbing for PSR interfaces (e.g., mock HTTP clients in feature tests)?

Integration Approach

Stack Fit

  • Laravel Core: Best suited for:
    • HTTP Layer: Dynamic resolution of PSR-18 clients (e.g., for Http::macro() or third-party SDKs).
    • Logging: Fallback for PSR-3 loggers when no explicit binding exists (e.g., in plugins).
    • Caching: Discover PSR-6 caches (e.g., Illuminate\Cache\Repository) without hardcoding drivers.
    • Events: Replace Event::dispatch() with PSR-14 dispatchers in modular packages.
  • Laravel Ecosystem:
    • Packages: Ideal for libraries (e.g., payment gateways, analytics) that need PSR interfaces but shouldn’t force dependencies.
    • Testing: Simplify test setups (e.g., swap GuzzleHttp\Client for a mock in unit tests).
  • Non-Fit Areas:
    • Core Services: Laravel’s critical services (e.g., database, queue) are tightly coupled and unlikely to benefit from discovery.
    • Legacy Code: Non-PSR-compliant classes won’t interact with this package.

Migration Path

  1. Phase 1: Opt-In Discovery

    • Introduce a discover() helper in Illuminate\Support\Facades\Facade to resolve PSR interfaces dynamically.
    • Example:
      use Psr\Log\LoggerInterface;
      $logger = discover(LoggerInterface::class); // Falls back to default if no binding/discoverable class exists.
      
    • Add meta-packages to composer.json for common PSR implementations (e.g., psr-discovery/http-client).
  2. Phase 2: Facade Integration

    • Modify facades (e.g., Log, Cache) to use discovery as a fallback:
      // In Illuminate\Support\Facades\Log
      public static function channel($name = null) {
          return tap(discover(LoggerInterface::class, $name), function ($logger) {
              // ...
          });
      }
      
    • Deprecate hardcoded implementations in favor of discovery where possible.
  3. Phase 3: SDK/Plugin Standard

    • Document the pattern for Laravel packages (e.g., "Use discover() for PSR dependencies").
    • Provide a laravel-discovery meta-package bundling common PSR implementations.

Compatibility

  • Laravel 10+: Fully compatible due to PSR-11 container and modern PHP (8.1+).
  • Legacy Laravel: May require shims for older PSR versions (e.g., PSR-6 cache in Laravel <8.0).
  • Third-Party Packages: Risk of conflicts if packages assume hard dependencies. Mitigate with:
    • Clear documentation on discovery vs. bindings.
    • Priority rules (e.g., bindings > discovery > defaults).

Sequencing

  1. Prototype: Build a proof-of-concept for one PSR interface (e.g., PSR-18 HTTP clients in Http::macro()).
  2. Benchmark: Measure performance impact vs. direct instantiation.
  3. Facades: Integrate with Log and Cache facades.
  4. Ecosystem: Partner with Laravel package maintainers to adopt the pattern.
  5. Deprecate: Phase out hardcoded implementations in Laravel’s core (e.g., replace new GuzzleClient() with discover()).

Operational Impact

Maintenance

  • Pros:
    • Reduced Boilerplate: No need to manually bind every PSR implementation.
    • Easier Updates: Swap implementations (e.g., Guzzle → Symfony Panther) without code changes.
    • Plugin-Friendly: Enables modular PSR services (e.g., add a new cache driver via discovery).
  • Cons:
    • Debugging Complexity: Discovery adds a layer of indirection. Tools like tinker or dd() may need enhancements to show discovery paths.
    • Configuration Drift: Developers might rely on discovery order, leading to subtle bugs (e.g., wrong HTTP client selected).
  • Tooling:
    • Extend Laravel Debugbar to show resolved PSR implementations.
    • Add php artisan discover:list to inspect discoverable classes.

Support

  • Common Issues:
    • "No implementation found" exceptions: Requires clear docs on fallback behavior (e.g., throw vs. return null).
    • Priority conflicts: Educate users on binding precedence (e.g., app->bind() overrides discovery).
  • Documentation Needs:
    • Migration guides for replacing hard dependencies.
    • Examples for testing (e.g., mocking PSR interfaces).
    • Troubleshooting discovery order (e.g., composer dump-autoload required after adding new implementations).
  • Community Adoption:
    • Highlight use cases in Laravel News/Forums.
    • Partner with package maintainers (e.g., Spatie, BeyondCode) to adopt the pattern.

Scaling

  • Performance:
    • Cold Start: Discovery adds ~1–5ms per resolved interface (negligible for most apps; benchmark in Octane).
    • Warm Start: Cached after first resolution (minimal overhead).
    • High-Volume: For critical paths (e.g., API routes), prefer bindings over discovery.
  • Horizontal Scaling:
    • No impact on stateless services (e.g., queues, jobs). Stateful services (e.g., caches) remain unchanged.
  • Database/External Services:
    • Discovery doesn’t affect underlying systems (e.g., Redis, MySQL), but mis
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
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
christhompsontldr/laravel-inky