1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* class PostConfigStore|Firesphere\SolrSearch\Stores\PostConfigStore Post storage configuration for a core |
4
|
|
|
* |
5
|
|
|
* @package Firesphere\Solr\Search |
6
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
7
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Firesphere\SolrSearch\Stores; |
11
|
|
|
|
12
|
|
|
use Countable; |
13
|
|
|
use Firesphere\SolrSearch\Interfaces\ConfigStore; |
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use LogicException; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Solarium\Exception\RuntimeException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class PostConfigStore |
21
|
|
|
* |
22
|
|
|
* Store configurations for Solr through a POST to a remote system. |
23
|
|
|
* |
24
|
|
|
* @package Firesphere\Solr\Search |
25
|
|
|
*/ |
26
|
|
|
class PostConfigStore implements ConfigStore |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array file extensions |
30
|
|
|
*/ |
31
|
|
|
protected static $extensions = [ |
32
|
|
|
'xml' => 'text/xml', |
33
|
|
|
'txt' => 'text/plain', |
34
|
|
|
]; |
35
|
|
|
/** |
36
|
|
|
* @var array Store configuration |
37
|
|
|
*/ |
38
|
|
|
protected $config; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* FileConfigStore constructor. |
42
|
|
|
* |
43
|
|
|
* @param array $config |
44
|
|
|
*/ |
45
|
5 |
|
public function __construct($config) |
46
|
|
|
{ |
47
|
5 |
|
if (empty($config)) { |
48
|
1 |
|
throw new RuntimeException('No config defined', 1); |
49
|
|
|
} |
50
|
4 |
|
if (!isset($config['uri'])) { |
51
|
1 |
|
throw new LogicException('No URI endpoint defined'); |
52
|
|
|
} |
53
|
3 |
|
if (!isset($config['path'])) { |
54
|
1 |
|
$config['path'] = '/'; |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
if (substr($config['path'], -1) !== '/') { |
58
|
2 |
|
$config['path'] .= '/'; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
$this->config = $config; |
62
|
3 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Upload a file to Solr for index $index |
66
|
|
|
* |
67
|
|
|
* @param $index string - The name of an index (which is also used as the name of the Solr core for the index) |
68
|
|
|
* @param $file string - A path to a file to upload. The base name of the file will be used on the remote side |
69
|
|
|
* @param null|Countable $handler A handler used for testing, not to be used in Live environments |
70
|
|
|
* @return ResponseInterface |
71
|
|
|
*/ |
72
|
1 |
|
public function uploadFile($index, $file, $handler = null) |
73
|
|
|
{ |
74
|
1 |
|
return $this->uploadString($index, $file, file_get_contents($file), $handler); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Upload a file to Solr from a string for index $index |
79
|
|
|
* |
80
|
|
|
* @param string $index - The name of an index (which is also used as the name of the Solr core for the index) |
81
|
|
|
* @param string $filename - The base name of the file to use on the remote side |
82
|
|
|
* @param string $string - The content to upload |
83
|
|
|
* @param null|Countable $handler A handler used for testing, not to be used in Live environments |
84
|
|
|
* @return ResponseInterface |
85
|
|
|
*/ |
86
|
1 |
|
public function uploadString($index, $filename, $string, $handler = null) |
87
|
|
|
{ |
88
|
1 |
|
$info = pathinfo($filename); |
89
|
|
|
$clientConfig = [ |
90
|
1 |
|
'base_uri' => $this->config['uri'], |
91
|
|
|
'headers' => [ |
92
|
1 |
|
'Content-Type' => static::$extensions[$info['extension']] ?? 'text/plain', |
93
|
|
|
], |
94
|
|
|
]; |
95
|
|
|
// Add auth to the post if needed |
96
|
1 |
|
if (isset($this->config['auth'])) { |
97
|
1 |
|
$clientConfig['auth'] = [ |
98
|
1 |
|
$this->config['auth']['username'], |
99
|
1 |
|
$this->config['auth']['password'], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
// For testing purposes, set the handler. Usually not used |
103
|
1 |
|
if ($handler) { |
104
|
1 |
|
$clientConfig['handler'] = $handler; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$client = new Client($clientConfig); |
108
|
|
|
|
109
|
1 |
|
$path = sprintf('%s%s', $this->getPath(), $index); |
110
|
|
|
|
111
|
1 |
|
return $client->post($path, ['body' => $string]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the path to the config |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
3 |
|
public function getPath() |
120
|
|
|
{ |
121
|
3 |
|
return $this->config['path']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the instanceDir to tell Solr to use for index $index |
126
|
|
|
* |
127
|
|
|
* @param string|null $index string - The name of an index |
128
|
|
|
* (which is also used as the name of the Solr core for the index) |
129
|
|
|
* @return null|string |
130
|
|
|
*/ |
131
|
1 |
|
public function instanceDir($index) |
132
|
|
|
{ |
133
|
1 |
|
return $index; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|