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> $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
        private readonly array $options = [],
31
        private readonly array $queries = []
32
    ) {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    #[Override]
40
    protected function configure(): void
41
    {
42
        $this->slave ? $this->configureMasterSlaveDsn()
43
            : $this->configureSingleDsn();
44
    }
45
46
    private function configureSingleDsn(): void
47
    {
48
        $connection = new EnvConnection(
49
            $this->dsn,
50
            null,
51
            $this->username,
52
            $this->password,
53
            $this->options,
54
            $this->queries,
55
        );
56
        $this->bind(EnvConnection::class)->annotatedWith($this->qualifer)->toInstance($connection);
57
        $this->bind(ExtendedPdoInterface::class)->annotatedWith($this->qualifer)->toProvider(
58
            NamedExtendedPdoProvider::class,
59
            $this->qualifer,
60
        );
61
    }
62
63
    private function configureMasterSlaveDsn(): void
64
    {
65
        $locator = ConnectionLocatorFactory::fromEnv(
66
            $this->dsn,
67
            $this->username,
68
            $this->password,
69
            $this->slave,
70
            $this->options,
71
            $this->queries,
72
        );
73
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
74
    }
75
}
76