a39737c3edd6eca9e5660c62e224be4c1f8ba6a5
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Compatibility / SlotReplacement.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Compatibility;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\EventDispatcher\EventDispatcherInterface;
20 use TYPO3\CMS\Core\Resource\Event\AfterFileAddedEvent;
21 use TYPO3\CMS\Core\Resource\Event\AfterFileAddedToIndexEvent;
22 use TYPO3\CMS\Core\Resource\Event\AfterFileContentsSetEvent;
23 use TYPO3\CMS\Core\Resource\Event\AfterFileCopiedEvent;
24 use TYPO3\CMS\Core\Resource\Event\AfterFileCreatedEvent;
25 use TYPO3\CMS\Core\Resource\Event\AfterFileDeletedEvent;
26 use TYPO3\CMS\Core\Resource\Event\AfterFileMarkedAsMissingEvent;
27 use TYPO3\CMS\Core\Resource\Event\AfterFileMetaDataCreatedEvent;
28 use TYPO3\CMS\Core\Resource\Event\AfterFileMetaDataDeletedEvent;
29 use TYPO3\CMS\Core\Resource\Event\AfterFileMetaDataUpdatedEvent;
30 use TYPO3\CMS\Core\Resource\Event\AfterFileMovedEvent;
31 use TYPO3\CMS\Core\Resource\Event\AfterFileProcessingEvent;
32 use TYPO3\CMS\Core\Resource\Event\AfterFileRemovedFromIndexEvent;
33 use TYPO3\CMS\Core\Resource\Event\AfterFileRenamedEvent;
34 use TYPO3\CMS\Core\Resource\Event\AfterFileReplacedEvent;
35 use TYPO3\CMS\Core\Resource\Event\AfterFileUpdatedInIndexEvent;
36 use TYPO3\CMS\Core\Resource\Event\AfterFolderAddedEvent;
37 use TYPO3\CMS\Core\Resource\Event\AfterFolderCopiedEvent;
38 use TYPO3\CMS\Core\Resource\Event\AfterFolderDeletedEvent;
39 use TYPO3\CMS\Core\Resource\Event\AfterFolderMovedEvent;
40 use TYPO3\CMS\Core\Resource\Event\AfterFolderRenamedEvent;
41 use TYPO3\CMS\Core\Resource\Event\AfterResourceStorageInitializationEvent;
42 use TYPO3\CMS\Core\Resource\Event\BeforeFileAddedEvent;
43 use TYPO3\CMS\Core\Resource\Event\BeforeFileContentsSetEvent;
44 use TYPO3\CMS\Core\Resource\Event\BeforeFileCopiedEvent;
45 use TYPO3\CMS\Core\Resource\Event\BeforeFileCreatedEvent;
46 use TYPO3\CMS\Core\Resource\Event\BeforeFileDeletedEvent;
47 use TYPO3\CMS\Core\Resource\Event\BeforeFileMovedEvent;
48 use TYPO3\CMS\Core\Resource\Event\BeforeFileProcessingEvent;
49 use TYPO3\CMS\Core\Resource\Event\BeforeFileRenamedEvent;
50 use TYPO3\CMS\Core\Resource\Event\BeforeFileReplacedEvent;
51 use TYPO3\CMS\Core\Resource\Event\BeforeFolderAddedEvent;
52 use TYPO3\CMS\Core\Resource\Event\BeforeFolderCopiedEvent;
53 use TYPO3\CMS\Core\Resource\Event\BeforeFolderDeletedEvent;
54 use TYPO3\CMS\Core\Resource\Event\BeforeFolderMovedEvent;
55 use TYPO3\CMS\Core\Resource\Event\BeforeFolderRenamedEvent;
56 use TYPO3\CMS\Core\Resource\Event\BeforeResourceStorageInitializationEvent;
57 use TYPO3\CMS\Core\Resource\Event\EnrichFileMetaDataEvent;
58 use TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent;
59 use TYPO3\CMS\Core\Resource\Event\SanitizeFileNameEvent;
60 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
61 use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
62 use TYPO3\CMS\Core\Resource\ResourceFactory;
63 use TYPO3\CMS\Core\Resource\ResourceStorage;
64 use TYPO3\CMS\Core\Resource\Service\FileProcessingService;
65 use TYPO3\CMS\Core\Utility\GeneralUtility;
66 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher as SignalSlotDispatcher;
67
68 /**
69 * This class provides a replacement for all existing signals in TYPO3 Core, which now act as a simple wrapper
70 * for PSR-14 events with a simple ("first prioritized") listener implementation.
71 *
72 * @internal Please note that this class will likely be removed in TYPO3 v11, and Extension Authors should
73 * switch to PSR-14 event listeners.
74 */
75 class SlotReplacement
76 {
77 /**
78 * @var SignalSlotDispatcher
79 */
80 protected $signalSlotDispatcher;
81
82 /**
83 * @var EventDispatcherInterface
84 */
85 protected $eventDispatcher;
86
87 public function __construct(SignalSlotDispatcher $signalSlotDispatcher, EventDispatcherInterface $eventDispatcher)
88 {
89 $this->signalSlotDispatcher = $signalSlotDispatcher;
90 $this->eventDispatcher = $eventDispatcher;
91 }
92
93 public function onMetaDataRepositoryRecordPostRetrieval(EnrichFileMetaDataEvent $event): void
94 {
95 $data = $event->getRecord();
96 $data = new \ArrayObject($data);
97 $this->signalSlotDispatcher->dispatch(MetaDataRepository::class, 'recordPostRetrieval', [$data]);
98 $event->setRecord($data->getArrayCopy());
99 }
100
101 public function onMetaDataRepositoryRecordUpdated(AfterFileMetaDataUpdatedEvent $event): void
102 {
103 $this->signalSlotDispatcher->dispatch(MetaDataRepository::class, 'recordUpdated', [$event->getRecord()]);
104 }
105
106 public function onMetaDataRepositoryRecordCreated(AfterFileMetaDataCreatedEvent $event): void
107 {
108 $this->signalSlotDispatcher->dispatch(MetaDataRepository::class, 'recordCreated', [$event->getRecord()]);
109 }
110
111 public function onMetaDataRepositoryRecordDeleted(AfterFileMetaDataDeletedEvent $event): void
112 {
113 $this->signalSlotDispatcher->dispatch(MetaDataRepository::class, 'recordDeleted', [$event->getFileUid()]);
114 }
115
116 public function onFileIndexRepositoryRecordUpdated(AfterFileUpdatedInIndexEvent $event): void
117 {
118 $this->signalSlotDispatcher->dispatch(FileIndexRepository::class, 'recordUpdated', [$event->getRelevantProperties()]);
119 }
120
121 public function onFileIndexRepositoryRecordCreated(AfterFileAddedToIndexEvent $event): void
122 {
123 $this->signalSlotDispatcher->dispatch(FileIndexRepository::class, 'recordCreated', [$event->getRecord()]);
124 }
125
126 public function onFileIndexRepositoryRecordMarkedAsMissing(AfterFileMarkedAsMissingEvent $event): void
127 {
128 $this->signalSlotDispatcher->dispatch(FileIndexRepository::class, 'recordMarkedAsMissing', [$event->getFileUid()]);
129 }
130
131 public function onFileIndexRepositoryRecordDeleted(AfterFileRemovedFromIndexEvent $event): void
132 {
133 $this->signalSlotDispatcher->dispatch(FileIndexRepository::class, 'recordDeleted', [$event->getFileUid()]);
134 }
135
136 public function onResourceFactoryPreProcessStorage(BeforeResourceStorageInitializationEvent $event): void
137 {
138 [, $uid, $recordData, $fileIdentifier] = $this->signalSlotDispatcher->dispatch(
139 ResourceFactory::class,
140 'preProcessStorage',
141 [ResourceFactory::getInstance(), $event->getStorageUid(), $event->getRecord(), $event->getFileIdentifier()]
142 );
143 $event->setStorageUid($uid);
144 $event->setRecord($recordData);
145 $event->setFileIdentifier($fileIdentifier);
146 }
147
148 public function onResourceFactoryPostProcessStorage(AfterResourceStorageInitializationEvent $event): void
149 {
150 $this->signalSlotDispatcher->dispatch(
151 ResourceFactory::class,
152 'postProcessStorage',
153 [ResourceFactory::getInstance(), $event->getStorage()]
154 );
155 }
156
157 public function onFileProcessingServiceEmitPreFileProcessSignal(BeforeFileProcessingEvent $event): void
158 {
159 $service = GeneralUtility::makeInstance(
160 FileProcessingService::class,
161 $event->getFile()->getStorage(),
162 $event->getDriver(),
163 $this->eventDispatcher
164 );
165 $this->signalSlotDispatcher->dispatch(
166 ResourceStorage::class,
167 FileProcessingService::SIGNAL_PreFileProcess,
168 [
169 $service, $event->getDriver(), $event->getProcessedFile(), $event->getFile(), $event->getTaskType(), $event->getConfiguration()
170 ]
171 );
172 }
173
174 public function onFileProcessingServiceEmitPostFileProcessSignal(AfterFileProcessingEvent $event): void
175 {
176 $service = GeneralUtility::makeInstance(
177 FileProcessingService::class,
178 $event->getFile()->getStorage(),
179 $event->getDriver(),
180 $this->eventDispatcher
181 );
182 $this->signalSlotDispatcher->dispatch(
183 ResourceStorage::class,
184 FileProcessingService::SIGNAL_PostFileProcess,
185 [$service, $event->getDriver(), $event->getProcessedFile(), $event->getFile(), $event->getTaskType(), $event->getConfiguration()]
186 );
187 }
188
189 public function onResourceStorageEmitSanitizeFileNameSignal(SanitizeFileNameEvent $event): void
190 {
191 list($fileName) = $this->signalSlotDispatcher->dispatch(
192 ResourceStorage::class,
193 ResourceStorage::SIGNAL_SanitizeFileName,
194 [
195 $event->getFileName(),
196 $event->getTargetFolder(),
197 $event->getStorage(),
198 $event->getDriver()
199 ]
200 );
201 $event->setFileName($fileName);
202 }
203
204 public function onResourceStorageEmitPreFileAddSignal(BeforeFileAddedEvent $event): void
205 {
206 $targetFileName = $event->getFileName();
207 $this->signalSlotDispatcher->dispatch(
208 ResourceStorage::class,
209 ResourceStorage::SIGNAL_PreFileAdd,
210 [
211 &$targetFileName,
212 $event->getTargetFolder(),
213 $event->getSourceFilePath(),
214 $event->getStorage(),
215 $event->getDriver()
216 ]
217 );
218 $event->setFileName($targetFileName);
219 }
220
221 public function onResourceStorageEmitPostFileAddSignal(AfterFileAddedEvent $event): void
222 {
223 $this->signalSlotDispatcher->dispatch(
224 ResourceStorage::class,
225 ResourceStorage::SIGNAL_PostFileAdd,
226 [
227 $event->getFile(),
228 $event->getFolder()
229 ]
230 );
231 }
232
233 public function onResourceStorageEmitPreFileCopySignal(BeforeFileCopiedEvent $event): void
234 {
235 $this->signalSlotDispatcher->dispatch(
236 ResourceStorage::class,
237 ResourceStorage::SIGNAL_PreFileCopy,
238 [
239 $event->getFile(),
240 $event->getFolder()
241 ]
242 );
243 }
244
245 public function onResourceStorageEmitPostFileCopySignal(AfterFileCopiedEvent $event): void
246 {
247 $this->signalSlotDispatcher->dispatch(
248 ResourceStorage::class,
249 ResourceStorage::SIGNAL_PostFileCopy,
250 [
251 $event->getFile(),
252 $event->getFolder()
253 ]
254 );
255 }
256
257 public function onResourceStorageEmitPreFileMoveSignal(BeforeFileMovedEvent $event): void
258 {
259 $this->signalSlotDispatcher->dispatch(
260 ResourceStorage::class,
261 ResourceStorage::SIGNAL_PreFileMove,
262 [
263 $event->getFile(),
264 $event->getFolder(),
265 $event->getTargetFileName()
266 ]
267 );
268 }
269
270 public function onResourceStorageEmitPostFileMoveSignal(AfterFileMovedEvent $event): void
271 {
272 $this->signalSlotDispatcher->dispatch(
273 ResourceStorage::class,
274 ResourceStorage::SIGNAL_PostFileMove,
275 [
276 $event->getFile(),
277 $event->getFolder(),
278 $event->getOriginalFolder()
279 ]
280 );
281 }
282
283 public function onResourceStorageEmitPreFileRenameSignal(BeforeFileRenamedEvent $event): void
284 {
285 $this->signalSlotDispatcher->dispatch(
286 ResourceStorage::class,
287 ResourceStorage::SIGNAL_PreFileRename,
288 [
289 $event->getFile(),
290 $event->getTargetFileName()
291 ]
292 );
293 }
294
295 public function onResourceStorageEmitPostFileRenameSignal(AfterFileRenamedEvent $event): void
296 {
297 $this->signalSlotDispatcher->dispatch(
298 ResourceStorage::class,
299 ResourceStorage::SIGNAL_PostFileRename,
300 [
301 $event->getFile(),
302 $event->getTargetFileName()
303 ]
304 );
305 }
306
307 public function onResourceStorageEmitPreFileReplaceSignal(BeforeFileReplacedEvent $event): void
308 {
309 $this->signalSlotDispatcher->dispatch(
310 ResourceStorage::class,
311 ResourceStorage::SIGNAL_PreFileReplace,
312 [
313 $event->getFile(),
314 $event->getLocalFilePath()
315 ]
316 );
317 }
318
319 public function onResourceStorageEmitPostFileReplaceSignal(AfterFileReplacedEvent $event): void
320 {
321 $this->signalSlotDispatcher->dispatch(
322 ResourceStorage::class,
323 ResourceStorage::SIGNAL_PostFileReplace,
324 [
325 $event->getFile(),
326 $event->getLocalFilePath()
327 ]
328 );
329 }
330
331 public function onResourceStorageEmitPreFileCreateSignal(BeforeFileCreatedEvent $event): void
332 {
333 $this->signalSlotDispatcher->dispatch(
334 ResourceStorage::class,
335 ResourceStorage::SIGNAL_PreFileCreate,
336 [
337 $event->getFileName(),
338 $event->getFolder()
339 ]
340 );
341 }
342
343 public function onResourceStorageEmitPostFileCreateSignal(AfterFileCreatedEvent $event): void
344 {
345 $this->signalSlotDispatcher->dispatch(
346 ResourceStorage::class,
347 ResourceStorage::SIGNAL_PostFileCreate,
348 [
349 $event->getFileName(),
350 $event->getFolder()
351 ]
352 );
353 }
354
355 public function onResourceStorageEmitPreFileDeleteSignal(BeforeFileDeletedEvent $event): void
356 {
357 $this->signalSlotDispatcher->dispatch(
358 ResourceStorage::class,
359 ResourceStorage::SIGNAL_PreFileDelete,
360 [
361 $event->getFile()
362 ]
363 );
364 }
365
366 public function onResourceStorageEmitPostFileDeleteSignal(AfterFileDeletedEvent $event): void
367 {
368 $this->signalSlotDispatcher->dispatch(
369 ResourceStorage::class,
370 ResourceStorage::SIGNAL_PostFileDelete,
371 [
372 $event->getFile()
373 ]
374 );
375 }
376
377 public function onResourceStorageEmitPreFileSetContentsSignal(BeforeFileContentsSetEvent $event): void
378 {
379 $this->signalSlotDispatcher->dispatch(
380 ResourceStorage::class,
381 ResourceStorage::SIGNAL_PreFileSetContents,
382 [
383 $event->getFile(),
384 $event->getContent()
385 ]
386 );
387 }
388
389 public function onResourceStorageEmitPostFileSetContentsSignal(AfterFileContentsSetEvent $event): void
390 {
391 $this->signalSlotDispatcher->dispatch(
392 ResourceStorage::class,
393 ResourceStorage::SIGNAL_PostFileSetContents,
394 [
395 $event->getFile(),
396 $event->getContent()
397 ]
398 );
399 }
400
401 public function onResourceStorageEmitPreFolderAddSignal(BeforeFolderAddedEvent $event): void
402 {
403 $this->signalSlotDispatcher->dispatch(
404 ResourceStorage::class,
405 ResourceStorage::SIGNAL_PreFolderAdd,
406 [
407 $event->getParentFolder(),
408 $event->getFolderName()
409 ]
410 );
411 }
412
413 public function onResourceStorageEmitPostFolderAddSignal(AfterFolderAddedEvent $event): void
414 {
415 $this->signalSlotDispatcher->dispatch(
416 ResourceStorage::class,
417 ResourceStorage::SIGNAL_PostFolderAdd,
418 [
419 $event->getFolder()
420 ]
421 );
422 }
423
424 public function onResourceStorageEmitPreFolderCopySignal(BeforeFolderCopiedEvent $event): void
425 {
426 $this->signalSlotDispatcher->dispatch(
427 ResourceStorage::class,
428 ResourceStorage::SIGNAL_PreFolderCopy,
429 [
430 $event->getFolder(),
431 $event->getTargetParentFolder(),
432 $event->getTargetFolderName()
433 ]
434 );
435 }
436
437 public function onResourceStorageEmitPostFolderCopySignal(AfterFolderCopiedEvent $event): void
438 {
439 $this->signalSlotDispatcher->dispatch(
440 ResourceStorage::class,
441 ResourceStorage::SIGNAL_PostFolderCopy,
442 [
443 $event->getFolder(),
444 $event->getTargetParentFolder(),
445 $event->getTargetFolder()->getName()
446 ]
447 );
448 }
449
450 public function onResourceStorageEmitPreFolderMoveSignal(BeforeFolderMovedEvent $event): void
451 {
452 $this->signalSlotDispatcher->dispatch(
453 ResourceStorage::class,
454 ResourceStorage::SIGNAL_PreFolderMove,
455 [
456 $event->getFolder(),
457 $event->getTargetParentFolder(),
458 $event->getTargetFolderName()
459 ]
460 );
461 }
462
463 public function onResourceStorageEmitPostFolderMoveSignal(AfterFolderMovedEvent $event): void
464 {
465 $this->signalSlotDispatcher->dispatch(
466 ResourceStorage::class,
467 ResourceStorage::SIGNAL_PostFolderMove,
468 [
469 $event->getFolder(),
470 $event->getTargetParentFolder(),
471 $event->getTargetFolder()->getName(),
472 $event->getFolder()->getParentFolder()
473 ]
474 );
475 }
476
477 public function onResourceStorageEmitPreFolderRenameSignal(BeforeFolderRenamedEvent $event): void
478 {
479 $this->signalSlotDispatcher->dispatch(
480 ResourceStorage::class,
481 ResourceStorage::SIGNAL_PreFolderRename,
482 [
483 $event->getFolder(),
484 $event->getTargetName()
485 ]
486 );
487 }
488
489 public function onResourceStorageEmitPostFolderRenameSignal(AfterFolderRenamedEvent $event): void
490 {
491 $this->signalSlotDispatcher->dispatch(
492 ResourceStorage::class,
493 ResourceStorage::SIGNAL_PostFolderRename,
494 [
495 $event->getFolder(),
496 $event->getFolder()->getName()
497 ]
498 );
499 }
500
501 public function onResourceStorageEmitPreFolderDeleteSignal(BeforeFolderDeletedEvent $event): void
502 {
503 $this->signalSlotDispatcher->dispatch(
504 ResourceStorage::class,
505 ResourceStorage::SIGNAL_PreFolderDelete,
506 [
507 $event->getFolder()
508 ]
509 );
510 }
511
512 public function onResourceStorageEmitPostFolderDeleteSignal(AfterFolderDeletedEvent $event): void
513 {
514 $this->signalSlotDispatcher->dispatch(
515 ResourceStorage::class,
516 ResourceStorage::SIGNAL_PostFolderDelete,
517 [
518 $event->getFolder()
519 ]
520 );
521 }
522
523 public function onResourceStorageEmitPreGeneratePublicUrlSignal(GeneratePublicUrlForResourceEvent $event): void
524 {
525 $publicUrl = $event->getPublicUrl();
526 $urlData = ['publicUrl' => &$publicUrl];
527 $this->signalSlotDispatcher->dispatch(
528 ResourceStorage::class,
529 ResourceStorage::SIGNAL_PreGeneratePublicUrl,
530 [
531 $event->getStorage(),
532 $event->getDriver(),
533 $event->getResource(),
534 $event->isRelativeToCurrentScript(),
535 $urlData
536 ]
537 );
538 $event->setPublicUrl($urlData['publicUrl']);
539 }
540 }