|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* class FileConfigStore|Firesphere\SolrSearch\Stores\FileConfigStore File 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 Firesphere\SolrSearch\Interfaces\ConfigStore; |
|
13
|
|
|
use SilverStripe\Control\Director; |
|
14
|
|
|
use Solarium\Exception\RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class FileConfigStore |
|
18
|
|
|
* Store the config in a file storage on the local file system, usually in project/.solr/indexname |
|
19
|
|
|
* |
|
20
|
|
|
* @package Firesphere\Solr\Search |
|
21
|
|
|
*/ |
|
22
|
|
|
class FileConfigStore implements ConfigStore |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array Configuration to use |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $config; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* FileConfigStore constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $config |
|
34
|
|
|
*/ |
|
35
|
39 |
|
public function __construct($config) |
|
36
|
|
|
{ |
|
37
|
39 |
|
if (empty($config)) { |
|
38
|
1 |
|
throw new RuntimeException('No valid config defined', 1); |
|
39
|
|
|
} |
|
40
|
|
|
// A relative folder should be rewritten to a writeable folder for the system |
|
41
|
38 |
|
if (strpos($config['path'], '/') !== 0) { |
|
42
|
37 |
|
$config['path'] = sprintf('%s/%s', Director::baseFolder(), $config['path']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
38 |
|
$this->config = $config; |
|
47
|
38 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Upload a file to the configuration store. Usually located in .solr/conf |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $index |
|
53
|
|
|
* @param string $file |
|
54
|
|
|
* @return void|null |
|
55
|
|
|
*/ |
|
56
|
36 |
|
public function uploadFile($index, $file) |
|
57
|
|
|
{ |
|
58
|
36 |
|
$targetDir = $this->getTargetDir($index); |
|
59
|
36 |
|
copy($file, sprintf('%s/%s', $targetDir, basename($file))); |
|
60
|
36 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the target dir for the file saving, create if it doesn't exist. |
|
64
|
|
|
* |
|
65
|
|
|
* @param $index |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
37 |
|
public function getTargetDir($index) |
|
69
|
|
|
{ |
|
70
|
37 |
|
$targetDir = sprintf('%s/conf', $this->instanceDir($index)); |
|
71
|
|
|
|
|
72
|
37 |
|
if (!is_dir($targetDir)) { |
|
73
|
2 |
|
$worked = @mkdir($targetDir, 0770, true); |
|
74
|
|
|
|
|
75
|
2 |
|
if (!$worked) { |
|
76
|
1 |
|
throw new RuntimeException( |
|
77
|
1 |
|
sprintf('Failed creating target directory %s, please check permissions', $targetDir) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
36 |
|
return $targetDir; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Path to the store location |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed|string |
|
89
|
|
|
*/ |
|
90
|
38 |
|
public function getPath() |
|
91
|
|
|
{ |
|
92
|
38 |
|
return $this->config['path']; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Upload/load a file in to the storage |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $index |
|
99
|
|
|
* @param string $filename |
|
100
|
|
|
* @param string $string |
|
101
|
|
|
* @return void|null |
|
102
|
|
|
*/ |
|
103
|
36 |
|
public function uploadString($index, $filename, $string) |
|
104
|
|
|
{ |
|
105
|
36 |
|
$targetDir = $this->getTargetDir($index); |
|
106
|
36 |
|
file_put_contents(sprintf('%s/%s', $targetDir, $filename), $string); |
|
107
|
36 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Location of the instance |
|
111
|
|
|
* |
|
112
|
|
|
* @param string|null $index |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
38 |
|
public function instanceDir($index) |
|
116
|
|
|
{ |
|
117
|
38 |
|
return sprintf('%s/%s', $this->getPath(), $index); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the config |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function getConfig(): array |
|
126
|
|
|
{ |
|
127
|
2 |
|
return $this->config; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Set the config |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $config |
|
134
|
|
|
* @return $this |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function setConfig(array $config): FileConfigStore |
|
137
|
|
|
{ |
|
138
|
1 |
|
$this->config = $config; |
|
139
|
|
|
|
|
140
|
1 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|