1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EasyDictionary; |
6
|
|
|
|
7
|
|
|
use EasyDictionary\Exception\InvalidConfigurationException; |
8
|
|
|
use EasyDictionary\Exception\RuntimeException; |
9
|
|
|
use Psr\SimpleCache\CacheInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Manager |
13
|
|
|
* @package EasyDictionary |
|
|
|
|
14
|
|
|
*/ |
|
|
|
|
15
|
|
|
class Manager |
16
|
|
|
{ |
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
public $defaultDictionary = 'EasyDictionary\Dictionary\Simple'; |
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function getDefaultDictionary(): string |
26
|
|
|
{ |
27
|
|
|
return $this->defaultDictionary; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @param string $defaultDictionary |
|
|
|
|
32
|
|
|
* @return $this |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
public function setDefaultDictionary(string $defaultDictionary) |
35
|
|
|
{ |
36
|
|
|
$this->defaultDictionary = $defaultDictionary; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getDefaultDataProvider(): string |
45
|
|
|
{ |
46
|
|
|
return $this->defaultDataProvider; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @param string $defaultDataProvider |
|
|
|
|
51
|
|
|
* @return $this |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
public function setDefaultDataProvider(string $defaultDataProvider) |
54
|
|
|
{ |
55
|
|
|
$this->defaultDataProvider = $defaultDataProvider; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $defaultDataProvider = 'EasyDictionary\DataProvider\Simple'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* [ |
67
|
|
|
* "caches" => [ |
68
|
|
|
* "cache_name_2" => callable |
69
|
|
|
* ] |
70
|
|
|
* ["defaultView" => function (...$data):\Generator {},] |
71
|
|
|
* "dictionaries" => [ |
72
|
|
|
* "dictionary_name" => [ |
73
|
|
|
* ["class" => EasyDictionary\DictionaryInterface,] |
74
|
|
|
* ["dataType" => "flat|array"] |
75
|
|
|
* ["cache" => "cache_name"] |
76
|
|
|
* ["cacheTTL" => 60,] |
77
|
|
|
* "data" => [ |
78
|
|
|
* ["class" => EasyDictionary\DataProviderInterface,] |
79
|
|
|
* [data provider arguments] |
80
|
|
|
* ], |
81
|
|
|
* ["view" => function (...$data):\Generator {},] |
82
|
|
|
* ["searchFields" => [<string>, ...]] |
83
|
|
|
* ], |
84
|
|
|
* |
85
|
|
|
* ... |
86
|
|
|
* ] |
87
|
|
|
* ] |
88
|
|
|
* |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
public $config = []; |
92
|
|
|
|
93
|
|
|
/** |
|
|
|
|
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
protected $dictionaries = []; |
97
|
|
|
|
98
|
|
|
/** |
|
|
|
|
99
|
|
|
* @param string $name |
|
|
|
|
100
|
|
|
* @return DictionaryInterface |
|
|
|
|
101
|
|
|
* @throws InvalidConfigurationException |
|
|
|
|
102
|
|
|
* @throws RuntimeException |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
public function get(string $name): DictionaryInterface |
105
|
|
|
{ |
106
|
|
|
if (!isset($this->dictionaries[$name])) { |
107
|
|
|
$config = $this->getConfig()['dictionaries'][$name] ?? null; |
108
|
|
|
|
109
|
|
|
if (!$config) { |
110
|
|
|
throw new RuntimeException(sprintf('Dictionary with key "%s" not found', $name)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->add($this->create($name, $config)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->dictionaries[$name]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
|
|
|
|
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getConfig(): array |
123
|
|
|
{ |
124
|
|
|
return $this->config; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
|
|
|
|
128
|
|
|
* @param array $config |
|
|
|
|
129
|
|
|
* @return $this |
|
|
|
|
130
|
|
|
*/ |
131
|
|
|
public function setConfig(array $config) |
132
|
|
|
{ |
133
|
|
|
$this->config = $config; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
|
|
|
|
139
|
|
|
* @param DictionaryInterface $dictionary |
|
|
|
|
140
|
|
|
* @return $this |
|
|
|
|
141
|
|
|
* @throws RuntimeException |
|
|
|
|
142
|
|
|
*/ |
143
|
|
|
public function add(DictionaryInterface $dictionary) |
144
|
|
|
{ |
145
|
|
|
$name = $dictionary->getName(); |
146
|
|
|
|
147
|
|
|
if (isset($this->dictionaries[$name])) { |
148
|
|
|
throw new RuntimeException(sprintf('The dictionary with key "%s" already exists', $name)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->dictionaries[$name] = $dictionary; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
|
|
|
|
157
|
|
|
* @param $name |
|
|
|
|
158
|
|
|
* @param $dictionaryConfig |
|
|
|
|
159
|
|
|
* @return DictionaryInterface |
|
|
|
|
160
|
|
|
* @throws InvalidConfigurationException |
|
|
|
|
161
|
|
|
*/ |
162
|
|
|
protected function create(string $name, array $dictionaryConfig): DictionaryInterface |
163
|
|
|
{ |
164
|
|
|
$dictionaryClass = $dictionaryConfig['class'] ?? $this->getDefaultDictionary(); |
165
|
|
|
if (!class_exists($dictionaryClass)) { |
166
|
|
|
throw new InvalidConfigurationException(sprintf('Class "%s" not found', $dictionaryClass)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$dataProviderClass = $dictionaryConfig['data']['class'] ?? $this->getDefaultDataProvider(); |
170
|
|
|
if (!class_exists($dataProviderClass)) { |
171
|
|
|
throw new InvalidConfigurationException(sprintf('Class "%s" not found', $dataProviderClass)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** @var DataProviderInterface $dataProvider */ |
|
|
|
|
175
|
|
|
$dataProvider = new $dataProviderClass($dictionaryConfig['data']); |
176
|
|
|
|
177
|
|
|
/** @var DictionaryInterface $dictionary */ |
|
|
|
|
178
|
|
|
$dictionary = new $dictionaryClass($name); |
179
|
|
|
$dictionary->setDataProvider($dataProvider); |
180
|
|
|
$dictionary->setDefaultView($dictionaryConfig['view'] ?? ($this->getConfig()['defaultView'] ?? null)); |
181
|
|
|
$dictionary->setSearchFields($dictionaryConfig['searchFields'] ?? []); |
182
|
|
|
|
183
|
|
|
if (isset($dictionaryConfig['dataType'])) { |
184
|
|
|
$dictionary->setDataValueType($dictionaryConfig['dataType']); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (isset($dictionaryConfig['cache'])) { |
188
|
|
|
$caches = $this->getConfig()['caches'] ?? []; |
189
|
|
|
|
190
|
|
View Code Duplication |
if (!isset($caches[$dictionaryConfig['cache']])) { |
|
|
|
|
191
|
|
|
throw new InvalidConfigurationException(sprintf('Cache "%s" not found', $dictionaryConfig['cache'])); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
View Code Duplication |
if (!is_callable($caches[$dictionaryConfig['cache']])) { |
|
|
|
|
195
|
|
|
throw new InvalidConfigurationException(sprintf('Cache "%s" not callable', $dictionaryConfig['cache'])); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$cache = $caches[$dictionaryConfig['cache']](); |
199
|
|
|
|
200
|
|
|
if (!($cache instanceof CacheInterface)) { |
201
|
|
|
throw new InvalidConfigurationException( |
202
|
|
|
sprintf('Object with class "%s" does not support expected interface', get_class($cache)) |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$dictionary->setCache($cache, $dictionaryConfig['cacheTTL'] ?? 60); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $dictionary; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|