[CLEANUP] Remove usage of self::class
[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 /**
23 * Class TextExtractorRegistry
24 *
25 */
26 class TextExtractorRegistry implements SingletonInterface {
27
28 /**
29 * Registered text extractor class names
30 *
31 * @var array
32 */
33 protected $textExtractorClasses = array();
34
35 /**
36 * Instance cache for text extractor classes
37 *
38 * @var TextExtractorInterface[]
39 */
40 protected $instances = array();
41
42
43 /**
44 * Returns an instance of this class
45 *
46 * @return TextExtractorRegistry
47 */
48 static public function getInstance() {
49 return GeneralUtility::makeInstance(__CLASS__);
50 }
51
52 /**
53 * Allows to register a text extractor class
54 *
55 * @param string $className
56 * @throws \InvalidArgumentException
57 */
58 public function registerTextExtractor($className) {
59 if (!class_exists($className)) {
60 throw new \InvalidArgumentException('The class "' . $className . '" you are trying to register is not available', 1422906893);
61 }
62
63 if (!in_array(TextExtractorInterface::class, class_implements($className), TRUE)) {
64 throw new \InvalidArgumentException($className . ' must implement interface' . TextExtractorInterface::class, 1422771427);
65 }
66
67 $this->textExtractorClasses[] = $className;
68 }
69
70 /**
71 * Get all registered text extractor instances
72 *
73 * @return TextExtractorInterface[]
74 */
75 public function getTextExtractorInstances() {
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 return GeneralUtility::makeInstance($className);
94 }
95
96 /**
97 * Checks whether any registered text extractor can deal with a given file
98 * and returns it.
99 *
100 * @param FileInterface $file
101 * @return NULL|TextExtractorInterface
102 */
103 public function getTextExtractor(FileInterface $file) {
104 foreach ($this->getTextExtractorInstances() as $textExtractor) {
105 if ($textExtractor->canExtractText($file)) {
106 return $textExtractor;
107 }
108 }
109
110 return NULL;
111 }
112
113 }