Completed
Push — master ( 4dc794...0f9475 )
by
unknown
15:26
created

AdminPanelRenderer::process()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 2
nop 2
dl 0
loc 23
rs 9.3554
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Adminpanel\Middleware;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
use TYPO3\CMS\Adminpanel\Controller\MainController;
25
use TYPO3\CMS\Adminpanel\Utility\StateUtility;
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
            $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
49
            && StateUtility::isActivatedForUser()
50
            && StateUtility::isActivatedInTypoScript()
51
            && !StateUtility::isHiddenForUser()
52
        ) {
53
            $mainController = GeneralUtility::makeInstance(MainController::class);
54
            $body = $response->getBody();
55
            $body->rewind();
56
            $contents = $response->getBody()->getContents();
57
            $content = str_ireplace(
58
                '</body>',
59
                $mainController->render($request) . '</body>',
60
                $contents
61
            );
62
            $body = new Stream('php://temp', 'rw');
63
            $body->write($content);
64
            $response = $response->withBody($body);
65
        }
66
        return $response;
67
    }
68
}
69