TYPO3 v10: Modify HTML in middleware while keeping existing headers

raw

AppendDot.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
namespace Mogic\Extension;
 
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
 
class AppendDot implements \Psr\Http\Server\MiddlewareInterface
{
    /**
     * Modify the generated HTML by adding a dot before the closing body tag.
     */
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);
 
        $newContent = str_replace('</body>', '.</body>', (string) $response->getBody());
 
        //stream body code copied from HtmlResponse
        $body = new \TYPO3\CMS\Core\Http\Stream('php://temp', 'wb+');
        $body->write($newContent);
        $body->rewind();
 
        return $response->withBody($body);
    }
}
Christian Weiske Christian Weiske
owner

History