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