-
-
Notifications
You must be signed in to change notification settings - Fork 31
Add config DTO's #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add config DTO's #10
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
91e5e33
Add base DTO config compatibility
SerafimArts 5fcbf99
Fix tests driver memoization
SerafimArts fc391ed
Apply phpcs
SerafimArts 1e32030
Rename config DTOs (Info -> Config)
SerafimArts 87d01ca
Add DSN connection configuration classes
SerafimArts 17a9025
Fix SQLite and SQLServer DSN db source
SerafimArts 77d26be
Improve constructor docblock
SerafimArts bf8b88d
Rename "Uri" to "Tcp"
SerafimArts c9dd36d
Deshuffle methods in the Driver class
roxblnfk 42b0807
Update src/Driver/Driver.php
SerafimArts 23e331f
Update composer.json
SerafimArts 5287004
Rename driver "addr" to "connection"
SerafimArts 7b128b4
Improve tests docblocks
SerafimArts 2728e09
Improve read pcre
SerafimArts 075ed5d
Add TODO with parameter escaping
SerafimArts dd173e2
Improve postgres driver config param description
SerafimArts 418bf66
Add sqlite's config database parameter description.
SerafimArts 5b76fee
Remove unused config class
SerafimArts 8f5eb17
Remove final keyword from config DTO
SerafimArts 5187cfb
Improve and fix driver configs
SerafimArts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of Cycle Database package. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Database\Config; | ||
|
|
||
| abstract class ConnectionConfig | ||
| { | ||
| /** | ||
| * @var array<non-empty-string> | ||
| */ | ||
| protected array $nonPrintableOptions = [ | ||
| // Postgres and MySQL | ||
| 'password', | ||
| // IBM, ODBC and DB2 | ||
| 'PWD', | ||
| ]; | ||
|
|
||
| /** | ||
| * @param non-empty-string|null $user | ||
| * @param non-empty-string|null $password | ||
| */ | ||
| public function __construct( | ||
| public ?string $user = null, | ||
| public ?string $password = null, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return non-empty-string|null | ||
| */ | ||
| public function getUsername(): ?string | ||
| { | ||
| return $this->user; | ||
| } | ||
|
|
||
| /** | ||
| * @return non-empty-string|null | ||
| */ | ||
| public function getPassword(): ?string | ||
| { | ||
| return $this->password; | ||
| } | ||
|
|
||
| /** | ||
| * @param bool $secure | ||
| * @return array | ||
| */ | ||
| protected function toArray(bool $secure = true): array | ||
| { | ||
| $options = \get_object_vars($this); | ||
|
|
||
| foreach ($options as $key => $value) { | ||
| if ($secure && \in_array($key, $this->nonPrintableOptions, true)) { | ||
| $value = '<hidden>'; | ||
| } | ||
|
|
||
| $options[$key] = $value; | ||
| } | ||
|
|
||
| return $options; | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function __debugInfo(): array | ||
| { | ||
| return $this->toArray(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of Cycle Database package. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Database\Config; | ||
|
|
||
| use Cycle\Database\Driver\DriverInterface; | ||
|
|
||
| /** | ||
| * Connection configuration described in DBAL config file. Any driver can be | ||
| * used as data source for multiple databases as table prefix and quotation | ||
| * defined on Database instance level. | ||
| * | ||
| * @template T of TcpConnectionConfig | ||
| */ | ||
| abstract class DriverConfig | ||
| { | ||
| /** | ||
| * @param T $connection | ||
| * @param bool $reconnect Allow reconnects | ||
| * @param non-empty-string $timezone All datetime objects will be converted | ||
| * relative to this timezone (must match with DB timezone!) | ||
| * @param bool $queryCache Enables query caching | ||
| * @param bool $readonlySchema Disable schema modifications | ||
| * @param bool $readonly Disable write expressions | ||
| */ | ||
| public function __construct( | ||
| public ConnectionConfig $connection, | ||
| public bool $reconnect = true, | ||
| public string $timezone = 'UTC', | ||
| public bool $queryCache = true, | ||
| public bool $readonlySchema = false, | ||
| public bool $readonly = false, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return DriverInterface | ||
| */ | ||
| abstract public function getDriver(): DriverInterface; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of Cycle Database package. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Database\Config\MySQL; | ||
|
|
||
| use Cycle\Database\Config\PDOConnectionConfig as BaseConnectionConfig; | ||
|
|
||
| /** | ||
| * @psalm-import-type PDOFlag from TcpConnectionConfig | ||
| */ | ||
| abstract class ConnectionConfig extends BaseConnectionConfig | ||
| { | ||
| /** | ||
| * General driver specific PDO options. | ||
| * | ||
| * @var array<PDOFlag, mixed> | ||
| */ | ||
| protected const DEFAULT_PDO_OPTIONS = [ | ||
| \PDO::ATTR_CASE => \PDO::CASE_NATURAL, | ||
| \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, | ||
| // TODO Should be moved into common driver settings. | ||
| \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "UTF8"', | ||
| \PDO::ATTR_STRINGIFY_FETCHES => false, | ||
| ]; | ||
|
|
||
| /** | ||
| * @param non-empty-string|null $user | ||
| * @param non-empty-string|null $password | ||
| * @param array<non-empty-string|int, non-empty-string> $options | ||
| */ | ||
| public function __construct( | ||
| ?string $user = null, | ||
| ?string $password = null, | ||
| array $options = [], | ||
| ) { | ||
| parent::__construct($user, $password, $options); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function getName(): string | ||
| { | ||
| return 'mysql'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of Cycle Database package. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Database\Config\MySQL; | ||
|
|
||
| use Cycle\Database\Config\ProvidesSourceString; | ||
| use Cycle\Database\Config\Support\DataSourceName; | ||
|
|
||
| class DsnConnectionConfig extends ConnectionConfig implements ProvidesSourceString | ||
| { | ||
| /** | ||
| * @var string|null | ||
| */ | ||
| private ?string $database = null; | ||
|
|
||
| /** | ||
| * @var non-empty-string | ||
| * @psalm-allow-private-mutation | ||
| */ | ||
| public string $dsn; | ||
|
|
||
| /** | ||
| * @param non-empty-string|\Stringable $dsn | ||
| * @param non-empty-string|null $user | ||
| * @param non-empty-string|null $password | ||
| * @param array $options | ||
| */ | ||
| public function __construct( | ||
| string|\Stringable $dsn, | ||
| ?string $user = null, | ||
| ?string $password = null, | ||
| array $options = [] | ||
| ) { | ||
| parent::__construct($user, $password, $options); | ||
|
|
||
| /** @psalm-suppress ArgumentTypeCoercion */ | ||
| $this->dsn = DataSourceName::normalize((string)$dsn, $this->getName()); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function getSourceString(): string | ||
| { | ||
| /** @psalm-suppress ArgumentTypeCoercion */ | ||
| return $this->database ??= DataSourceName::read($this->getDsn(), 'dbname') ?? '*'; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function getDsn(): string | ||
| { | ||
| return $this->dsn; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of Cycle Database package. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\Database\Config\MySQL; | ||
|
|
||
| use Cycle\Database\Config\ProvidesSourceString; | ||
|
|
||
| class SocketConnectionConfig extends ConnectionConfig implements ProvidesSourceString | ||
| { | ||
| /** | ||
| * @param non-empty-string $socket | ||
| * @param non-empty-string $database | ||
| * @param non-empty-string|null $charset | ||
| * @param non-empty-string|null $user | ||
| * @param non-empty-string|null $password | ||
| * @param array<non-empty-string|int, non-empty-string> $options | ||
| */ | ||
| public function __construct( | ||
| public string $database, | ||
| public string $socket, | ||
| public ?string $charset = null, | ||
| ?string $user = null, | ||
| ?string $password = null, | ||
| array $options = [] | ||
| ) { | ||
| parent::__construct($user, $password, $options); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function getSourceString(): string | ||
| { | ||
| return $this->database; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the MySQL-specific PDO DataSourceName with connection Unix socket, | ||
| * that looks like: | ||
| * <code> | ||
| * mysql:unix_socket=/tmp/mysql.sock;dbname=dbname | ||
| * </code> | ||
| * | ||
| * {@inheritDoc} | ||
| */ | ||
| public function getDsn(): string | ||
| { | ||
| $config = [ | ||
| 'unix_socket' => $this->socket, | ||
| 'dbname' => $this->database, | ||
| 'charset' => $this->charset, | ||
| ]; | ||
|
|
||
| return \sprintf('%s:%s', $this->getName(), $this->dsn($config)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.