Introduce a dedicated ResponseEmitter class to safely handle responses#64
Merged
tommitchelmore merged 4 commits intomasterfrom Mar 24, 2026
Merged
Conversation
… emission and refactor core classes to use it via dependency injection
added 2 commits
March 24, 2026 13:53
richstandbrook
approved these changes
Mar 24, 2026
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.
Safe Response Emission and Defensive Header Management
Overview
This PR introduces a
ResponseEmitterabstraction to handle PSR-7 response emission safely and implements defensive guards across the core to prevent "headers already sent" issues during complex WordPress lifecycles.Problem
Laminas\HttpHandlerRunner\Emitter\SapiEmitterthrows anEmitterExceptionifheaders_sent()is true, which occurs frequently in WordPress when plugins or the theme have already started output.send_headerscan be triggered multiple times, and attempting to modify headers (e.g., setting session cookies or removing headers) after output has started leads to PHP warnings and unstable state.Solution
Safe Response Emission
We introduced
Rareloop\Lumberjack\Http\ResponseEmitter, which encapsulates emission logic with a fallback mechanism:headers_sent()is true, it echoes the response body directly.Laminas\SapiEmitterfor standard emission.Defensive Header Management
src/Application.php: Added aheaders_sent()guard toremoveSentHeadersAndMoveIntoResponse. This ensures the method returns early if it's too late to manipulate the header stack, avoiding unnecessary processing and potential side effects.src/Providers/SessionServiceProvider.php: Implemented a$cookieSetstate lock and aheaders_sent()check within thesend_headersaction. This prevents duplicate session cookies and ensuressetcookie()is only called when valid, specifically handling cases where WordPress might fire the hook twice.Implementation Details
src/Http/ResponseEmitter.php: New class providing the safeemit()method.src/Application.php: Refactored to useResponseEmitterand added aterminate()wrapper for testing.src/Bootstrappers/RegisterExceptionHandler.php: Updated to use the new emitter via dependency injection.src/Providers/SessionServiceProvider.php: Hardened the session cookie emission logic.Testing
phpmockto verify both "headers sent" and "headers not sent" scenarios.