-
Notifications
You must be signed in to change notification settings - Fork 610
/
DbConfig.php
787 lines (736 loc) · 21.7 KB
/
DbConfig.php
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\config;
use Craft;
use craft\db\Connection;
use craft\helpers\Db;
use craft\helpers\StringHelper;
use craft\services\Config;
use yii\base\InvalidConfigException;
/**
* DB config class
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class DbConfig extends BaseConfig
{
/**
* @deprecated in 3.4.0. Use [[Connection::DRIVER_MYSQL]] instead.
*/
public const DRIVER_MYSQL = 'mysql';
/**
* @deprecated in 3.4.0. Use [[Connection::DRIVER_PGSQL]] instead.
*/
public const DRIVER_PGSQL = 'pgsql';
/**
* @inheritdoc
*/
protected ?string $filename = Config::CATEGORY_DB;
/**
* @var array An array of key-value pairs of PDO attributes to pass into the PDO constructor.
*
* For example, when using the [MySQL PDO driver](https://php.net/manual/en/ref.pdo-mysql.php), if you wanted to enable a SSL database connection
* (assuming [SSL is enabled in MySQL](https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/secure-deployment-secure-connections.html) and `'user'` can connect via SSL,
* you’d set these:
*
* ```php
* ->attributes([
* PDO::MYSQL_ATTR_SSL_KEY => '/path/to/my/client-key.pem',
* PDO::MYSQL_ATTR_SSL_CERT => '/path/to/my/client-cert.pem',
* PDO::MYSQL_ATTR_SSL_CA => '/path/to/my/ca-cert.pem',
* ])
* ```
*/
public array $attributes = [];
/**
* @var string The character set to use when creating tables.
*
* ::: tip
* You can change the character set and collation across all existing database tables using this terminal command:
*
* ```bash
* php craft db/convert-charset
* ```
* :::
*
* ::: warning
* If you set this to something besides `utf8` or `utf8mb4` for MySQL, you **must** also set the [[collation]]
* setting to a compatible collation name.
* :::
*
* ::: code
* ```php Static Config
* ->charset('utf8mb3')
* ->collation('utf8mb3_general_ci')
* ```
* ```shell Environment Override
* CRAFT_DB_CHARSET=utf8mb3
* CRAFT_DB_COLLATION=utf8mb3_general_ci
* ```
* :::
*/
public string $charset = 'utf8';
/**
* @var string|null The collation to use when creating tables. (MySQL only.)
*
* If null, the following collation will be used by default:
*
* - **MySQL 8.0+**: `utf8mb4_0900_ai_ci`
* - **Older MySQL versions and MariaDB**: `utf8mb4_unicode_ci`
*
* ::: tip
* You can change the character set and collation across all existing database tables using this terminal command:
*
* ```bash
* php craft db/convert-charset
* ```
* :::
*
* ::: code
* ```php Static Config
* ->charset('utf8mb3')
* ->collation('utf8mb3_general_ci')
* ```
* ```shell Environment Override
* CRAFT_DB_CHARSET=utf8mb3
* CRAFT_DB_COLLATION=utf8mb3_general_ci
* ```
* :::
*
* @since 3.6.4
*/
public ?string $collation = null;
/**
* @var string|null The Data Source Name (“DSN”) that tells Craft how to connect to the database.
*
* DSNs should begin with a driver prefix (`mysql:` or `pgsql:`), followed by driver-specific parameters.
* For example, `mysql:host=127.0.0.1;port=3306;dbname=acme_corp`.
*
* - MySQL parameters: <https://php.net/manual/en/ref.pdo-mysql.connection.php>
* - PostgreSQL parameters: <https://php.net/manual/en/ref.pdo-pgsql.connection.php>
*
* ::: code
* ```php Static Config
* ->dsn('mysql:host=127.0.0.1;port=3306;dbname=acme_corp')
* ```
* ```shell Environment Override
* CRAFT_DB_DSN=mysql:host=127.0.0.1;port=3306;dbname=acme_corp
* ```
* :::
*/
public ?string $dsn = null;
/**
* @var string The database password to connect with.
*
* ::: code
* ```php Static Config
* ->password('super-secret')
* ```
* ```shell Environment Override
* CRAFT_DB_PASSWORD=super-secret
* ```
* :::
*/
public string $password = '';
/**
* @var string|null The schema that Postgres is configured to use by default (PostgreSQL only).
*
* ::: tip
* To force Craft to use the specified schema regardless of PostgreSQL’s `search_path` setting, you must enable
* the [[setSchemaOnConnect]] setting.
* :::
*
* ::: code
* ```php Static Config
* ->schema('myschema,public')
* ```
* ```shell Environment Override
* CRAFT_DB_SCHEMA=myschema,public
* ```
* :::
*
* @see https://www.postgresql.org/docs/8.2/static/ddl-schemas.html
*/
public ?string $schema = 'public';
/**
* @var bool Whether the [[schema]] should be explicitly used for database queries (PostgreSQL only).
*
* ::: warning
* This will cause an extra `SET search_path` SQL query to be executed per database connection. Ideally,
* PostgreSQL’s `search_path` setting should be configured to prioritize the desired schema.
* :::
*
* ::: code
* ```php Static Config
* ->setSchemaOnConnect(true)
* ```
* ```shell Environment Override
* CRAFT_DB_SET_SCHEMA_ON_CONNECT=true
* ```
* :::
*
* @since 3.7.27
*/
public bool $setSchemaOnConnect = false;
/**
* @var string|null If you’re sharing Craft installs in a single database (MySQL) or a single database and using a shared schema (PostgreSQL),
* you can set a table prefix here to avoid per-install table naming conflicts. This can be no more than 5 characters, and must be all lowercase.
*
* ::: code
* ```php Static Config
* ->tablePrefix('craft_')
* ```
* ```shell Environment Override
* CRAFT_DB_TABLE_PREFIX=craft_
* ```
* :::
*/
public ?string $tablePrefix = null;
/**
* @var string The database username to connect with.
*
* ::: code
* ```php Static Config
* ->user('db')
* ```
* ```shell Environment Override
* CRAFT_DB_USER=db
* ```
* :::
*/
public string $user = 'root';
/**
* @var bool Whether batched queries should be executed on a separate, unbuffered database connection.
*
* This setting only applies to MySQL. It can be enabled when working with high volume content, to prevent
* PHP from running out of memory when querying too much data at once. (See
* <https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#batch-query-mysql> for an explanation
* of MySQL’s batch query limitations.)
*
* For more on Craft batch queries, see <https://craftcms.com/knowledge-base/query-batching-batch-each>.
*
* ::: code
* ```php Static Config
* ->useUnbufferedConnections(true)
* ```
* ```shell Environment Override
* CRAFT_DB_USE_UNBUFFERED_CONNECTIONS=true
* ```
* :::
*
* @since 3.7.0
*/
public bool $useUnbufferedConnections = false;
/**
* @var string|null The database connection URL, if one was provided by your hosting environment.
*
* If this is set, the values for [[driver]], [[user]], [[database]], [[server]], [[port]], and [[database]] will be extracted from it.
*
* ::: code
* ```php Static Config
* ->url('jdbc:mysql://database.foo:3306/mydb')
* ```
* ```shell Environment Override
* CRAFT_DB_URL=jdbc:mysql://database.foo:3306/mydb
* ```
* :::
*/
public ?string $url = null;
/**
* @var string|null The database driver to use. Either `mysql` for MySQL or `pgsql` for PostgreSQL.
*
* ::: code
* ```php Static Config
* ->driver('mysql')
* ```
* ```shell Environment Override
* CRAFT_DB_DRIVER=mysql
* ```
* :::
*/
public ?string $driver = null;
/**
* @var string|null The database server name or IP address. Usually `localhost` or `127.0.0.1`.
*
* ::: code
* ```php Static Config
* ->server('localhost')
* ```
* ```shell Environment Override
* CRAFT_DB_SERVER=localhost
* ```
* :::
*/
public ?string $server = null;
/**
* @var int|null The database server port. Defaults to 3306 for MySQL and 5432 for PostgreSQL.
*
* ::: code
* ```php Static Config
* ->port(3306)
* ```
* ```shell Environment Override
* CRAFT_DB_PORT=3306
* ```
* :::
*/
public ?int $port = null;
/**
* @var string|null MySQL only. If this is set, the CLI connection string (used for yiic) will connect to the Unix socket instead of
* the server and port. If this is specified, then `server` and `port` settings are ignored.
*
* ::: code
* ```php Static Config
* ->unixSocket('/Applications/MAMP/tmp/mysql/mysql.sock')
* ```
* ```shell Environment Override
* CRAFT_DB_UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
* ```
* :::
*/
public ?string $unixSocket = null;
/**
* @var string|null The name of the database to select.
*
* ::: code
* ```php Static Config
* ->database('mydatabase')
* ```
* ```shell Environment Override
* CRAFT_DB_DATABASE=mydatabase
* ```
* :::
*/
public ?string $database = null;
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init(): void
{
// (Re-)normalize everything.
$this
->url($this->url)
->tablePrefix($this->tablePrefix)
;
// If we don't have a DSN yet, create one from the deprecated settings
if (!isset($this->dsn)) {
$this->_updateDsn();
}
}
/**
* An array of key-value pairs of PDO attributes to pass into the PDO constructor.
*
* For example, when using the [MySQL PDO driver](https://php.net/manual/en/ref.pdo-mysql.php), if you wanted to enable a SSL database connection
* (assuming [SSL is enabled in MySQL](https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/secure-deployment-secure-connections.html) and `'user'` can connect via SSL,
* you’d set these:
*
* ```php
* ->pdoAttributes([
* PDO::MYSQL_ATTR_SSL_KEY => '/path/to/my/client-key.pem',
* PDO::MYSQL_ATTR_SSL_CERT => '/path/to/my/client-cert.pem',
* PDO::MYSQL_ATTR_SSL_CA => '/path/to/my/ca-cert.pem',
* ])
* ```
* @param array $value
* @return self
* @see $attributes
* @since 4.2.0
*/
public function pdoAttributes(array $value): self
{
$this->attributes = $value;
return $this;
}
/**
* The character set to use when creating tables.
*
* ::: tip
* You can change the character set and collation across all existing database tables using this terminal command:
*
* ```bash
* php craft db/convert-charset
* ```
* :::
*
* ::: warning
* If you set this to something besides `utf8` or `utf8mb4` for MySQL, you **must** also set the [[collation]]
* setting to a compatible collation name.
* :::
*
* ```php
* ->charset('utf8mb3')
* ->collation('utf8mb3_general_ci')
* ```
*
* @param string $value
* @return self
* @see $charset
* @since 4.2.0
*/
public function charset(string $value): self
{
$this->charset = $value;
return $this;
}
/**
* Returns the normalized charset.
*
* @return string
* @since 5.0.0
*/
public function getCharset(): string
{
if (
$this->driver === Connection::DRIVER_MYSQL &&
$this->charset === 'utf8' &&
(!isset($this->collation) || str_starts_with($this->collation, 'utf8mb4_'))
) {
// treat utf8 as an alias for utf8mb4
// (MySQL aliases it to utf8mb3, but that's deprecated and likely to change eventually)
return 'utf8mb4';
}
return $this->charset;
}
/**
* The collation to use when creating tables. (MySQL only.)
*
* If null, the following collation will be used by default:
*
* - **MySQL 8.0+**: `utf8mb4_0900_ai_ci`
* - **Older MySQL versions and MariaDB**: `utf8mb4_unicode_ci`
*
* ::: tip
* You can change the character set and collation across all existing database tables using this terminal command:
*
* ```bash
* php craft db/convert-charset
* ```
* :::
*
* ```php
* ->collation('utf8mb3_general_ci')
* ```
*
* @param string|null $value
* @return self
* @see $collation
* @since 4.2.0
*/
public function collation(?string $value): self
{
$this->collation = $value;
return $this;
}
/**
* The Data Source Name (“DSN”) that tells Craft how to connect to the database.
*
* DSNs should begin with a driver prefix (`mysql:` or `pgsql:`), followed by driver-specific parameters.
* For example, `mysql:host=127.0.0.1;port=3306;dbname=acme_corp`.
*
* - MySQL parameters: <https://php.net/manual/en/ref.pdo-mysql.connection.php>
* - PostgreSQL parameters: <https://php.net/manual/en/ref.pdo-pgsql.connection.php>
*
* ```php
* ->dsn('mysql:host=127.0.0.1;port=3306;dbname=acme_corp')
* ```
*
* @param string|null $value
* @return self
* @see $dsn
* @since 4.2.0
*/
public function dsn(?string $value): self
{
$this->dsn = $value;
if ($value) {
$parsed = Db::parseDsn($value);
$this->driver = $parsed['driver'];
$this->unixSocket = $parsed['unix_socket'] ?? null;
$this->server = $parsed['host'] ?? null;
$this->port = (int)($parsed['port'] ?? 0) ?: null;
$this->database = $parsed['dbname'] ?? null;
}
return $this;
}
/**
* The database password to connect with.
*
* ```php
* ->password('super-secret')
* ```
*
* @param string $value
* @return self
* @see $password
* @since 4.2.0
*/
public function password(string $value): self
{
$this->password = $value;
return $this;
}
/**
* The schema that Postgres is configured to use by default (PostgreSQL only).
*
* ::: tip
* To force Craft to use the specified schema regardless of PostgreSQL’s `search_path` setting, you must enable
* the [[setSchemaOnConnect]] setting.
* :::
*
* ```php
* ->schema('myschema,public')
* ```
*
* @param string|null $value
* @return self
* @see $schema
* @see https://www.postgresql.org/docs/8.2/static/ddl-schemas.html
* @since 4.2.0
*/
public function schema(?string $value): self
{
$this->schema = $value;
return $this;
}
/**
* Whether the [[schema]] should be explicitly used for database queries (PostgreSQL only).
*
* ::: warning
* This will cause an extra `SET search_path` SQL query to be executed per database connection. Ideally,
* PostgreSQL’s `search_path` setting should be configured to prioritize the desired schema.
* :::
*
* ```php
* ->setSchemaOnConnect()
* ```
*
* @param bool $value
* @return self
* @see $setSchemaOnConnect
* @since 4.2.0
*/
public function setSchemaOnConnect(bool $value = true): self
{
$this->setSchemaOnConnect = $value;
return $this;
}
/**
* If you’re sharing Craft installs in a single database (MySQL) or a single database and using a shared schema (PostgreSQL),
* you can set a table prefix here to avoid per-install table naming conflicts. This can be no more than 5 characters, and must be all lowercase.
*
* ```php
* ->tablePrefix('craft_')
* ```
*
* @param string|null $value
* @return self
* @throws InvalidConfigException
* @see $tablePrefix
* @since 4.2.0
*/
public function tablePrefix(?string $value): self
{
if ($value) {
$value = StringHelper::ensureRight($value, '_');
if (strlen($value) > 6) {
throw new InvalidConfigException('tablePrefix must be 5 or less characters long: ' . $value);
}
}
$this->tablePrefix = $value;
return $this;
}
/**
* The database username to connect with.
*
* ```php
* ->user('db')
* ```
*
* @param string $value
* @return self
* @see $user
* @since 4.2.0
*/
public function user(string $value): self
{
$this->user = $value;
return $this;
}
/**
* Whether batched queries should be executed on a separate, unbuffered database connection.
*
* This setting only applies to MySQL. It can be enabled when working with high volume content, to prevent
* PHP from running out of memory when querying too much data at once. (See
* <https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#batch-query-mysql> for an explanation
* of MySQL’s batch query limitations.)
*
* For more on Craft batch queries, see <https://craftcms.com/knowledge-base/query-batching-batch-each>.
*
* ```php
* ->useUnbufferedConnections()
* ```
*
* @param bool $value
* @return self
* @see $useUnbufferedConnections
* @since 4.2.0
*/
public function useUnbufferedConnections(bool $value = true): self
{
$this->useUnbufferedConnections = $value;
return $this;
}
/**
* The database connection URL, if one was provided by your hosting environment.
*
* If this is set, the values for [[driver]], [[user]], [[database]], [[server]], [[port]], and [[database]] will be extracted from it.
*
* ```php
* ->url('jdbc:mysql://database.foo:3306/mydb')
* ```
*
* @param string|null $value
* @return self
* @see $url
* @since 4.2.0
*/
public function url(?string $value): self
{
if ($value) {
Craft::configure($this, Db::url2config($value));
}
$this->url = $value;
return $this;
}
/**
* The database driver to use. Either `mysql` for MySQL or `pgsql` for PostgreSQL.
*
* ```php
* ->driver('mysql')
* ```
*
* @param string|null $value
* @return self
* @throws InvalidConfigException
* @see $driver
* @since 4.2.0
*/
public function driver(?string $value): self
{
$this->driver = $value;
$this->_updateDsn();
return $this;
}
/**
* The database server name or IP address. Usually `localhost` or `127.0.0.1`.
*
* ```php
* ->server('localhost')
* ```
*
* @param string|null $value
* @return self
* @throws InvalidConfigException
* @see $server
* @since 4.2.0
*/
public function server(?string $value): self
{
$this->server = $value;
$this->_updateDsn();
return $this;
}
/**
* The database server port. Defaults to 3306 for MySQL and 5432 for PostgreSQL.
*
* ```php
* ->port(3306)
* ```
*
* @param int|null $value
* @return self
* @throws InvalidConfigException
* @see $port
* @since 4.2.0
*/
public function port(?int $value): self
{
$this->port = $value;
$this->_updateDsn();
return $this;
}
/**
* MySQL only. If this is set, the CLI connection string (used for yiic) will connect to the Unix socket instead of
* the server and port. If this is specified, then `server` and `port` settings are ignored.
*
* ```php
* ->unixSocket('/Applications/MAMP/tmp/mysql/mysql.sock')
* ```
*
* @param string|null $value
* @return self
* @throws InvalidConfigException
* @see $unixSocket
* @since 4.2.0
*/
public function unixSocket(?string $value): self
{
$this->unixSocket = $value;
$this->_updateDsn();
return $this;
}
/**
* The name of the database to select.
*
* ```php
* ->database('mydatabase')
* ```
*
* @param string|null $value
* @return self
* @throws InvalidConfigException
* @see $database
* @since 4.2.0
*/
public function database(?string $value): self
{
$this->database = $value;
$this->_updateDsn();
return $this;
}
/**
* Updates the DSN string based on the config setting values.
*
* @throws InvalidConfigException
*/
private function _updateDsn(): void
{
if (!$this->driver) {
$this->driver = Connection::DRIVER_MYSQL;
}
if (!in_array($this->driver, [Connection::DRIVER_MYSQL, Connection::DRIVER_PGSQL], true)) {
throw new InvalidConfigException('Unsupported DB driver value: ' . $this->driver);
}
if ($this->driver === Connection::DRIVER_MYSQL && $this->unixSocket) {
$this->unixSocket = strtolower($this->unixSocket);
$this->dsn = "$this->driver:unix_socket=$this->unixSocket;dbname=$this->database";
return;
}
$this->server = strtolower($this->server ?? '');
if ($this->port) {
$port = $this->port;
} else {
$port = match ($this->driver) {
Connection::DRIVER_MYSQL => 3306,
Connection::DRIVER_PGSQL => 5432,
};
}
$this->dsn = "$this->driver:host=$this->server;dbname=$this->database;port=$port";
}
}