Skip to content

Add middleware/interceptor system (v3.7.0)#77

Merged
Thavarshan merged 1 commit into
mainfrom
feature/middleware-system
Jul 21, 2026
Merged

Add middleware/interceptor system (v3.7.0)#77
Thavarshan merged 1 commit into
mainfrom
feature/middleware-system

Conversation

@Thavarshan

Copy link
Copy Markdown
Owner

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\Middlewarehandle(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\ManagesMiddlewareaddMiddleware(), middleware(), withoutMiddleware(), getMiddleware(), plus when() / unless() conditionals, on both ClientHandler and Client. Priority ordering (highest runs first, stable on ties).
  • Integration in 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 buffered Response). Works in async mode too.
  • Built-in middleware: AddHeadersMiddleware (inject/override headers) and LoggingMiddleware (PSR-3 request/response logging with a correlation id; sync + async).

Design notes

  • Purely additive — no breaking changes. Targets a v3.7.0 minor release.
  • Middleware operate on the PSR-7 request's method, URI, headers, and body; body rewrites are sent verbatim while structured json/form_params/multipart bodies are preserved when untouched.
  • Docs: README feature, CODE_MAP.md section, new docs/guide/middleware.md (+ sidebar), CHANGELOG.md v3.7.0.

Testing

  • 30 new tests: pipeline ordering/short-circuit/callables (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).
  • Full suite: 548 tests pass. PHPStan clean. Duster lint clean. VitePress docs build succeeds.

🤖 Generated with Claude Code

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>
@cursor

cursor Bot commented Jul 21, 2026

Copy link
Copy Markdown

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.

@netlify

netlify Bot commented Jul 21, 2026

Copy link
Copy Markdown

Deploy Preview for fetch-php canceled.

Name Link
🔨 Latest commit 0670282
🔍 Latest deploy log https://app.netlify.com/projects/fetch-php/deploys/6a5f56fc5594210007bc1c38

@github-actions github-actions Bot added the breaking-change Changes the public API or observable behaviour label Jul 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@Thavarshan
Thavarshan merged commit 06f662e into main Jul 21, 2026
42 checks passed
@Thavarshan
Thavarshan deleted the feature/middleware-system branch July 21, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Changes the public API or observable behaviour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Middleware/Interceptor System

2 participants