[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / TextExtraction / TextExtractorRegistry.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\TextExtraction;
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\Resource\FileInterface;
18 use TYPO3\CMS\Core\SingletonInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Class TextExtractorRegistry
23 */
24 class TextExtractorRegistry implements SingletonInterface
25 {
26 /**
27 * Registered text extractor class names
28 *
29 * @var array
30 */
31 protected $textExtractorClasses = [];
32
33 /**
34 * Instance cache for text extractor classes
35 *
36 * @var TextExtractorInterface[]
37 */
38 protected $instances = [];
39
40 /**
41 * Returns an instance of this class
42 *
43 * @return TextExtractorRegistry
44 */
45 public static function getInstance()
46 {
47 return GeneralUtility::makeInstance(__CLASS__);
48 }
49
50 /**
51 * Allows to register a text extractor class
52 *
53 * @param string $className
54 * @throws \InvalidArgumentException
55 */
56 public function registerTextExtractor($className)
57 {
58 if (!class_exists($className)) {
59 throw new \InvalidArgumentException('The class "' . $className . '" you are trying to register is not available', 1422906893);
60 }
61
62 if (!in_array(TextExtractorInterface::class, class_implements($className), true)) {
63 throw new \InvalidArgumentException($className . ' must implement interface' . TextExtractorInterface::class, 1422771427);
64 }
65
66 $this->textExtractorClasses[] = $className;
67 }
68
69 /**
70 * Get all registered text extractor instances
71 *
72 * @return TextExtractorInterface[]
73 */
74 public function getTextExtractorInstances()
75 {
76 if (empty($this->instances) && !empty($this->textExtractorClasses)) {
77 foreach ($this->textExtractorClasses as $className) {
78 $object = $this->createTextExtractorInstance($className);
79 $this->instances[] = $object;
80 }
81 }
82
83 return $this->instances;
84 }
85
86 /**
87 * Create an instance of a certain text extractor class
88 *
89 * @param string $className
90 * @return TextExtractorInterface
91 */
92 protected function createTextExtractorInstance($className)
93 {
94 return GeneralUtility::makeInstance($className);
95 }
96
97 /**
98 * Checks whether any registered text extractor can deal with a given file
99 * and returns it.
100 *
101 * @param FileInterface $file
102 * @return null|TextExtractorInterface
103 */
104 public function getTextExtractor(FileInterface $file)
105 {
106 foreach ($this->getTextExtractorInstances() as $textExtractor) {
107 if ($textExtractor->canExtractText($file)) {
108 return $textExtractor;
109 }
110 }
111
112 return null;
113 }
114 }