| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ray\AuraSqlModule; |
||
| 6 | |||
| 7 | use Aura\Sql\ExtendedPdo; |
||
| 8 | use Aura\Sql\ExtendedPdoInterface; |
||
| 9 | use Override; |
||
| 10 | use Ray\Di\AbstractModule; |
||
| 11 | use Ray\Di\Scope; |
||
| 12 | use SensitiveParameter; |
||
| 13 | |||
| 14 | final class AuraSqlModule 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 $dsn Data Source Name (DSN) |
||
| 20 | * @param string $user User name for the DSN string |
||
| 21 | * @param string $password Password for the DSN string |
||
| 22 | * @param string $slave Comma separated slave host list |
||
| 23 | * @param array<string> $options A key=>value array of driver-specific connection options |
||
| 24 | * @param array<string> $queries |
||
| 25 | */ |
||
| 26 | public function __construct( |
||
| 27 | private readonly string $dsn, |
||
| 28 | private readonly string $user = '', |
||
| 29 | #[SensitiveParameter] |
||
| 30 | private readonly string $password = '', |
||
| 31 | private readonly string $slave = '', |
||
| 32 | private readonly array $options = [], |
||
| 33 | private readonly array $queries = [] |
||
| 34 | ) { |
||
| 35 | parent::__construct(); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritDoc} |
||
| 40 | */ |
||
| 41 | #[Override] |
||
| 42 | protected function configure(): void |
||
| 43 | { |
||
| 44 | $this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn(); |
||
| 45 | $this->install(new AuraSqlBaseModule($this->dsn)); |
||
| 46 | } |
||
| 47 | |||
| 48 | private function configureSingleDsn(): void |
||
| 49 | { |
||
| 50 | $this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn); |
||
| 51 | $this->bind()->annotatedWith('pdo_user')->toInstance($this->user); |
||
| 52 | $this->bind()->annotatedWith('pdo_pass')->toInstance($this->password); |
||
| 53 | $this->bind()->annotatedWith('pdo_slave')->toInstance($this->slave); |
||
| 54 | $this->bind()->annotatedWith('pdo_options')->toInstance($this->options); |
||
| 55 | $this->bind()->annotatedWith('pdo_queries')->toInstance($this->queries); |
||
| 56 | $this->bind(ExtendedPdoInterface::class)->toConstructor( |
||
| 57 | ExtendedPdo::class, |
||
| 58 | 'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_options,queries=pdo_queries', |
||
| 59 | )->in(Scope::SINGLETON); |
||
| 60 | } |
||
| 61 | |||
| 62 | private function configureMasterSlaveDsn(): void |
||
| 63 | { |
||
| 64 | $locator = ConnectionLocatorFactory::fromInstance( |
||
| 65 | $this->dsn, |
||
| 66 | $this->user, |
||
| 67 | $this->password, |
||
| 68 | $this->slave, |
||
| 69 | $this->options, |
||
| 70 | $this->queries, |
||
| 71 | ); |
||
| 72 | $this->install(new AuraSqlReplicationModule($locator)); |
||
| 73 | } |
||
| 74 | } |
||
| 75 |