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

eliashaeussler / typo3-warming / 15384842948

02 Jun 2025 05:49AM UTC coverage: 90.629% (+0.1%) from 90.534%
15384842948

Pull #866

github

eliashaeussler
[FEATURE] Inject all available crawling strategies into extension configuration modal
Pull Request #866: [FEATURE] Inject all available crawling strategies into extension configuration modal

15 of 15 new or added lines in 2 files covered. (100.0%)

2 existing lines in 1 file now uncovered.

1354 of 1494 relevant lines covered (90.63%)

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

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

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

59
        // Early return on TYPO3 v12 (uses jQuery and thus doesn't need a workaround for <script> tags in modals)
60
        // @todo Remove once support for TYPO3 v12 is dropped
61
        if ((new Core\Information\Typo3Version())->getMajorVersion() === 12) {
29✔
UNCOV
62
            return $response;
×
63
        }
64

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

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

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

83
        return $response;
21✔
84
    }
85

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

93
        return <<<JS
21✔
94
<script async nonce="{$nonceValue}" id="tx-warming-script-inject">
21✔
95
import('@eliashaeussler/typo3-warming/backend/extension-configuration.js').then(({default: extensionConfiguration}) => {
96
    extensionConfiguration.initializeModalListener('{$nonceValue}', {$strategies});
21✔
97
});
98
</script>
99
JS;
21✔
100
    }
101
}
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

© 2025 Coveralls, Inc