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

Http Factory Implementations Laravel Package

psr-discovery/http-factory-implementations

Discovers a PSR-17 HTTP factory at runtime by scanning for well-known implementations and returning the first available instance. Ideal for libraries/SDKs that want PSR-17 support without hard dependencies or user configuration. PHP 8.2+.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • PSR-17 Compliance: The package aligns perfectly with Laravel’s existing PSR-17 adoption (e.g., symfony/http-foundation for Request/Response in Laravel). It abstracts implementation details, reducing coupling to specific libraries like Guzzle or Slim.
  • Dependency Injection (DI) Synergy: Laravel’s service container (PSR-11) can leverage this package to dynamically resolve PSR-17 factories without hardcoding dependencies. This complements Laravel’s HttpClient and Illuminate\Http abstractions.
  • Modularity: The package’s discovery mechanism enables optional dependencies—factories are only instantiated if their implementations exist, making it ideal for plugins or optional features (e.g., file uploads, URI handling).

Integration Feasibility

  • Low Friction: Requires zero configuration for basic use cases. Laravel’s existing PSR-17 implementations (e.g., symfony/http-foundation for Request/Response) will be auto-discovered.
  • Backward Compatibility: Laravel’s Illuminate\Http\Request/Response already implement PSR-17 interfaces, so this package can coexist without breaking existing code.
  • Testing Support: Mock implementations (e.g., psr-mock/http-factory-implementation) integrate seamlessly with Laravel’s testing tools (e.g., Mockery, PHPUnit).

Technical Risk

  • Implementation Availability: If no PSR-17 implementations are installed, the package returns null. Mitigation:
    • Fallback Strategy: Laravel can ship with a default implementation (e.g., nyholm/psr7) as a dev dependency.
    • Runtime Checks: Add a bootstrap check in config/app.php to enforce required packages.
  • Performance Overhead: Discovery is lightweight, but singleton caching (via Discover::httpRequestFactory(singleton: true)) should be used in Laravel’s service container to avoid redundant instantiation.
  • Version Conflicts: Some supported implementations (e.g., guzzlehttp/psr7) have version constraints. Laravel’s composer.json must align with the package’s supported ranges (e.g., ^2.0 for Guzzle PSR7).

Key Questions

  1. Prioritization:
    • Should Laravel prefer a specific implementation (e.g., nyholm/psr7 for performance) or rely on auto-discovery?
    • How will this interact with Laravel’s existing HttpClient (PSR-18) and Illuminate\Http layers?
  2. Testing:
    • Will this replace Laravel’s current mocking strategies (e.g., createMock(RequestFactoryInterface))?
    • Should the package be required in tests to ensure mocks take precedence?
  3. Performance:
    • Should singleton instances be globally cached in Laravel’s container (e.g., via bindIf)?
  4. Deprecation:
    • How will this interact with Laravel’s internal PSR-17 implementations (e.g., symfony/http-foundation) if they are deprecated in favor of this package?

Integration Approach

Stack Fit

  • Laravel Core: The package integrates with Laravel’s PSR-17-aware components:
    • Illuminate\Http\Request/Response (PSR-17 compatible).
    • Illuminate\Http\UploadedFile (PSR-7 UploadedFileInterface).
    • Illuminate\Support\Facades\Request (can delegate factory creation).
  • HTTP Client: Laravel’s HttpClient (PSR-18) can use this package to resolve PSR-17 factories for request/response handling.
  • Testing: Packages like laravel/pint or phpunit can leverage mock implementations for isolated testing.

Migration Path

  1. Phase 1: Opt-In Discovery
    • Add the package as a dev dependency (composer require --dev psr-discovery/http-factory-implementations).
    • Use Discover::httpRequestFactory() in new features (e.g., plugins, SDKs) to avoid hardcoding dependencies.
  2. Phase 2: Service Container Integration
    • Bind discovered factories to Laravel’s container:
      // app/Providers/AppServiceProvider.php
      public function register()
      {
          $this->app->bindIf(
              RequestFactoryInterface::class,
              fn() => Discover::httpRequestFactory() ?? new Nyholm\Psr7\Factory\Psr17Factory()
          );
      }
      
  3. Phase 3: Deprecate Hard Dependencies
    • Replace direct instantiation of GuzzleHttp\Psr7\Factory\Psr17Factory with discovered instances.
    • Update documentation to recommend the package for new PSR-17 usage.

Compatibility

  • Existing Code: No breaking changes if implementations are already installed. Fallback to null allows graceful degradation.
  • Laravel Versions:
    • Laravel 10+: PHP 8.2+ aligns with the package’s requirements.
    • Laravel 9: Requires downgrading to v1.1.1 (PHP 8.1 support).
  • Package Conflicts:
    • Guzzle PSR7: Laravel’s guzzlehttp/guzzle may pull in guzzlehttp/psr7. Ensure version constraints are compatible (e.g., ^2.0).
    • Symfony Components: symfony/http-foundation is not auto-discovered (not in the package’s list). Explicitly prefer it if needed:
      RequestFactories::prefer('symfony/http-foundation');
      

Sequencing

  1. Add Dependency:
    composer require --dev psr-discovery/http-factory-implementations
    
  2. Update composer.json:
    • Add required implementations (e.g., nyholm/psr7) as dev dependencies if none exist.
    • Example:
      "require-dev": {
          "nyholm/psr7": "^1.0",
          "psr-mock/http-factory-implementation": "^1.0"
      }
      
  3. Implement Fallback Logic:
    • Create a custom factory resolver in app/Providers to handle null returns.
  4. Test Integration:
    • Verify mock implementations are prioritized in tests.
    • Test singleton behavior in the container.

Operational Impact

Maintenance

  • Dependency Management:
    • Pros: Reduces Laravel’s direct dependencies on PSR-17 implementations.
    • Cons: Requires monitoring for new implementations (e.g., adding symfony/http-client if it gains PSR-17 support).
  • Upgrade Path:
    • New versions of the package may support additional implementations. Laravel should periodically audit supported packages (e.g., composer why-not psr-discovery/http-factory-implementations).
  • Deprecation:
    • If Laravel’s internal PSR-17 implementations are deprecated, this package provides a soft migration path.

Support

  • Debugging:
    • Null Returns: Log warnings when no implementation is found (e.g., via Discover::httpRequestFactory() returning null).
    • Implementation Conflicts: Document common conflicts (e.g., Guzzle version mismatches).
  • Community:
    • Leverage the PSR Discovery ecosystem for support (e.g., GitHub issues, PHP-FIG discussions).
    • Provide Laravel-specific examples in the package’s README (e.g., service container integration).

Scaling

  • Performance:
    • Singleton Caching: Critical for high-traffic applications. Laravel’s container should cache discovered instances:
      $this->app->singleton(RequestFactoryInterface::class, fn() => Discover::httpRequestFactory(singleton: true));
      
    • Benchmark: Compare against hardcoded factories (e.g., new GuzzleHttp\Psr7\Factory\Psr17Factory()).
  • Memory Usage:
    • Discovery is lazy-loaded, so no impact until first use. Singleton instances reduce memory overhead.

Failure Modes

Scenario Impact Mitigation Strategy
No PSR-17 implementations installed null returns, broken functionality Enforce required dev dependencies via composer.json.
Version conflicts Runtime errors (e.g., method not found) Pin compatible versions in composer.json.
Mock implementations missing Tests fail Add psr-mock/http-factory-implementation as a test dependency.
Singleton misconfiguration Memory leaks or stale instances Use singleton: true explicitly in container bindings.

Ramp-Up

  • Developer Onboarding:
    • Documentation: Add a Laravel-specific section to the package’s README (e.g., "Using with Laravel’s Service Container").
    • Examples: Show how to:
      • Resolve factories in controllers.
      • Mock factories in tests.
      • Prefer specific implementations (e.g., for performance).
  • Training:
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.
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
spatie/mailcoach-vapor