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

eliashaeussler / typo3-warming / 15422445901

03 Jun 2025 04:13PM UTC coverage: 90.617% (-0.4%) from 91.04%
15422445901

push

github

github-actions[bot]
[TASK] Automatically rebuild frontend assets

1352 of 1492 relevant lines covered (90.62%)

10.07 hits per line

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

96.3
/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(
30✔
41
        private CacheWarmup\Crawler\Strategy\CrawlingStrategyFactory $crawlingStrategyFactory,
42
    ) {}
30✔
43

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

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

59
        // Early return if response is invalid
60
        if ($response->getStatusCode() !== 200) {
30✔
61
            return $response;
×
62
        }
63

64
        // Early return on insufficient privileges (only system maintainers can access settings module)
65
        if (!($backedUser?->isSystemMaintainer() ?? false)) {
30✔
66
            return $response;
8✔
67
        }
68

69
        // Inject scripts into <head>
70
        $body = $response->getBody();
22✔
71
        $contents = (string)$body;
22✔
72
        $body->rewind();
22✔
73
        $body->write(
22✔
74
            str_replace('</head>', $this->renderScriptTag($request) . '</head>', $contents),
22✔
75
        );
22✔
76

77
        return $response;
22✔
78
    }
79

80
    private function renderScriptTag(Message\ServerRequestInterface $request): string
22✔
81
    {
82
        /** @var Core\Security\ContentSecurityPolicy\ConsumableNonce $nonce */
83
        $nonce = $request->getAttribute('nonce');
22✔
84
        $nonceValue = $nonce->consume();
22✔
85
        $strategies = json_encode($this->crawlingStrategyFactory->getAll());
22✔
86

87
        return <<<JS
22✔
88
<script async nonce="{$nonceValue}" id="tx-warming-script-inject">
22✔
89
import('@eliashaeussler/typo3-warming/backend/extension-configuration.js').then(({default: extensionConfiguration}) => {
90
    extensionConfiguration.initializeModalListener('{$nonceValue}', {$strategies});
22✔
91
});
92
</script>
93
JS;
22✔
94
    }
95
}
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