TYPO3 CMS  TYPO3_7-6
TextExtractorRegistry.php
Go to the documentation of this file.
1 <?php
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 
20 
26 {
32  protected $textExtractorClasses = [];
33 
39  protected $instances = [];
40 
46  public static function getInstance()
47  {
48  return GeneralUtility::makeInstance(__CLASS__);
49  }
50 
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 
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 
93  protected function createTextExtractorInstance($className)
94  {
95  return GeneralUtility::makeInstance($className);
96  }
97 
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 }