| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ray\AuraSqlModule; |
||
| 6 | |||
| 7 | use Aura\Sql\ConnectionLocator; |
||
| 8 | use Aura\Sql\ExtendedPdo; |
||
| 9 | use Aura\Sql\ExtendedPdoInterface; |
||
| 10 | use Override; |
||
| 11 | use Ray\Di\AbstractModule; |
||
| 12 | use SensitiveParameter; |
||
| 13 | |||
| 14 | final class NamedPdoModule extends AbstractModule |
||
| 15 | { |
||
| 16 | public const string PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 17 | |||
| 18 | /** |
||
| 19 | * @param string $qualifer Qualifer for ExtendedPdoInterface |
||
| 20 | * @param string $dsn Data Source Name (DSN) |
||
| 21 | * @param string $username User name for the DSN string |
||
| 22 | * @param string $password Password for the DSN string |
||
| 23 | * @param string $slave Comma separated slave host list |
||
| 24 | * @param array<string> $options A key=>value array of driver-specific connection options |
||
| 25 | * @param array<string> $queries Queries to execute after the connection. |
||
| 26 | */ |
||
| 27 | public function __construct( |
||
| 28 | private readonly string $qualifer, |
||
| 29 | private readonly string $dsn, |
||
| 30 | private readonly string $username = '', |
||
| 31 | #[SensitiveParameter] |
||
| 32 | private readonly string $password = '', |
||
| 33 | private readonly string $slave = '', |
||
| 34 | private readonly array $options = [], |
||
| 35 | private readonly array $queries = [] |
||
| 36 | ) { |
||
| 37 | parent::__construct(); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritDoc} |
||
| 42 | */ |
||
| 43 | #[Override] |
||
| 44 | protected function configure(): void |
||
| 45 | { |
||
| 46 | $this->slave ? $this->configureMasterSlaveDsn() |
||
| 47 | : $this->configureSingleDsn(); |
||
| 48 | } |
||
| 49 | |||
| 50 | private function configureSingleDsn(): void |
||
| 51 | { |
||
| 52 | $this->bind(ExtendedPdoInterface::class) |
||
| 53 | ->annotatedWith($this->qualifer) |
||
| 54 | ->toConstructor( |
||
| 55 | ExtendedPdo::class, |
||
| 56 | "dsn={$this->qualifer}_dsn,username={$this->qualifer}_username,password={$this->qualifer}_password", |
||
| 57 | ); |
||
| 58 | $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn); |
||
| 59 | $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username); |
||
| 60 | $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password); |
||
| 61 | } |
||
| 62 | |||
| 63 | private function configureMasterSlaveDsn(): void |
||
| 64 | { |
||
| 65 | $locator = $this->getLocator(); |
||
| 66 | $this->install(new AuraSqlReplicationModule($locator, $this->qualifer)); |
||
| 67 | } |
||
| 68 | |||
| 69 | private function getLocator(): ConnectionLocator |
||
| 70 | { |
||
| 71 | return ConnectionLocatorFactory::fromInstance( |
||
| 72 | $this->dsn, |
||
| 73 | $this->username, |
||
| 74 | $this->password, |
||
| 75 | $this->slave, |
||
| 76 | $this->options, |
||
| 77 | $this->queries, |
||
| 78 | ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 |