Refactor: Decouple Visitor and Stream for Renderers #143
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.
Covers PR#142.
Because the PSR-7 HTTP middleware requires the response body to be a message stream, the renderers were built to not only implement the visitor pattern, but also the PSR-7 message stream interface.
For layouts and templates, streams are not easy to handle, especially, when things need to be inserted in certain positions. Whenever the buffer content changes, the cursor position needs to be re-calculated. Even for appending it is needed to move the pointer to the end of the buffer first.
That is way too complicated and prune to errors.
This PR separates the stream from the renderers. A renderer manages its own string buffer, if needed,
After rendering, the buffer is wrapped into a PSR-7 compatible stream and attached as body to the response, as that is the only place, where the output has to be a stream.
ContentTypeVisitorTrait is introduced with this PR to simplify the handling of visitor methods, especially in case they are very similar. Additionally, renderers using that trait are much more robust against changes in the ContentTypeVisitorInterface, as they automatically provide a fallback for new content types.