-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasConfiguration.php
More file actions
378 lines (300 loc) · 8.65 KB
/
HasConfiguration.php
File metadata and controls
378 lines (300 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
namespace Cybex\Protector\Traits;
use Cybex\Protector\Classes\Metadata\Providers\DatabaseMetadataProvider;
use Cybex\Protector\Exceptions\InvalidConnectionException;
use Illuminate\Support\Collection;
trait HasConfiguration
{
/**
* Cache for the current connection-name.
*/
protected string $connectionName;
/**
* Cache for the current connection-configuration.
*/
protected mixed $connectionConfig;
/**
* Defines whether dumps should include a DB creation statement.
*/
protected bool $createDb = true;
/**
* Defines whether existing databases should be dropped before importing a dump.
* Only works if used together with the $createDb option.
* (PostgreSQL only, controls the --clean flag)
*/
protected bool $dropDb = true;
/**
* The dump endpoint URL.
*/
protected string $dumpEndpointUrl = '';
/**
* The Protector Auth Token.
*/
protected string $authToken = '';
/**
* The Protector Private Key.
*/
protected string $privateKey = '';
/**
* The maximum packet length for the dump.
*/
protected string $maxPacketLength;
/**
* If set to false, the --no-tablespaces dump option will be used.
*/
protected bool $tablespaces = true;
/**
* Specifies whether comments should be added to the dump file.
*/
protected bool $dumpComments = true;
/**
* If false, no SET NAMES statements will be written to the dump.
*/
protected bool $dumpCharsets = true;
/**
* Specifies whether table data should be dumped.
*/
protected bool $dumpData = true;
/**
* If true, the auto-increment state will be stripped from the dump.
*/
protected bool $removeAutoIncrementingState = false;
/**
* The metadata provider classes for the dump metadata.
*/
protected array $metadataProviders;
/**
* Sets the auth token for Laravel Sanctum authentication.
*/
public function withAuthToken(string $authToken): static
{
$this->authToken = $authToken;
return $this;
}
public function withAutoIncrementingState(): static
{
$this->removeAutoIncrementingState = false;
return $this;
}
public function withoutAutoIncrementingState(): static
{
$this->removeAutoIncrementingState = true;
return $this;
}
public function withCharsets(): static
{
$this->dumpCharsets = true;
return $this;
}
public function withoutCharsets(): static
{
$this->dumpCharsets = false;
return $this;
}
public function withComments(): static
{
$this->dumpComments = true;
return $this;
}
public function withoutComments(): static
{
$this->dumpComments = false;
return $this;
}
public function withData(): static
{
$this->dumpData = true;
return $this;
}
public function withoutData(): static
{
$this->dumpData = false;
return $this;
}
/**
* @throws InvalidConnectionException
*/
public function withConnectionName(?string $connectionName = null): static
{
$this->connectionName = $connectionName ?? config('database.default');
if (($this->connectionConfig = $this->getDatabaseConfig()) === false) {
throw new InvalidConnectionException('Invalid database configuration');
}
return $this;
}
public function withCreateDb(): static
{
$this->createDb = true;
return $this;
}
public function withoutCreateDb(): static
{
$this->createDb = false;
return $this;
}
/**
* Defines that existing databases should be dropped before importing a dump.
* Only works if used together with the $createDb option.
* (PostgreSQL only, controls the --clean flag)
*/
public function withDropDb(): static
{
$this->dropDb = true;
return $this;
}
/**
* Defines that existing databases should not be dropped before importing a dump.
* Only works if used together with the $createDb option
* (PostgreSQL only, controls the --clean flag)
*/
public function withoutDropDb(): static
{
$this->dropDb = false;
return $this;
}
public function withTablespaces(): static
{
$this->tablespaces = true;
return $this;
}
public function withoutTablespaces(): static
{
$this->tablespaces = false;
return $this;
}
public function withMaxPacketLength(string $maxPacketLength): static
{
$this->maxPacketLength = $maxPacketLength;
return $this;
}
public function withDefaultMaxPacketLength(): static
{
$this->maxPacketLength = config('protector.dump.maxPacketLength');
return $this;
}
/**
* Sets the dump endpoint URL.
*/
public function withDumpEndpointUrl(string $dumpEndpointUrl): static
{
$this->dumpEndpointUrl = $dumpEndpointUrl;
return $this;
}
public function withPrivateKey(string $privateKey): static
{
$this->privateKey = $privateKey;
return $this;
}
public function withMetadataProviders(array $metadataProviders): static
{
$this->metadataProviders = $metadataProviders;
return $this;
}
/**
* Retrieves the auth token for Laravel Sanctum authentication.
*/
protected function getAuthToken(): string
{
return $this->authToken ?: $this->getConfigValueForKey('client.authToken');
}
/**
* Gets the name of the .env key for the auth token.
*/
public function getAuthTokenKeyName(): string
{
return 'PROTECTOR_CLIENT_AUTH_TOKEN';
}
/**
* Returns the database config for the given connection.
*/
protected function getDatabaseConfig(): mixed
{
return config(sprintf('database.connections.%s', $this->connectionName), false);
}
/**
* Returns the database name specified in the connectionConfig array.
*/
public function getDatabaseName(): string
{
return $this->connectionConfig['database'];
}
/**
* Returns the maximum packet length specified in the config.
* The option 'max_allowed_packet' sets an upper limit on the size of any single message between the MySQL server and clients.
* This has to be set up on the client (here) and the MySQL server.
* This is not applicable to PostgreSQL.
*/
public function getMaxPacketLength(): string
{
return $this->maxPacketLength;
}
/**
* Gets the name of the .env key for the Protector private key.
*/
public function getPrivateKeyName(): string
{
return 'PROTECTOR_CLIENT_PRIVATE_KEY';
}
/**
* Retrieves the private key for Sodium encryption.
*/
protected function getPrivateKey(): string
{
return $this->privateKey ?: $this->getConfigValueForKey('client.privateKey');
}
public function getConnectionName(): string
{
return $this->connectionName;
}
/**
* Retrieves the URL of the dump endpoint.
*/
public function getDumpEndpointUrl(): string
{
return $this->dumpEndpointUrl ?: $this->getConfigValueForKey('client.dumpEndpointUrl');
}
/**
* Gets the name of the .env key for the Protector dump endpoint URL.
*/
public function getDumpEndpointUrlKeyName(): string
{
return 'PROTECTOR_CLIENT_DUMP_ENDPOINT_URL';
}
/**
* The metadata provider classes can be configured on the protector instance, else we return the config default.
* @return Collection
*/
public function getMetadataProviders(): Collection
{
$additionalMetadataProviders = collect($this->metadataProviders ?? $this->getConfigValueForKey('dump.metadata.providers'));
return $additionalMetadataProviders->prepend(DatabaseMetadataProvider::class);
}
public function shouldDumpCharsets(): bool
{
return $this->dumpCharsets;
}
public function shouldDumpComments(): bool
{
return $this->dumpComments;
}
public function shouldCreateDb(): bool
{
return $this->createDb;
}
public function shouldDropDb(): bool
{
return $this->dropDb;
}
public function shouldDumpData(): bool
{
return $this->dumpData;
}
public function shouldRemoveAutoIncrementingState(): bool
{
return $this->removeAutoIncrementingState;
}
public function shouldUseTablespaces(): bool
{
return $this->tablespaces;
}
}