Completed
Branch master (d17104)
by Christian
21:20
created

AdminPanelRenderer::process()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 2
nop 2
dl 0
loc 25
rs 8.6666
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Middleware;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Server\MiddlewareInterface;
22
use Psr\Http\Server\RequestHandlerInterface;
23
use TYPO3\CMS\Adminpanel\Controller\MainController;
24
use TYPO3\CMS\Adminpanel\Utility\StateUtility;
25
use TYPO3\CMS\Core\Http\NullResponse;
26
use TYPO3\CMS\Core\Http\Stream;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
29
30
/**
31
 * Render the admin panel via PSR-15 middleware
32
 *
33
 * @internal
34
 */
35
class AdminPanelRenderer implements MiddlewareInterface
36
{
37
38
    /**
39
     * Render the admin panel if activated
40
     * @param ServerRequestInterface $request
41
     * @param RequestHandlerInterface $handler
42
     * @return ResponseInterface
43
     */
44
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
45
    {
46
        $response = $handler->handle($request);
47
        if (
48
            !($response instanceof NullResponse)
49
            && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
50
            && $GLOBALS['TSFE']->isOutputting()
51
            && StateUtility::isActivatedForUser()
52
            && StateUtility::isActivatedInTypoScript()
53
            && !StateUtility::isHiddenForUser()
54
        ) {
55
            $mainController = GeneralUtility::makeInstance(MainController::class);
56
            $body = $response->getBody();
57
            $body->rewind();
58
            $contents = $response->getBody()->getContents();
59
            $content = str_ireplace(
60
                '</body>',
61
                $mainController->render($request) . '</body>',
62
                $contents
63
            );
64
            $body = new Stream('php://temp', 'rw');
65
            $body->write($content);
66
            $response = $response->withBody($body);
67
        }
68
        return $response;
69
    }
70
}
71