Issues (41)

src/NamedPdoEnvModule.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ExtendedPdoInterface;
8
use Override;
9
use Ray\Di\AbstractModule;
10
11
final class NamedPdoEnvModule extends AbstractModule
12
{
13
    public const string PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 13 at column 24
Loading history...
14
15
    /**
16
     * @param string              $qualifer Qualifer for ExtendedPdoInterface
17
     * @param string              $dsn      Data Source Name (DSN)
18
     * @param string              $username User name for the DSN string
19
     * @param string              $password Password for the DSN string
20
     * @param string              $slave    Comma separated slave host list
21
     * @param array<string,mixed> $options  A key=>value array of driver-specific connection options
22
     * @param array<string>       $queries  Queries to execute after the connection.
23
     */
24
    public function __construct(
25
        private readonly string $qualifer,
26
        private readonly string $dsn,
27
        private readonly string $username = '',
28
        private readonly string $password = '',
29
        private readonly string $slave = '',
30
        /** @var array<string, mixed> */
31
        private readonly array $options = [],
32
        /** @var array<string> */
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()
45
            : $this->configureSingleDsn();
46
    }
47
48
    private function configureSingleDsn(): void
49
    {
50
        $connection = new EnvConnection(
51
            $this->dsn,
52
            null,
53
            $this->username,
54
            $this->password,
55
            $this->options,
56
            $this->queries,
57
        );
58
        $this->bind(EnvConnection::class)->annotatedWith($this->qualifer)->toInstance($connection);
59
        $this->bind(ExtendedPdoInterface::class)->annotatedWith($this->qualifer)->toProvider(
60
            NamedExtendedPdoProvider::class,
61
            $this->qualifer,
62
        );
63
    }
64
65
    private function configureMasterSlaveDsn(): void
66
    {
67
        $locator = ConnectionLocatorFactory::fromEnv(
68
            $this->dsn,
69
            $this->username,
70
            $this->password,
71
            $this->slave,
72
            $this->options,
73
            $this->queries,
74
        );
75
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
76
    }
77
}
78