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.
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).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).Dispatcher and CallableHandler simplify middleware testing by providing standalone execution without requiring a full Laravel container or HTTP server.Illuminate\Pipeline) is PSR-15 compatible, so this package can integrate as a drop-in utility for PSR-7/PSR-17 operations.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).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()).Request facade) may not translate directly to PSR-7.Symfony\Component\HttpFoundation is not a PSR-17 factory, so migration effort is required to adopt this package for PSR-7 message creation.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.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).Factory default to Laravel’s Symfony\Component\HttpFoundation or a PSR-17 implementation (e.g., Diactoros)?UploadedFileInterface) be handled, given Laravel’s Illuminate\Http\UploadedFile is not PSR-7 compliant?Authenticate, Validate) be rewrapped as PSR-15 middleware, or will this package only handle new middleware?FactoryDiscovery overhead justify the flexibility, or should a static PSR-17 factory (e.g., Diactoros) be hardcoded?HttpErrorException integrate with Laravel’s exception handling (e.g., App\Exceptions\Handler)?psr/http-server-middleware or third-party packages like middlewares/auth).Symfony\Component\HttpFoundation for consistent PSR-7 message creation (e.g., in APIs, CLI commands, or testing).Dispatcher and CallableHandler simplify middleware unit testing by providing standalone execution.CallableHandler enables runtime middleware resolution (e.g., for route-based middleware).Route::get()/Route::post() workflows, which rely on Illuminate\Http\Request/Response.| 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. |
RequestHandlerContainer for dependency injection).Request/Response facades, Route system, or Illuminate\Pipeline.Illuminate\Http\Request to PSR-7 before passing to middleware).symfony/http-foundation and nyholm/psr7 unless using FactoryDiscovery to prioritize one.Phase 1: Testing & Utilities
Factory for PSR-7 message creation in:
create_mock_request()).artisan commands needing HTTP messages).Dispatcher implementations with middlewares/utils/Dispatcher.Phase 2: Middleware Layer
CallableHandler for dynamic middleware (e.g., route-based middleware).Illuminate\Pipeline middleware).Phase 3: Error Handling
App\Exceptions\Handler to convert HttpErrorException to Laravel responses.HttpErrorException-based implementations.Phase 4: Full PSR-7 Adoption (Optional)
Symfony\Component\HttpFoundation with this package’s Factory for all HTTP message creation.UploadedFileInterface.Factory and Dispatcher reduce custom utility code.How can I help you explore Laravel packages today?