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

Utils Laravel Package

middlewares/utils

Common utilities for Middlewares PSR packages: auto-discovered PSR-7/PSR-17 Factory (Diactoros, Guzzle, Slim, Nyholm, Sunrise) plus PSR-15 Dispatcher for testing, callable handler helpers, and HTTP error exceptions.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • PSR-15 Middleware Ecosystem: The package is purpose-built for Laravel/PHP applications leveraging PSR-15 middleware (e.g., via psr/http-server-middleware). It aligns with Laravel’s middleware stack (e.g., Illuminate\Pipeline) but provides low-level utilities for PSR-7/PSR-17 implementations, which can complement or replace Laravel’s built-in factories (e.g., Symfony\Component\HttpFoundation).
  • Decoupled Design: The Factory class abstracts PSR-17 implementations (Diactoros, Guzzle, Nyholm, Slim, Sunrise), enabling vendor-agnostic HTTP message creation. This reduces coupling to Laravel’s default stack (e.g., Symfony’s HttpFoundation).
  • Testing & Mocking: The Dispatcher and CallableHandler simplify middleware testing by providing standalone execution without requiring a full Laravel container or HTTP server.

Integration Feasibility

  • Laravel Compatibility:
    • Pros:
      • Laravel’s middleware stack (Illuminate\Pipeline) is PSR-15 compatible, so this package can integrate as a drop-in utility for PSR-7/PSR-17 operations.
      • The Factory can replace Laravel’s default factories (e.g., Symfony\Component\HttpFoundation) for consistent PSR-7 message creation across the app.
      • CallableHandler can simplify dynamic middleware resolution (e.g., for route-based middleware).
    • Cons:
      • Laravel’s HttpFoundation is not a PSR-17 implementation, so mixing this package with Laravel’s native HTTP layer may require explicit factory configuration (e.g., via Factory::setFactory()).
      • Some Laravel-specific features (e.g., Request facade) may not translate directly to PSR-7.
  • Non-Laravel PHP Apps: Ideal for micro-frameworks (e.g., Slim, Lumen) or PSR-15-only applications where PSR-7/PSR-17 is the sole HTTP layer.

Technical Risk

  • Breaking Changes:
    • PHP 8.1+ Requirement: Laravel 9+ supports PHP 8.1+, so this is non-blocking. Older Laravel versions (e.g., 8.x) would need PHP upgrades.
    • PSR-17 Factory Changes: Laravel’s Symfony\Component\HttpFoundation is not a PSR-17 factory, so migration effort is required to adopt this package for PSR-7 message creation.
  • Dependency Conflicts:
    • Potential conflicts with Laravel’s symfony/http-foundation or nyholm/psr7 if both are used. The FactoryDiscovery class prioritizes libraries in a configurable order, but explicit factory registration may be needed.
  • Testing Overhead:
    • The Dispatcher is useful for unit testing middleware, but integrating it into Laravel’s Pipeline would require custom adapters (e.g., wrapping Laravel’s middleware in PSR-15 interfaces).

Key Questions

  1. Use Case Clarity:
    • Is this package being adopted to replace Laravel’s HTTP layer (risky) or supplement it (e.g., for PSR-7 testing/mocking)?
    • Will it be used in new projects (lower risk) or legacy Laravel apps (higher migration cost)?
  2. Factory Strategy:
    • Should the Factory default to Laravel’s Symfony\Component\HttpFoundation or a PSR-17 implementation (e.g., Diactoros)?
    • How will uploaded files (PSR-7 UploadedFileInterface) be handled, given Laravel’s Illuminate\Http\UploadedFile is not PSR-7 compliant?
  3. Middleware Integration:
    • Will Laravel’s middleware (e.g., Authenticate, Validate) be rewrapped as PSR-15 middleware, or will this package only handle new middleware?
  4. Performance Impact:
    • Does the FactoryDiscovery overhead justify the flexibility, or should a static PSR-17 factory (e.g., Diactoros) be hardcoded?
  5. Error Handling:
    • How will HttpErrorException integrate with Laravel’s exception handling (e.g., App\Exceptions\Handler)?

Integration Approach

Stack Fit

  • Primary Fit:
    • PSR-15 Middleware: The package is a natural fit for Laravel applications using PSR-15 middleware (e.g., via psr/http-server-middleware or third-party packages like middlewares/auth).
    • PSR-7/PSR-17 Utilities: Replaces or augments Laravel’s Symfony\Component\HttpFoundation for consistent PSR-7 message creation (e.g., in APIs, CLI commands, or testing).
  • Secondary Fit:
    • Testing: The Dispatcher and CallableHandler simplify middleware unit testing by providing standalone execution.
    • Dynamic Middleware: CallableHandler enables runtime middleware resolution (e.g., for route-based middleware).
  • Non-Fit:
    • Traditional Laravel Controllers: The package is not designed for Laravel’s Route::get()/Route::post() workflows, which rely on Illuminate\Http\Request/Response.
    • Blade/Templating: No direct integration with Laravel’s view layer.

Migration Path

Component Current Laravel Post-Integration Migration Steps
HTTP Message Creation Symfony\Component\HttpFoundation middlewares/utils/Factory (PSR-17) Replace new Request(), Response::create() with Factory::createRequest().
Middleware Testing Manual Pipeline setup Dispatcher::run() Replace custom test setups with Dispatcher.
Dynamic Middleware Closure-based (limited) CallableHandler Replace Route::middleware(fn() => ...) with CallableHandler-wrapped middleware.
Error Handling App\Exceptions\Handler HttpErrorException Extend Handler to catch HttpErrorException and convert to Laravel responses.
Uploaded Files Illuminate\Http\UploadedFile PSR-7 UploadedFileInterface Use Factory::createUploadedFile() or wrap UploadedFile in a PSR-7 adapter.

Compatibility

  • PSR Standards:
    • PSR-15: Full compatibility (middleware interfaces).
    • PSR-7: Full compatibility (HTTP messages).
    • PSR-17: Full compatibility (factories).
    • PSR-11: Partial (via RequestHandlerContainer for dependency injection).
  • Laravel-Specific:
    • No: Does not integrate with Laravel’s Request/Response facades, Route system, or Illuminate\Pipeline.
    • Workaround: Use adapters (e.g., convert Illuminate\Http\Request to PSR-7 before passing to middleware).
  • Dependency Conflicts:
    • Avoid mixing with symfony/http-foundation and nyholm/psr7 unless using FactoryDiscovery to prioritize one.

Sequencing

  1. Phase 1: Testing & Utilities

    • Adopt Factory for PSR-7 message creation in:
      • API tests (replace create_mock_request()).
      • CLI commands (e.g., artisan commands needing HTTP messages).
      • Third-party middleware packages.
    • Replace custom Dispatcher implementations with middlewares/utils/Dispatcher.
  2. Phase 2: Middleware Layer

    • Introduce CallableHandler for dynamic middleware (e.g., route-based middleware).
    • Rewrite new middleware to use PSR-15 interfaces (compatible with this package).
    • For existing middleware, create PSR-15 adapters (e.g., wrap Illuminate\Pipeline middleware).
  3. Phase 3: Error Handling

    • Extend App\Exceptions\Handler to convert HttpErrorException to Laravel responses.
    • Replace custom error middleware with HttpErrorException-based implementations.
  4. Phase 4: Full PSR-7 Adoption (Optional)

    • Replace Symfony\Component\HttpFoundation with this package’s Factory for all HTTP message creation.
    • Migrate uploaded files to PSR-7 UploadedFileInterface.

Operational Impact

Maintenance

  • Pros:
    • MIT License: No legal restrictions.
    • Active Development: Regular updates (last release: 2025-01-23) with PHP 8.4 support.
    • Minimal Boilerplate: Factory and Dispatcher reduce custom utility code.
  • Cons:
    • Dependency on PSR-17: Requires maintaining PSR-7 compatibility (e.g., if Laravel
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