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
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
29
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
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
|
|
|
* @var TypoScriptConfiguration |
57
|
|
|
*/ |
58
|
|
|
protected $typoScriptConfiguration; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* ResultParserRegistry constructor. |
62
|
|
|
* @param TypoScriptConfiguration $configuration |
63
|
|
|
*/ |
64
|
|
|
public function __construct(TypoScriptConfiguration $configuration) |
65
|
|
|
{ |
66
|
|
|
$this->typoScriptConfiguration = $configuration; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get registered parser classNames |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getParsers() |
75
|
|
|
{ |
76
|
|
|
return $this->parsers; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Can be used to register a custom parser. |
81
|
|
|
* |
82
|
|
|
* @param string $className classname of the parser that should be used |
83
|
|
|
* @param int $priority higher priority means more important |
84
|
|
|
* @throws \InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
public function registerParser($className, $priority) |
87
|
|
|
{ |
88
|
|
|
// check if the class is available for TYPO3 before registering the driver |
89
|
|
|
if (!class_exists($className)) { |
90
|
|
|
throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1468863997); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!is_subclass_of($className, AbstractResultParser::class)) { |
|
|
|
|
94
|
|
|
throw new \InvalidArgumentException('Parser ' . $className . ' needs to implement the AbstractResultParser.', 1468863998); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (array_key_exists((int)$priority, $this->parsers)) { |
98
|
|
|
throw new \InvalidArgumentException('There is already a parser registerd with priority ' . (int)$priority . '.', 1468863999); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->parsers[(int)$priority] = $className; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return AbstractResultParser[] |
106
|
|
|
*/ |
107
|
|
|
public function getParserInstances() |
108
|
|
|
{ |
109
|
|
|
if ($this->parserInstances === null) { |
110
|
|
|
ksort($this->parsers); |
111
|
|
|
$orderedParsers = array_reverse($this->parsers); |
112
|
|
|
foreach ($orderedParsers as $className) { |
113
|
|
|
$this->parserInstances[] = $this->createParserInstance($className); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
return $this->parserInstances; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param SearchResultSet $resultSet |
121
|
|
|
* @return AbstractResultParser|null |
122
|
|
|
*/ |
123
|
|
|
public function getParser(SearchResultSet $resultSet) |
124
|
|
|
{ |
125
|
|
|
/** @var AbstractResultParser $parser */ |
126
|
|
|
foreach ($this->getParserInstances() as $parser) { |
127
|
|
|
if ($parser->canParse($resultSet)) { |
128
|
|
|
return $parser; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Create an instance of a certain parser class |
136
|
|
|
* |
137
|
|
|
* @return AbstractResultParser |
138
|
|
|
*/ |
139
|
|
|
protected function createParserInstance($className) |
140
|
|
|
{ |
141
|
|
|
return GeneralUtility::makeInstance($className, $this->typoScriptConfiguration); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|