[TASK] Removes extra empty lines
[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 */
25 class TextExtractorRegistry implements SingletonInterface
26 {
27 /**
28 * Registered text extractor class names
29 *
30 * @var array
31 */
32 protected $textExtractorClasses = array();
33
34 /**
35 * Instance cache for text extractor classes
36 *
37 * @var TextExtractorInterface[]
38 */
39 protected $instances = array();
40
41 /**
42 * Returns an instance of this class
43 *
44 * @return TextExtractorRegistry
45 */
46 public static function getInstance()
47 {
48 return GeneralUtility::makeInstance(__CLASS__);
49 }
50
51 /**
52 * Allows to register a text extractor class
53 *
54 * @param string $className
55 * @throws \InvalidArgumentException
56 */
57 public function registerTextExtractor($className)
58 {
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 {
77 if (empty($this->instances) && !empty($this->textExtractorClasses)) {
78 foreach ($this->textExtractorClasses as $className) {
79 $object = $this->createTextExtractorInstance($className);
80 $this->instances[] = $object;
81 }
82 }
83
84 return $this->instances;
85 }
86
87 /**
88 * Create an instance of a certain text extractor class
89 *
90 * @param string $className
91 * @return TextExtractorInterface
92 */
93 protected function createTextExtractorInstance($className)
94 {
95 return GeneralUtility::makeInstance($className);
96 }
97
98 /**
99 * Checks whether any registered text extractor can deal with a given file
100 * and returns it.
101 *
102 * @param FileInterface $file
103 * @return NULL|TextExtractorInterface
104 */
105 public function getTextExtractor(FileInterface $file)
106 {
107 foreach ($this->getTextExtractorInstances() as $textExtractor) {
108 if ($textExtractor->canExtractText($file)) {
109 return $textExtractor;
110 }
111 }
112
113 return null;
114 }
115 }