Add middleware/interceptor system (v3.7.0)#77
Merged
Conversation
Implement a PSR-7 based middleware pipeline for wrapping the request/response lifecycle with cross-cutting concerns (resolves #39). - Middleware interface: handle(RequestInterface, callable $next); plain callables with the same signature are also accepted. - MiddlewarePipeline composes an ordered, outermost-first stack into a single onion around the core request handler; transport-agnostic across sync responses and async promises. - ManagesMiddleware trait adds addMiddleware()/middleware()/withoutMiddleware()/ getMiddleware() plus when()/unless() conditionals to ClientHandler and Client, with priority ordering (highest first, stable on ties). - Integration in PerformsHttpRequests: sendRequest builds a PSR-7 request, runs it through the pipeline, folds changes back into Guzzle options, and wraps the built-in mock/cache/retry/execute core. Middleware can short-circuit (any PSR-7 response is coerced into a Fetch Response) and run in async mode. - Built-in middleware: AddHeadersMiddleware and LoggingMiddleware. Includes unit + integration tests (pipeline ordering, priority, conditionals, short-circuit, body rewrite, async) and docs (README, CODE_MAP, guide, CHANGELOG for v3.7.0). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
✅ Deploy Preview for fetch-php canceled.
|
Contributor
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the Middleware/Interceptor System requested in #39 — a PSR-7-based middleware pipeline for wrapping the request/response lifecycle with cross-cutting concerns (auth, logging, versioning, tenancy, caching layers, circuit breakers, …) without touching every call site.
Closes #39.
What's new
Fetch\Interfaces\Middleware—handle(RequestInterface $request, callable $next): ResponseInterface|PromiseInterface. Plain callables with the same signature are accepted anywhere a middleware is, so trivial middleware need no class.Fetch\Http\MiddlewarePipeline— composes an ordered, outermost-first stack into a single onion around the core request handler. Transport-agnostic: passes through sync responses and async promises unchanged.Fetch\Concerns\ManagesMiddleware—addMiddleware(),middleware(),withoutMiddleware(),getMiddleware(), pluswhen()/unless()conditionals, on bothClientHandlerandClient. Priority ordering (highest runs first, stable on ties).PerformsHttpRequests::sendRequest(): builds a PSR-7 request from the resolved method/URI/headers/body, runs it through the pipeline, and folds middleware changes back into the Guzzle options before the built-in mock/cache/retry/execute core. Middleware therefore sit outside caching and can short-circuit (any PSR-7 response returned is coerced into a bufferedResponse). Works in async mode too.AddHeadersMiddleware(inject/override headers) andLoggingMiddleware(PSR-3 request/response logging with a correlation id; sync + async).Design notes
json/form_params/multipartbodies are preserved when untouched.CODE_MAP.mdsection, newdocs/guide/middleware.md(+ sidebar),CHANGELOG.mdv3.7.0.Testing
MiddlewarePipelineTest), stack management + priority +when/unless(ManagesMiddlewareTest), built-ins, and end-to-end integration via a Guzzle mock handler covering header injection, short-circuit, response mutation, priority, body rewrite, async, and conditional/removal (MiddlewareTest).🤖 Generated with Claude Code