|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Object; |
|
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\Extbase\Object\ObjectManagerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Abstract class to hold the logic to register and retrieve different classes |
|
22
|
|
|
* for a specific key. |
|
23
|
|
|
* |
|
24
|
|
|
* Can be used to retrieve different "strategies" for the same thing. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Timo Hund <[email protected]> |
|
27
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets |
|
28
|
|
|
*/ |
|
29
|
|
|
class AbstractClassRegistry implements SingletonInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Holds the mapping key => className |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $classMap = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Name for the default implementation |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $defaultClass = \stdClass::class; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var ObjectManagerInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $objectManager; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param ObjectManagerInterface $objectManager |
|
51
|
|
|
*/ |
|
52
|
|
|
public function injectObjectManager(ObjectManagerInterface $objectManager) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->objectManager = $objectManager; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Retrieves an instance for an registered type. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $type |
|
61
|
|
|
* @return object |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getInstance($type) |
|
64
|
|
|
{ |
|
65
|
|
|
$className = $this->resolveClassName($type); |
|
66
|
|
|
return $this->createInstance($className); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $type |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function resolveClassName($type) |
|
74
|
|
|
{ |
|
75
|
|
|
$className = $this->defaultClass; |
|
76
|
|
|
if (isset($this->classMap[$type])) { |
|
77
|
|
|
$className = $this->classMap[$type]; |
|
78
|
|
|
return $className; |
|
79
|
|
|
} |
|
80
|
|
|
return $className; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create an instance of a certain class |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $className |
|
87
|
|
|
* @return object |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function createInstance($className) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->objectManager->get($className); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Can be used to register an implementation in the classMap. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $className |
|
98
|
|
|
* @param string $type |
|
99
|
|
|
* @param string $requiredBaseClass |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function register($className, $type, $requiredBaseClass) { |
|
102
|
|
|
// check if the class is available for TYPO3 before registering the driver |
|
103
|
|
|
if (!class_exists($className)) { |
|
104
|
|
|
throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1462883324); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (!is_subclass_of($className, $requiredBaseClass)) { |
|
|
|
|
|
|
108
|
|
|
throw new \InvalidArgumentException('Parser ' . $className . ' needs to extend the ' . $requiredBaseClass . '.', 1462883325); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->classMap[$type] = $className; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|