1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\IndexQueue; |
19
|
|
|
|
20
|
|
|
use RuntimeException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Index Queue Page Indexer response to provide data for requested actions. |
24
|
|
|
* |
25
|
|
|
* @author Ingo Renner <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class PageIndexerResponse |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Unique request ID. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected string $requestId; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The actions' results as action => result pairs. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected array $results = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Turns a JSON encoded result string back into its PHP representation. |
45
|
|
|
* |
46
|
|
|
* @param string $jsonEncodedResponse JSON encoded result string |
47
|
|
|
* @return array|bool An array of action => result pairs or FALSE if the response could not be decoded |
48
|
|
|
*/ |
49
|
5 |
|
public static function getResultsFromJson(string $jsonEncodedResponse) |
50
|
|
|
{ |
51
|
5 |
|
$responseData = json_decode($jsonEncodedResponse, true); |
52
|
|
|
|
53
|
5 |
|
if (is_array($responseData['actionResults'] ?? null)) { |
54
|
4 |
|
foreach ($responseData['actionResults'] as $action => $serializedActionResult) { |
55
|
4 |
|
$responseData['actionResults'][$action] = unserialize($serializedActionResult); |
56
|
|
|
} |
57
|
1 |
|
} elseif (is_null($responseData)) { |
58
|
1 |
|
$responseData = false; |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
return $responseData; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds an action's result. |
66
|
|
|
* |
67
|
|
|
* @param string $action The action name. |
68
|
|
|
* @param mixed $result The action's result. |
69
|
|
|
* @throws RuntimeException if $action is null |
70
|
|
|
*/ |
71
|
5 |
|
public function addActionResult(string $action, $result) |
72
|
|
|
{ |
73
|
5 |
|
$this->results[$action] = $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the complete set of results or a specific action's results. |
78
|
|
|
* |
79
|
|
|
* @param string|null $action Optional action name. |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
3 |
|
public function getActionResult(?string $action = null) |
83
|
|
|
{ |
84
|
3 |
|
if (empty($action)) { |
85
|
1 |
|
return $this->results; |
86
|
|
|
} |
87
|
2 |
|
return $this->results[$action]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Compiles the response's content so that it can be sent back to the |
92
|
|
|
* Index Queue page indexer. |
93
|
|
|
* |
94
|
|
|
* @return string The response content |
95
|
|
|
*/ |
96
|
|
|
public function getContent(): string |
97
|
|
|
{ |
98
|
|
|
return $this->toJson(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Converts the response's data to JSON. |
103
|
|
|
* |
104
|
|
|
* @return string JSON representation of the results. |
105
|
|
|
*/ |
106
|
|
|
protected function toJson(): string |
107
|
|
|
{ |
108
|
|
|
$serializedActionResults = []; |
109
|
|
|
|
110
|
|
|
foreach ($this->results as $action => $result) { |
111
|
|
|
$serializedActionResults[$action] = serialize($result); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$responseData = [ |
115
|
|
|
'requestId' => $this->requestId, |
116
|
|
|
'actionResults' => $serializedActionResults, |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
return json_encode($responseData); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Gets the Id of the request this response belongs to. |
124
|
|
|
* |
125
|
|
|
* @return string Request Id. |
126
|
|
|
*/ |
127
|
1 |
|
public function getRequestId(): string |
128
|
|
|
{ |
129
|
1 |
|
return $this->requestId; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the Id of the request this response belongs to. |
134
|
|
|
* |
135
|
|
|
* @param string $requestId Request Id. |
136
|
|
|
*/ |
137
|
3 |
|
public function setRequestId(string $requestId) |
138
|
|
|
{ |
139
|
3 |
|
$this->requestId = $requestId; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|