|
1
|
|
|
<?php |
|
2
|
|
|
namespace Shlinkio\Shlink\CLI\Model; |
|
3
|
|
|
|
|
4
|
|
|
use Zend\Stdlib\ArraySerializableInterface; |
|
5
|
|
|
|
|
6
|
|
|
final class CustomizableAppConfig implements ArraySerializableInterface |
|
7
|
|
|
{ |
|
8
|
|
|
const SQLITE_DB_PATH = 'data/database.sqlite'; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
private $database; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $urlShortener; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $language; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $app; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $importedInstallationPath; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
4 |
|
public function getDatabase() |
|
35
|
|
|
{ |
|
36
|
4 |
|
return $this->database; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $database |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function setDatabase(array $database) |
|
44
|
|
|
{ |
|
45
|
4 |
|
$this->database = $database; |
|
46
|
4 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function hasDatabase() |
|
53
|
|
|
{ |
|
54
|
4 |
|
return ! empty($this->database); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function getUrlShortener() |
|
61
|
|
|
{ |
|
62
|
3 |
|
return $this->urlShortener; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param array $urlShortener |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function setUrlShortener(array $urlShortener) |
|
70
|
|
|
{ |
|
71
|
3 |
|
$this->urlShortener = $urlShortener; |
|
72
|
3 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function hasUrlShortener() |
|
79
|
|
|
{ |
|
80
|
3 |
|
return ! empty($this->urlShortener); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function getLanguage() |
|
87
|
|
|
{ |
|
88
|
3 |
|
return $this->language; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param array $language |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function setLanguage(array $language) |
|
96
|
|
|
{ |
|
97
|
3 |
|
$this->language = $language; |
|
98
|
3 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function hasLanguage() |
|
105
|
|
|
{ |
|
106
|
3 |
|
return ! empty($this->language); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function getApp() |
|
113
|
|
|
{ |
|
114
|
3 |
|
return $this->app; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param array $app |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
3 |
|
public function setApp(array $app) |
|
122
|
|
|
{ |
|
123
|
3 |
|
$this->app = $app; |
|
124
|
3 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
3 |
|
public function hasApp() |
|
131
|
|
|
{ |
|
132
|
3 |
|
return ! empty($this->app); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function getImportedInstallationPath() |
|
139
|
|
|
{ |
|
140
|
2 |
|
return $this->importedInstallationPath; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $importedInstallationPath |
|
145
|
|
|
* @return $this|self |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function setImportedInstallationPath($importedInstallationPath) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->importedInstallationPath = $importedInstallationPath; |
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
public function hasImportedInstallationPath() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->importedInstallationPath !== null; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Exchange internal values from provided array |
|
163
|
|
|
* |
|
164
|
|
|
* @param array $array |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function exchangeArray(array $array) |
|
168
|
|
|
{ |
|
169
|
1 |
|
if (isset($array['app_options'], $array['app_options']['secret_key'])) { |
|
170
|
|
|
$this->setApp([ |
|
171
|
|
|
'SECRET' => $array['app_options']['secret_key'], |
|
172
|
|
|
]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
1 |
|
if (isset($array['entity_manager'], $array['entity_manager']['connection'])) { |
|
176
|
|
|
$this->deserializeDatabase($array['entity_manager']['connection']); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
if (isset($array['translator'], $array['translator']['locale'], $array['cli'], $array['cli']['locale'])) { |
|
180
|
|
|
$this->setLanguage([ |
|
181
|
|
|
'DEFAULT' => $array['translator']['locale'], |
|
182
|
|
|
'CLI' => $array['cli']['locale'], |
|
183
|
|
|
]); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
1 |
|
if (isset($array['url_shortener'])) { |
|
187
|
|
|
$urlShortener = $array['url_shortener']; |
|
188
|
|
|
$this->setUrlShortener([ |
|
189
|
|
|
'SCHEMA' => $urlShortener['domain']['schema'], |
|
190
|
|
|
'HOSTNAME' => $urlShortener['domain']['hostname'], |
|
191
|
|
|
'CHARS' => $urlShortener['shortcode_chars'], |
|
192
|
|
|
'VALIDATE_URL' => $urlShortener['validate_url'], |
|
193
|
|
|
]); |
|
194
|
|
|
} |
|
195
|
1 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
private function deserializeDatabase(array $conn) |
|
198
|
|
|
{ |
|
199
|
|
|
if (! isset($conn['driver'])) { |
|
200
|
|
|
return; |
|
201
|
|
|
} |
|
202
|
|
|
$driver = $conn['driver']; |
|
203
|
|
|
|
|
204
|
|
|
$params = ['DRIVER' => $driver]; |
|
205
|
|
|
if ($driver !== 'pdo_sqlite') { |
|
206
|
|
|
$params['USER'] = $conn['user']; |
|
207
|
|
|
$params['PASSWORD'] = $conn['password']; |
|
208
|
|
|
$params['NAME'] = $conn['dbname']; |
|
209
|
|
|
$params['HOST'] = $conn['host']; |
|
210
|
|
|
$params['PORT'] = $conn['port']; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$this->setDatabase($params); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Return an array representation of the object |
|
218
|
|
|
* |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
3 |
|
public function getArrayCopy() |
|
222
|
|
|
{ |
|
223
|
|
|
$config = [ |
|
224
|
|
|
'app_options' => [ |
|
225
|
3 |
|
'secret_key' => $this->app['SECRET'], |
|
226
|
|
|
], |
|
227
|
|
|
'entity_manager' => [ |
|
228
|
|
|
'connection' => [ |
|
229
|
3 |
|
'driver' => $this->database['DRIVER'], |
|
230
|
|
|
], |
|
231
|
|
|
], |
|
232
|
|
|
'translator' => [ |
|
233
|
3 |
|
'locale' => $this->language['DEFAULT'], |
|
234
|
|
|
], |
|
235
|
|
|
'cli' => [ |
|
236
|
3 |
|
'locale' => $this->language['CLI'], |
|
237
|
|
|
], |
|
238
|
|
|
'url_shortener' => [ |
|
239
|
|
|
'domain' => [ |
|
240
|
3 |
|
'schema' => $this->urlShortener['SCHEMA'], |
|
241
|
3 |
|
'hostname' => $this->urlShortener['HOSTNAME'], |
|
242
|
|
|
], |
|
243
|
3 |
|
'shortcode_chars' => $this->urlShortener['CHARS'], |
|
244
|
3 |
|
'validate_url' => $this->urlShortener['VALIDATE_URL'] |
|
245
|
|
|
], |
|
246
|
|
|
]; |
|
247
|
|
|
|
|
248
|
|
|
// Build dynamic database config based on selected driver |
|
249
|
3 |
|
if ($this->database['DRIVER'] === 'pdo_sqlite') { |
|
250
|
|
|
$config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH; |
|
251
|
|
|
} else { |
|
252
|
3 |
|
$config['entity_manager']['connection']['user'] = $this->database['USER']; |
|
253
|
3 |
|
$config['entity_manager']['connection']['password'] = $this->database['PASSWORD']; |
|
254
|
3 |
|
$config['entity_manager']['connection']['dbname'] = $this->database['NAME']; |
|
255
|
3 |
|
$config['entity_manager']['connection']['host'] = $this->database['HOST']; |
|
256
|
3 |
|
$config['entity_manager']['connection']['port'] = $this->database['PORT']; |
|
257
|
|
|
|
|
258
|
3 |
|
if ($this->database['DRIVER'] === 'pdo_mysql') { |
|
259
|
|
|
$config['entity_manager']['connection']['driverOptions'] = [ |
|
260
|
|
|
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', |
|
261
|
|
|
]; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
3 |
|
return $config; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|