|
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\Extbase\Core; |
|
19
|
|
|
|
|
20
|
|
|
use Psr\Container\ContainerInterface; |
|
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
use TYPO3\CMS\Backend\Routing\Route; |
|
24
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager; |
|
25
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
|
26
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
27
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
|
28
|
|
|
use TYPO3\CMS\Extbase\Configuration\RequestHandlersConfigurationFactory; |
|
29
|
|
|
use TYPO3\CMS\Extbase\Mvc\Dispatcher; |
|
30
|
|
|
use TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver; |
|
31
|
|
|
use TYPO3\CMS\Extbase\Mvc\RequestInterface; |
|
32
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder; |
|
33
|
|
|
use TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory; |
|
34
|
|
|
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface; |
|
35
|
|
|
use TYPO3\CMS\Extbase\Service\CacheService; |
|
36
|
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Creates a request and dispatches it to the controller which was specified |
|
40
|
|
|
* by TS Setup, flexForm and returns the content. |
|
41
|
|
|
* |
|
42
|
|
|
* This class is the main entry point for extbase extensions. |
|
43
|
|
|
*/ |
|
44
|
|
|
class Bootstrap |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
public static $persistenceClasses = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Back reference to the parent content object |
|
53
|
|
|
* This has to be public as it is set directly from TYPO3 |
|
54
|
|
|
* |
|
55
|
|
|
* @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer |
|
56
|
|
|
*/ |
|
57
|
|
|
public $cObj; |
|
58
|
|
|
|
|
59
|
|
|
protected ContainerInterface $container; |
|
60
|
|
|
protected ConfigurationManagerInterface $configurationManager; |
|
61
|
|
|
protected PersistenceManagerInterface $persistenceManager; |
|
62
|
|
|
protected RequestHandlerResolver $requestHandlerResolver; |
|
63
|
|
|
protected CacheService $cacheService; |
|
64
|
|
|
protected Dispatcher $dispatcher; |
|
65
|
|
|
protected RequestBuilder $extbaseRequestBuilder; |
|
66
|
|
|
|
|
67
|
|
|
public function __construct( |
|
68
|
|
|
ContainerInterface $container, |
|
69
|
|
|
ConfigurationManagerInterface $configurationManager, |
|
70
|
|
|
PersistenceManagerInterface $persistenceManager, |
|
71
|
|
|
RequestHandlerResolver $requestHandlerResolver, |
|
72
|
|
|
CacheService $cacheService, |
|
73
|
|
|
Dispatcher $dispatcher, |
|
74
|
|
|
RequestBuilder $extbaseRequestBuilder |
|
75
|
|
|
) { |
|
76
|
|
|
$this->container = $container; |
|
77
|
|
|
$this->configurationManager = $configurationManager; |
|
78
|
|
|
$this->persistenceManager = $persistenceManager; |
|
79
|
|
|
$this->requestHandlerResolver = $requestHandlerResolver; |
|
80
|
|
|
$this->cacheService = $cacheService; |
|
81
|
|
|
$this->dispatcher = $dispatcher; |
|
82
|
|
|
$this->extbaseRequestBuilder = $extbaseRequestBuilder; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Explicitly initializes all necessary Extbase objects by invoking the various initialize* methods. |
|
87
|
|
|
* |
|
88
|
|
|
* Usually this method is only called from unit tests or other applications which need a more fine grained control over |
|
89
|
|
|
* the initialization and request handling process. Most other applications just call the run() method. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $configuration The TS configuration array |
|
92
|
|
|
* @throws \RuntimeException |
|
93
|
|
|
* @see run() |
|
94
|
|
|
*/ |
|
95
|
|
|
public function initialize(array $configuration): void |
|
96
|
|
|
{ |
|
97
|
|
|
if (!Environment::isCli()) { |
|
98
|
|
|
if (!isset($configuration['extensionName']) || $configuration['extensionName'] === '') { |
|
99
|
|
|
throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020); |
|
100
|
|
|
} |
|
101
|
|
|
if (!isset($configuration['pluginName']) || $configuration['pluginName'] === '') { |
|
102
|
|
|
throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
$this->initializeConfiguration($configuration); |
|
106
|
|
|
$this->initializePersistenceClassesConfiguration(); |
|
107
|
|
|
$this->initializeRequestHandlersConfiguration(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Initializes the Object framework. |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $configuration |
|
114
|
|
|
* @see initialize() |
|
115
|
|
|
* @internal |
|
116
|
|
|
*/ |
|
117
|
|
|
public function initializeConfiguration(array $configuration): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->cObj ??= $this->container->get(ContentObjectRenderer::class); |
|
120
|
|
|
$this->configurationManager->setContentObject($this->cObj); |
|
121
|
|
|
$this->configurationManager->setConfiguration($configuration); |
|
122
|
|
|
// todo: Shouldn't the configuration manager object – which is a singleton – be stateless? |
|
123
|
|
|
// todo: At this point we give the configuration manager a state, while we could directly pass the |
|
124
|
|
|
// todo: configuration (i.e. controllerName, actionName and such), directly to the request |
|
125
|
|
|
// todo: handler, which then creates stateful request objects. |
|
126
|
|
|
// todo: Once this has changed, \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder::loadDefaultValues does not need |
|
127
|
|
|
// todo: to fetch this configuration from the configuration manager. |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException |
|
132
|
|
|
*/ |
|
133
|
|
|
private function initializePersistenceClassesConfiguration(): void |
|
134
|
|
|
{ |
|
135
|
|
|
$cacheManager = GeneralUtility::makeInstance(CacheManager::class); |
|
136
|
|
|
GeneralUtility::makeInstance(ClassesConfigurationFactory::class, $cacheManager) |
|
137
|
|
|
->createClassesConfiguration(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException |
|
142
|
|
|
*/ |
|
143
|
|
|
private function initializeRequestHandlersConfiguration(): void |
|
144
|
|
|
{ |
|
145
|
|
|
$cacheManager = GeneralUtility::makeInstance(CacheManager::class); |
|
146
|
|
|
GeneralUtility::makeInstance(RequestHandlersConfigurationFactory::class, $cacheManager) |
|
147
|
|
|
->createRequestHandlersConfiguration(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it. |
|
152
|
|
|
* If the Framework is not initialized yet, it will be initialized. |
|
153
|
|
|
* |
|
154
|
|
|
* This is usually used in Frontend plugins. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $content The content. Not used |
|
157
|
|
|
* @param array $configuration The TS configuration array |
|
158
|
|
|
* @return string $content The processed content |
|
159
|
|
|
*/ |
|
160
|
|
|
public function run(string $content, array $configuration, ?ServerRequestInterface $request = null): string |
|
161
|
|
|
{ |
|
162
|
|
|
$request = $request ?? $GLOBALS['TYPO3_REQUEST']; |
|
163
|
|
|
$this->initialize($configuration); |
|
164
|
|
|
return $this->handleFrontendRequest($request); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
protected function handleFrontendRequest(ServerRequestInterface $request): string |
|
168
|
|
|
{ |
|
169
|
|
|
$extbaseRequest = $this->extbaseRequestBuilder->build($request); |
|
170
|
|
|
if (!$this->isExtbaseRequestCacheable($extbaseRequest)) { |
|
171
|
|
|
if ($this->cObj->getUserObjectType() === ContentObjectRenderer::OBJECTTYPE_USER) { |
|
|
|
|
|
|
172
|
|
|
// ContentObjectRenderer::convertToUserIntObject() will recreate the object, |
|
173
|
|
|
// so we have to stop the request here before the action is actually called |
|
174
|
|
|
$this->cObj->convertToUserIntObject(); |
|
175
|
|
|
return ''; |
|
176
|
|
|
} |
|
177
|
|
|
$extbaseRequest->setIsCached(false); |
|
178
|
|
|
} else { |
|
179
|
|
|
$extbaseRequest->setIsCached(true); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// Dispatch the extbase request |
|
183
|
|
|
$requestHandler = $this->requestHandlerResolver->resolveRequestHandler($extbaseRequest); |
|
184
|
|
|
$response = $requestHandler->handleRequest($extbaseRequest); |
|
185
|
|
|
if (headers_sent() === false) { |
|
186
|
|
|
foreach ($response->getHeaders() as $name => $values) { |
|
187
|
|
|
foreach ($values as $value) { |
|
188
|
|
|
header(sprintf('%s: %s', $name, $value)); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
$body = $response->getBody(); |
|
193
|
|
|
$body->rewind(); |
|
194
|
|
|
$content = $body->getContents(); |
|
195
|
|
|
$this->resetSingletons(); |
|
196
|
|
|
$this->cacheService->clearCachesOfRegisteredPageIds(); |
|
197
|
|
|
return $content; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Entrypoint for backend modules, handling PSR-7 requests/responses. |
|
202
|
|
|
* |
|
203
|
|
|
* Creates an Extbase Request, dispatches it and then returns the Response |
|
204
|
|
|
* |
|
205
|
|
|
* @param ServerRequestInterface $request |
|
206
|
|
|
* @return ResponseInterface |
|
207
|
|
|
* @internal |
|
208
|
|
|
*/ |
|
209
|
|
|
public function handleBackendRequest(ServerRequestInterface $request): ResponseInterface |
|
210
|
|
|
{ |
|
211
|
|
|
// build the configuration from the Server request / route |
|
212
|
|
|
/** @var Route $route */ |
|
213
|
|
|
$route = $request->getAttribute('route'); |
|
214
|
|
|
$moduleConfiguration = $route->getOption('moduleConfiguration'); |
|
215
|
|
|
$configuration = [ |
|
216
|
|
|
'extensionName' => $moduleConfiguration['extensionName'], |
|
217
|
|
|
'pluginName' => $route->getOption('moduleName') |
|
218
|
|
|
]; |
|
219
|
|
|
|
|
220
|
|
|
$this->initialize($configuration); |
|
221
|
|
|
$extbaseRequest = $this->extbaseRequestBuilder->build($request); |
|
222
|
|
|
$response = $this->dispatcher->dispatch($extbaseRequest); |
|
223
|
|
|
$this->resetSingletons(); |
|
224
|
|
|
$this->cacheService->clearCachesOfRegisteredPageIds(); |
|
225
|
|
|
return $response; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Resets global singletons for the next plugin |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function resetSingletons(): void |
|
232
|
|
|
{ |
|
233
|
|
|
$this->persistenceManager->persistAll(); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
protected function isExtbaseRequestCacheable(RequestInterface $extbaseRequest): bool |
|
237
|
|
|
{ |
|
238
|
|
|
$controllerClassName = $extbaseRequest->getControllerObjectName(); |
|
239
|
|
|
$actionName = $extbaseRequest->getControllerActionName(); |
|
|
|
|
|
|
240
|
|
|
$frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
|
241
|
|
|
$nonCacheableActions = $frameworkConfiguration['controllerConfiguration'][$controllerClassName]['nonCacheableActions'] ?? null; |
|
242
|
|
|
if (!is_array($nonCacheableActions)) { |
|
243
|
|
|
return true; |
|
244
|
|
|
} |
|
245
|
|
|
return !in_array($actionName, $nonCacheableActions, true); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|