1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle; |
4
|
|
|
|
5
|
|
|
use Spiral\Database\Config\DatabaseConfig; |
6
|
|
|
use Yiisoft\Aliases\Aliases; |
7
|
|
|
use Yiisoft\Yii\Cycle\Config\BaseConfig; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property-read string $default |
11
|
|
|
* @property-read array $aliases |
12
|
|
|
* @property-read array $databases |
13
|
|
|
* @property-read array $connections |
14
|
|
|
* |
15
|
|
|
* @method string getDefault() |
16
|
|
|
* @method array getAliases() |
17
|
|
|
* @method array getDatabases() |
18
|
|
|
* @method array getConnections() |
19
|
|
|
*/ |
20
|
|
|
class CycleDbalConfig extends BaseConfig |
21
|
|
|
{ |
22
|
|
|
protected $default = ''; |
23
|
|
|
protected $aliases = []; |
24
|
|
|
protected $databases = []; |
25
|
|
|
protected $connections = []; |
26
|
|
|
|
27
|
|
|
// private property will be ignored in toArray() method |
28
|
|
|
/** @var Aliases */ |
29
|
|
|
private $objAliases; |
30
|
|
|
|
31
|
|
|
public function __construct(Aliases $aliases) |
32
|
|
|
{ |
33
|
|
|
$this->objAliases = $aliases; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function prepareConfig(): DatabaseConfig |
37
|
|
|
{ |
38
|
|
|
return new DatabaseConfig($this->toArray()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function setConnections($data): void |
42
|
|
|
{ |
43
|
|
|
$this->connections = $data; |
|
|
|
|
44
|
|
|
foreach ($this->connections as &$connection) { |
45
|
|
|
// if connection option contain alias in path |
46
|
|
|
if (isset($connection['connection']) && preg_match('/^(?<proto>\w+:)?@/', $connection['connection'], $m)) { |
47
|
|
|
$proto = $m['proto']; |
48
|
|
|
$path = $this->convertAlias(substr($connection['connection'], strlen($proto))); |
49
|
|
|
$connection['connection'] = $proto . $path; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function convertAlias(string $alias): string |
55
|
|
|
{ |
56
|
|
|
return $this->objAliases->get($alias, true); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|