1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2017 Timo Hund <[email protected]> |
9
|
|
|
* |
10
|
|
|
* All rights reserved |
11
|
|
|
* |
12
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
13
|
|
|
* free software; you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU General Public License as published by |
15
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
16
|
|
|
* (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* The GNU General Public License can be found at |
19
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
20
|
|
|
* |
21
|
|
|
* This script is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
27
|
|
|
***************************************************************/ |
28
|
|
|
|
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
30
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
31
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class ResultParserRegistry |
35
|
|
|
* |
36
|
|
|
* @author Frans Saris <[email protected]> |
37
|
|
|
* @author Timo Hund <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class ResultParserRegistry implements SingletonInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Array of available parser classNames |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $parsers = [ |
47
|
|
|
100 => DefaultResultParser::class, |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var AbstractResultParser[] |
52
|
|
|
*/ |
53
|
|
|
protected $parserInstances; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get registered parser classNames |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getParsers() |
61
|
|
|
{ |
62
|
|
|
return $this->parsers; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Can be used to register a custom parser. |
67
|
|
|
* |
68
|
|
|
* @param string $className classname of the parser that should be used |
69
|
|
|
* @param int $priority higher priority means more important |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
*/ |
72
|
|
|
public function registerParser($className, $priority) |
73
|
|
|
{ |
74
|
|
|
// check if the class is available for TYPO3 before registering the driver |
75
|
|
|
if (!class_exists($className)) { |
76
|
|
|
throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1468863997); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!is_subclass_of($className, AbstractResultParser::class)) { |
|
|
|
|
80
|
|
|
throw new \InvalidArgumentException('Parser ' . $className . ' needs to implement the AbstractResultParser.', 1468863998); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (array_key_exists((int)$priority, $this->parsers)) { |
84
|
|
|
throw new \InvalidArgumentException('There is already a parser registerd with priority ' . (int)$priority . '.', 1468863999); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->parsers[(int)$priority] = $className; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Method to check if a certain parser is allready registered |
92
|
|
|
* |
93
|
|
|
* @param string $className |
94
|
|
|
* @param int $priority |
95
|
|
|
* @return boolean |
96
|
|
|
*/ |
97
|
|
|
public function hasParser($className, $priority) |
98
|
|
|
{ |
99
|
|
|
if (empty($this->parsers[$priority])) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->parsers[$priority] === $className; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return AbstractResultParser[] |
108
|
|
|
*/ |
109
|
|
|
public function getParserInstances() |
110
|
|
|
{ |
111
|
|
|
if ($this->parserInstances === null) { |
112
|
|
|
ksort($this->parsers); |
113
|
|
|
$orderedParsers = array_reverse($this->parsers); |
114
|
|
|
foreach ($orderedParsers as $className) { |
115
|
|
|
$this->parserInstances[] = $this->createParserInstance($className); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return $this->parserInstances; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param SearchResultSet $resultSet |
123
|
|
|
* @return AbstractResultParser|null |
124
|
|
|
*/ |
125
|
|
|
public function getParser(SearchResultSet $resultSet) |
126
|
|
|
{ |
127
|
|
|
/** @var AbstractResultParser $parser */ |
128
|
|
|
foreach ($this->getParserInstances() as $parser) { |
129
|
|
|
if ($parser->canParse($resultSet)) { |
130
|
|
|
return $parser; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Create an instance of a certain parser class |
138
|
|
|
* |
139
|
|
|
* @return AbstractResultParser |
140
|
|
|
*/ |
141
|
|
|
protected function createParserInstance($className) |
142
|
|
|
{ |
143
|
|
|
return GeneralUtility::makeInstance($className); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|