[TASK] Update php-cs-fixer to 2.5.0
[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 class ExtractorRegistry implements SingletonInterface
24 {
25 /**
26 * Registered ClassNames
27 * @var array
28 */
29 protected $extractors = [];
30
31 /**
32 * Instance Cache for Extractors
33 *
34 * @var ExtractorInterface[]
35 */
36 protected $instances = null;
37
38 /**
39 * Returns an instance of this class
40 *
41 * @return ExtractorRegistry
42 */
43 public static function getInstance()
44 {
45 return GeneralUtility::makeInstance(self::class);
46 }
47
48 /**
49 * Allows to register MetaData extraction to the FAL Indexer
50 *
51 * @param string $className
52 * @throws \InvalidArgumentException
53 */
54 public function registerExtractionService($className)
55 {
56 if (!class_exists($className)) {
57 throw new \InvalidArgumentException('The class "' . $className . '" you are registering is not available', 1422705270);
58 }
59 if (!in_array(ExtractorInterface::class, class_implements($className))) {
60 throw new \InvalidArgumentException('The extractor needs to implement the ExtractorInterface', 1422705271);
61 }
62 $this->extractors[] = $className;
63 }
64
65 /**
66 * Get all registered extractors
67 *
68 * @return ExtractorInterface[]
69 */
70 public function getExtractors()
71 {
72 if ($this->instances === null) {
73 $this->instances = [];
74
75 $extractors = array_reverse($this->extractors);
76 foreach ($extractors as $className) {
77 /** @var ExtractorInterface $object */
78 $object = $this->createExtractorInstance($className);
79 $this->instances[] = $object;
80 }
81
82 if (count($this->instances) > 1) {
83 usort($this->instances, [$this, 'compareExtractorPriority']);
84 }
85 }
86 return $this->instances;
87 }
88
89 /**
90 * Get Extractors which work for a special driver
91 *
92 * @param string $driverType
93 * @return ExtractorInterface[]
94 */
95 public function getExtractorsWithDriverSupport($driverType)
96 {
97 $allExtractors = $this->getExtractors();
98
99 $filteredExtractors = [];
100 foreach ($allExtractors as $priority => $extractorObject) {
101 if (empty($extractorObject->getDriverRestrictions())) {
102 $filteredExtractors[$priority] = $extractorObject;
103 } elseif (in_array($driverType, $extractorObject->getDriverRestrictions())) {
104 $filteredExtractors[$priority] = $extractorObject;
105 }
106 }
107 return $filteredExtractors;
108 }
109
110 /**
111 * Compare the priority of two Extractor classes.
112 * Is used for sorting array of Extractor instances by priority.
113 * We want the result to be ordered from high to low so a higher
114 * priority comes before a lower.
115 *
116 * @param ExtractorInterface $extractorA
117 * @param ExtractorInterface $extractorB
118 * @return int -1 a > b, 0 a == b, 1 a < b
119 */
120 protected function compareExtractorPriority(ExtractorInterface $extractorA, ExtractorInterface $extractorB)
121 {
122 return $extractorB->getPriority() - $extractorA->getPriority();
123 }
124
125 /**
126 * Create an instance of a Metadata Extractor
127 *
128 * @param string $className
129 * @return ExtractorInterface
130 */
131 protected function createExtractorInstance($className)
132 {
133 return GeneralUtility::makeInstance($className);
134 }
135 }