[TASK] Remove ext:dbal from installation steps
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Index / ExtractorRegistry.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Index;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\SingletonInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Registry for MetaData extraction Services
22 *
23 */
24 class ExtractorRegistry implements SingletonInterface
25 {
26 /**
27 * Registered ClassNames
28 * @var array
29 */
30 protected $extractors = [];
31
32 /**
33 * Instance Cache for Extractors
34 *
35 * @var ExtractorInterface[]
36 */
37 protected $instances = null;
38
39 /**
40 * Returns an instance of this class
41 *
42 * @return ExtractorRegistry
43 */
44 public static function getInstance()
45 {
46 return GeneralUtility::makeInstance(ExtractorRegistry::class);
47 }
48
49 /**
50 * Allows to register MetaData extraction to the FAL Indexer
51 *
52 * @param string $className
53 * @throws \InvalidArgumentException
54 */
55 public function registerExtractionService($className)
56 {
57 if (!class_exists($className)) {
58 throw new \InvalidArgumentException('The class "' . $className . '" you are registering is not available', 1422705270);
59 } elseif (!in_array(ExtractorInterface::class, class_implements($className))) {
60 throw new \InvalidArgumentException('The extractor needs to implement the ExtractorInterface', 1422705271);
61 } else {
62 $this->extractors[] = $className;
63 }
64 }
65
66 /**
67 * Get all registered extractors
68 *
69 * @return ExtractorInterface[]
70 */
71 public function getExtractors()
72 {
73 if ($this->instances === null) {
74 $this->instances = [];
75
76 $extractors = array_reverse($this->extractors);
77 foreach ($extractors as $className) {
78 /** @var ExtractorInterface $object */
79 $object = $this->createExtractorInstance($className);
80 $this->instances[] = $object;
81 }
82
83 if (count($this->instances) > 1) {
84 usort($this->instances, [$this, 'compareExtractorPriority']);
85 }
86 }
87 return $this->instances;
88 }
89
90 /**
91 * Get Extractors which work for a special driver
92 *
93 * @param string $driverType
94 * @return ExtractorInterface[]
95 */
96 public function getExtractorsWithDriverSupport($driverType)
97 {
98 $allExtractors = $this->getExtractors();
99
100 $filteredExtractors = [];
101 foreach ($allExtractors as $priority => $extractorObject) {
102 if (empty($extractorObject->getDriverRestrictions())) {
103 $filteredExtractors[$priority] = $extractorObject;
104 } elseif (in_array($driverType, $extractorObject->getDriverRestrictions())) {
105 $filteredExtractors[$priority] = $extractorObject;
106 }
107 }
108 return $filteredExtractors;
109 }
110
111 /**
112 * Compare the priority of two Extractor classes.
113 * Is used for sorting array of Extractor instances by priority.
114 * We want the result to be ordered from high to low so a higher
115 * priority comes before a lower.
116 *
117 * @param ExtractorInterface $extractorA
118 * @param ExtractorInterface $extractorB
119 * @return int -1 a > b, 0 a == b, 1 a < b
120 */
121 protected function compareExtractorPriority(ExtractorInterface $extractorA, ExtractorInterface $extractorB)
122 {
123 return $extractorB->getPriority() - $extractorA->getPriority();
124 }
125
126 /**
127 * Create an instance of a Metadata Extractor
128 *
129 * @param string $className
130 * @return ExtractorInterface
131 */
132 protected function createExtractorInstance($className)
133 {
134 return GeneralUtility::makeInstance($className);
135 }
136 }