• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-warming / 16642170546

31 Jul 2025 06:58AM UTC coverage: 92.569% (-0.05%) from 92.618%
16642170546

push

github

web-flow
Merge pull request #917 from eliashaeussler/fix/inject-js

1 of 2 new or added lines in 1 file covered. (50.0%)

1632 of 1763 relevant lines covered (92.57%)

12.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/Classes/Middleware/InjectExtensionConfigurationScriptMiddleware.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "warming".
7
 *
8
 * Copyright (C) 2021-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3Warming\Middleware;
25

26
use EliasHaeussler\CacheWarmup;
27
use Psr\Http\Message;
28
use Psr\Http\Server;
29
use TYPO3\CMS\Backend;
30
use TYPO3\CMS\Core;
31

32
/**
33
 * InjectExtensionConfigurationScriptMiddleware
34
 *
35
 * @author Elias Häußler <elias@haeussler.dev>
36
 * @license GPL-2.0-or-later
37
 */
38
final readonly class InjectExtensionConfigurationScriptMiddleware implements Server\MiddlewareInterface
39
{
40
    public function __construct(
38✔
41
        private CacheWarmup\Crawler\Strategy\CrawlingStrategyFactory $crawlingStrategyFactory,
42
    ) {}
38✔
43

44
    public function process(
38✔
45
        Message\ServerRequestInterface $request,
46
        Server\RequestHandlerInterface $handler,
47
    ): Message\ResponseInterface {
48
        $response = $handler->handle($request);
38✔
49
        /** @var Backend\Routing\Route|null $route */
50
        $route = $request->getAttribute('route');
38✔
51
        /** @var Core\Authentication\BackendUserAuthentication|null $backendUser */
52
        $backendUser = $GLOBALS['BE_USER'] ?? null;
38✔
53

54
        // Early return if we're not on main route
55
        if ($route?->getPath() !== '/main') {
38✔
56
            return $response;
38✔
57
        }
58

59
        // Early return if EXT:install is not loaded and extension settings module is not available
60
        if (!Core\Utility\ExtensionManagementUtility::isLoaded('install')) {
38✔
NEW
61
            return $response;
×
62
        }
63

64
        // Early return if response is invalid
65
        if ($response->getStatusCode() !== 200) {
38✔
66
            return $response;
×
67
        }
68

69
        // Early return on insufficient privileges (only system maintainers can access settings module)
70
        if (!($backendUser?->isSystemMaintainer() ?? false)) {
38✔
71
            return $response;
11✔
72
        }
73

74
        // Inject scripts into <head>
75
        $body = $response->getBody();
28✔
76
        $contents = (string)$body;
28✔
77
        $body->rewind();
28✔
78
        $body->write(
28✔
79
            str_replace('</head>', $this->renderScriptTag($request) . '</head>', $contents),
28✔
80
        );
28✔
81

82
        return $response;
28✔
83
    }
84

85
    private function renderScriptTag(Message\ServerRequestInterface $request): string
28✔
86
    {
87
        /** @var Core\Security\ContentSecurityPolicy\ConsumableNonce $nonce */
88
        $nonce = $request->getAttribute('nonce');
28✔
89
        $nonceValue = $nonce->consume();
28✔
90
        $strategies = json_encode($this->crawlingStrategyFactory->getAll());
28✔
91

92
        return <<<JS
28✔
93
<script async nonce="{$nonceValue}" id="tx-warming-script-inject">
28✔
94
import('@eliashaeussler/typo3-warming/backend/extension-configuration.js').then(({default: extensionConfiguration}) => {
95
    extensionConfiguration.initializeModalListener('{$nonceValue}', {$strategies});
28✔
96
});
97
</script>
98
JS;
28✔
99
    }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc