-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPageRendererHook.php
More file actions
91 lines (81 loc) · 3.41 KB
/
Copy pathPageRendererHook.php
File metadata and controls
91 lines (81 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace B13\Http2;
/*
* This file is part of TYPO3 CMS-based extension "http2" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
/**
* Hooks into PageRenderer before the rendering is taken care of, and remember the files
* that can be sent as resources.
*
* This considers that everything is required, thus is marked as "preload", not via "prefetch".
*/
class PageRendererHook
{
public function __construct(protected ResourceMatcher $matcher) {}
/**
* @param array $params
* @param PageRenderer $pageRenderer
* @internal this is just a hook implementation.
*/
public function accumulateResources(array $params, PageRenderer $pageRenderer)
{
$request = $this->getRequest();
if ($request === null) {
return;
}
// If this is a second run (non-cached cObjects adding more data), then the existing cached data is fetched
if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
/** @var TypoScriptFrontendController $frontendController */
$frontendController = $request->getAttribute('frontend.controller');
$allResources = $frontendController->config['b13/http2'] ?? [];
} else {
$allResources = [];
}
foreach ($params['headerData'] as $headerData) {
$allResources = array_merge_recursive($allResources, $this->matcher->match($headerData));
}
foreach ($params['footerData'] as $footerData) {
$allResources = array_merge_recursive($allResources, $this->matcher->match($footerData));
}
foreach (['jsLibs', 'jsFiles', 'jsFooterLibs', 'jsFooterFiles', 'cssLibs', 'cssFiles'] as $part) {
if (empty($params[$part])) {
continue;
}
$allResources = array_merge_recursive($allResources, $this->matcher->match($params[$part]));
}
$allResources['scripts'] = array_unique($allResources['scripts'] ?? []);
$allResources['styles'] = array_unique($allResources['styles'] ?? []);
$this->process($allResources, $request);
}
/**
* In FE the data is stored in TSFE->config.
* In BE it is sent directly to the HTTP response.
*
* @param array $allResources
*/
protected function process(array $allResources, ServerRequestInterface $request): void
{
if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) {
/** @var TypoScriptFrontendController $frontendController */
$frontendController = $request->getAttribute('frontend.controller');
$frontendController->config['b13/http2'] = $allResources;
} elseif (GeneralUtility::getIndpEnv('TYPO3_SSL')) {
// Push directly into the TYPO3 Backend, but only if TYPO3 is running in SSL
GeneralUtility::makeInstance(ResourcePusher::class)->pushAll($allResources);
}
}
protected function getRequest(): ?ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'] ?? null;
}
}