|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Stores; |
|
5
|
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Interfaces\ConfigStore; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Solarium\Exception\RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
class PostConfigStore implements ConfigStore |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $extensions = [ |
|
14
|
|
|
'xml' => 'text/xml', |
|
15
|
|
|
'txt' => 'text/plain', |
|
16
|
|
|
]; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* FileConfigStore constructor. |
|
24
|
|
|
* @param array $config |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($config) |
|
27
|
|
|
{ |
|
28
|
|
|
if (empty($config)) { |
|
29
|
|
|
throw new RuntimeException('No config defined', 1); |
|
30
|
|
|
} |
|
31
|
|
|
if (!isset($config['path'])) { |
|
32
|
|
|
$config['path'] = '/'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (substr($config['path'], -1) !== '/') { |
|
36
|
|
|
$config['path'] .= '/'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->config = $config; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Upload a file to Solr for index $index |
|
44
|
|
|
* @param $index string - The name of an index (which is also used as the name of the Solr core for the index) |
|
45
|
|
|
* @param $file string - A path to a file to upload. The base name of the file will be used on the remote side |
|
46
|
|
|
* @return ResponseInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public function uploadFile($index, $file) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->uploadString($index, $file, file_get_contents($file)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Upload a file to Solr from a string for index $index |
|
55
|
|
|
* @param string $index - The name of an index (which is also used as the name of the Solr core for the index) |
|
56
|
|
|
* @param string $filename - The base name of the file to use on the remote side |
|
57
|
|
|
* @param string $string - The content to upload |
|
58
|
|
|
* @return ResponseInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public function uploadString($index, $filename, $string) |
|
61
|
|
|
{ |
|
62
|
|
|
$info = pathinfo($filename); |
|
63
|
|
|
$clientConfig = [ |
|
64
|
|
|
'base_uri' => $this->config['uri'], |
|
65
|
|
|
'headers' => [ |
|
66
|
|
|
'Content-Type' => static::$extensions[$info['extension']] |
|
67
|
|
|
] |
|
68
|
|
|
]; |
|
69
|
|
|
// Add auth to the post if needed |
|
70
|
|
|
if (isset($this->config['auth'])) { |
|
71
|
|
|
$clientConfig['auth'] = [ |
|
72
|
|
|
$this->config['auth']['username'], |
|
73
|
|
|
$this->config['auth']['password'], |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$client = new Client($clientConfig); |
|
78
|
|
|
|
|
79
|
|
|
$path = sprintf('%s%s', $this->getPath(), $index); |
|
80
|
|
|
|
|
81
|
|
|
return $client->post($path, ['body' => $string]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getPath() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->config['path']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get the instanceDir to tell Solr to use for index $index |
|
94
|
|
|
* @param string|null $index string - The name of an index |
|
95
|
|
|
* (which is also used as the name of the Solr core for the index) |
|
96
|
|
|
* @return null|string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function instanceDir($index) |
|
99
|
|
|
{ |
|
100
|
|
|
return $index; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|