‪TYPO3CMS  11.5
ExtractorService.php
Go to the documentation of this file.
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 
19 
24 
29 {
33  private ‪$extractionServices;
34 
39  public function ‪extractMetaData(‪File $fileObject): array
40  {
41  $newMetaData = [];
42  // Loop through available extractors and fetch metadata for the given file.
43  foreach ($this->‪getExtractionServices($fileObject->‪getStorage()->‪getDriverType()) as $service) {
44  foreach ($service ?? [] as $extractorService) {
45  if ($this->‪isFileTypeSupportedByExtractor($fileObject, $extractorService) &&
46  $extractorService->canProcess($fileObject)) {
47  $newMetaData[$extractorService->getPriority()] = $extractorService->extractMetaData($fileObject, $newMetaData);
48  }
49  }
50  }
51  // Sort metadata by priority so that merging happens in order of precedence.
52  ksort($newMetaData);
53  // Merge the collected metadata.
54  $metaData = [[]];
55  foreach ($newMetaData as $data) {
56  $metaData[] = $data;
57  }
58  return array_filter(array_merge(...$metaData));
59  }
60 
67  protected function ‪getExtractionServices(string $driverType): array
68  {
69  if (empty($this->extractionServices[$driverType])) {
70  $this->extractionServices[$driverType] = $this->‪getExtractorRegistry()->‪getExtractorsWithDriverSupport($driverType);
71  }
72  return $this->extractionServices[$driverType];
73  }
74 
82  private function ‪isFileTypeSupportedByExtractor(‪File $file, ‪ExtractorInterface $extractor): bool
83  {
84  $isSupported = true;
85  $fileTypeRestrictions = $extractor->‪getFileTypeRestrictions();
86  if (!empty($fileTypeRestrictions) && !in_array($file->‪getType(), $fileTypeRestrictions, true)) {
87  $isSupported = false;
88  }
89  return $isSupported;
90  }
91 
97  protected function ‪getExtractorRegistry(): ‪ExtractorRegistry
98  {
99  return GeneralUtility::makeInstance(ExtractorRegistry::class);
100  }
101 }
‪TYPO3\CMS\Core\Resource\Index\ExtractorInterface\getFileTypeRestrictions
‪array getFileTypeRestrictions()
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\extractMetaData
‪array extractMetaData(File $fileObject)
Definition: ExtractorService.php:38
‪TYPO3\CMS\Core\Resource\Index\ExtractorRegistry\getExtractorsWithDriverSupport
‪ExtractorInterface[] getExtractorsWithDriverSupport($driverType)
Definition: ExtractorRegistry.php:95
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:291
‪TYPO3\CMS\Core\Resource\ResourceStorage\getDriverType
‪string getDriverType()
Definition: ResourceStorage.php:2904
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\getExtractionServices
‪ExtractorInterface[] getExtractionServices(string $driverType)
Definition: ExtractorService.php:66
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\isFileTypeSupportedByExtractor
‪bool isFileTypeSupportedByExtractor(File $file, ExtractorInterface $extractor)
Definition: ExtractorService.php:81
‪TYPO3\CMS\Core\Resource\Index\ExtractorInterface
Definition: ExtractorInterface.php:25
‪TYPO3\CMS\Core\Resource\Index\ExtractorRegistry
Definition: ExtractorRegistry.php:25
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\$extractionServices
‪ExtractorInterface[][] $extractionServices
Definition: ExtractorService.php:32
‪TYPO3\CMS\Core\Resource\Service
Definition: ConfigurationService.php:18
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\Service\ExtractorService
Definition: ExtractorService.php:29
‪TYPO3\CMS\Core\Resource\Service\ExtractorService\getExtractorRegistry
‪ExtractorRegistry getExtractorRegistry()
Definition: ExtractorService.php:96
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:395