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