Passed
Push — master ( ab8c40...cd2896 )
by 世昌
03:30 queued 01:01
created

DataSource::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace suda\orm;
4
5
use function in_array;
6
use suda\orm\connection\Connection;
7
use suda\orm\exception\SQLException;
8
use suda\orm\connection\MySQLConnection;
9
use suda\orm\connection\SQLiteConnection;
10
11
class DataSource
12
{
13
    /**
14
     * 读连接
15
     *
16
     * @var Connection[]
17
     */
18
    protected $write = [];
19
20
    /**
21
     * 写连接
22
     *
23
     * @var Connection[]
24
     */
25
    protected $read = [];
26
27
    /**
28
     * 当前写数据库
29
     *
30
     * @var Connection|null
31
     */
32
    protected $master;
33
34
    /**
35
     * 当前读数据库
36
     *
37
     * @var Connection|null
38
     */
39
    protected $slave;
40
41
    protected static $type = [
42
        'mysql' => MySQLConnection::class,
43
        'sqlite' => SQLiteConnection::class,
44
    ];
45
46
    /**
47
     * 添加连接
48
     *
49
     * @param Connection $connection
50
     * @return $this
51
     */
52
    public function add(Connection $connection)
53
    {
54
        $this->addRead($connection);
55
        $this->addWrite($connection);
56
        return $this;
57
    }
58
59
    /**
60
     * 添加读连接
61
     *
62
     * @param Connection $connection
63
     * @return $this
64
     */
65
    public function addRead(Connection $connection)
66
    {
67
        if (!in_array($connection, $this->read)) {
68
            $this->read[] = $connection;
69
        }
70
        return $this;
71
    }
72
73
74
    /**
75
     * 添加写连接
76
     *
77
     * @param Connection $connection
78
     * @return $this
79
     */
80
    public function addWrite(Connection $connection)
81
    {
82
        if (!in_array($connection, $this->write)) {
83
            $this->write[] = $connection;
84
        }
85
        return $this;
86
    }
87
88
    /**
89
     * 获取写连接
90
     *
91
     * @return Connection
92
     */
93
    public function read(): Connection
94
    {
95
        $this->selectReadConnection();
96
        return $this->slave;
97
    }
98
99
    /**
100
     * 获取读连接
101
     *
102
     * @return Connection
103
     */
104
    public function write(): Connection
105
    {
106
        $this->selectWriteConnection();
107
        return $this->master;
108
    }
109
110
    /**
111
     * 创建连接
112
     *
113
     * @param string $type
114
     * @param array $config
115
     * @param string|null $name
116
     * @return Connection
117
     * @throws SQLException
118
     */
119
    public static function new(string $type, array $config, ?string $name = null): Connection
120
    {
121
        if (array_key_exists($type, static::$type)) {
122
            return new static::$type[$type]($config, $name);
123
        } else {
124
            throw new SQLException(sprintf('no connection type of %s', $type));
125
        }
126
    }
127
128
    /**
129
     * 读数据库选择
130
     *
131
     * @return void
132
     */
133
    protected function selectReadConnection()
134
    {
135
        $postion = mt_rand(0, count($this->read) - 1);
136
        $this->slave = $this->read[$postion];
137
    }
138
139
    /**
140
     * 写数据库选择
141
     *
142
     * @return void
143
     */
144
    protected function selectWriteConnection()
145
    {
146
        if ($this->master === null) {
147
            $position = mt_rand(0, count($this->write) - 1);
148
            $this->master = $this->write[$position];
149
        }
150
    }
151
}
152