Issues (41)

src/AuraSqlModule.php (1 issue)

Labels
Severity
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
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 16 at column 24
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,mixed> $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
        /** @var array<string, mixed> */
33
        private readonly array $options = [],
34
        /** @var array<string> */
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() : $this->configureSingleDsn();
47
        $this->install(new AuraSqlBaseModule($this->dsn));
48
    }
49
50
    private function configureSingleDsn(): void
51
    {
52
        $this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn);
53
        $this->bind()->annotatedWith('pdo_user')->toInstance($this->user);
54
        $this->bind()->annotatedWith('pdo_pass')->toInstance($this->password);
55
        $this->bind()->annotatedWith('pdo_slave')->toInstance($this->slave);
56
        $this->bind()->annotatedWith('pdo_options')->toInstance($this->options);
57
        $this->bind()->annotatedWith('pdo_queries')->toInstance($this->queries);
58
        $this->bind(ExtendedPdoInterface::class)->toConstructor(
59
            ExtendedPdo::class,
60
            'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_options,queries=pdo_queries',
61
        )->in(Scope::SINGLETON);
62
    }
63
64
    private function configureMasterSlaveDsn(): void
65
    {
66
        $locator = ConnectionLocatorFactory::fromInstance(
67
            $this->dsn,
68
            $this->user,
69
            $this->password,
70
            $this->slave,
71
            $this->options,
72
            $this->queries,
73
        );
74
        $this->install(new AuraSqlReplicationModule($locator));
75
    }
76
}
77