1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016-2022 |
6
|
|
|
* @package Base |
7
|
|
|
* @subpackage DB |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Base\DB\Manager; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Manager for database connections |
16
|
|
|
* |
17
|
|
|
* @package Base |
18
|
|
|
* @subpackage DB |
19
|
|
|
*/ |
20
|
|
|
class Standard implements \Aimeos\Base\DB\Manager\Iface |
21
|
|
|
{ |
22
|
|
|
private $conns = []; |
23
|
|
|
private $config; |
24
|
|
|
private $type; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initializes the database manager object |
29
|
|
|
* |
30
|
|
|
* @param array $config Database resource configuration |
31
|
|
|
* @param string $type Type of the connection |
32
|
|
|
*/ |
33
|
|
|
public function __construct( array $config, string $type = 'PDO' ) |
34
|
|
|
{ |
35
|
|
|
$this->config = $config; |
36
|
|
|
$this->type = $type; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Cleans up the object |
42
|
|
|
*/ |
43
|
|
|
public function __destruct() |
44
|
|
|
{ |
45
|
|
|
foreach( $this->conns as $key => $conn ) { |
46
|
|
|
unset( $this->conns[$key] ); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Reset when cloning the object |
53
|
|
|
*/ |
54
|
|
|
public function __clone() |
55
|
|
|
{ |
56
|
|
|
$this->conns = []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Clean up the objects inside |
62
|
|
|
*/ |
63
|
|
|
public function __sleep() |
64
|
|
|
{ |
65
|
|
|
$this->__destruct(); |
66
|
|
|
$this->conns = []; |
67
|
|
|
|
68
|
|
|
return get_object_vars( $this ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a database connection. |
74
|
|
|
* |
75
|
|
|
* @param string $name Name of the resource in configuration |
76
|
|
|
* @param bool $new Create a new connection instead of returning the existing one |
77
|
|
|
* @return \Aimeos\Base\DB\Connection\Iface |
78
|
|
|
*/ |
79
|
|
|
public function get( string $name = 'db', bool $new = false ) : \Aimeos\Base\DB\Connection\Iface |
80
|
|
|
{ |
81
|
|
|
if( $new ) { |
82
|
|
|
return $this->create( $this->config( $name ) ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if( !isset( $this->conns[$name] ) ) { |
86
|
|
|
$this->conns[$name] = $this->create( $this->config( $name ) ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->conns[$name]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns the configuration for the given name |
95
|
|
|
* |
96
|
|
|
* @param string $name Name of the resource, e.g. "db" or "db-product" |
97
|
|
|
* @return array Configuration values |
98
|
|
|
* @throws \Aimeos\Base\DB\Exception If an no configuration for that name is found |
99
|
|
|
*/ |
100
|
|
|
protected function config( string $name ) : array |
101
|
|
|
{ |
102
|
|
|
foreach( [$name, 'db'] as $fsname ) |
103
|
|
|
{ |
104
|
|
|
if( isset( $this->config[$fsname] ) ) { |
105
|
|
|
return $this->config[$fsname]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$msg = sprintf( 'No resource configuration for "%1$s" available', $name ); |
110
|
|
|
throw new \Aimeos\Base\DB\Exception( $msg ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Creates and returns a database connection. |
116
|
|
|
* |
117
|
|
|
* @param array $config Database connection configurations |
118
|
|
|
* @param string $type Type of the connection |
119
|
|
|
* @return \Aimeos\Base\DB\Connection\Iface Instance of a database connection |
120
|
|
|
* @throws \Aimeos\Base\DB\Exception if database connection class isn't found |
121
|
|
|
*/ |
122
|
|
|
protected function create( array $config ) |
123
|
|
|
{ |
124
|
|
|
$classname = '\Aimeos\Base\DB\Connection\\' . $this->type; |
125
|
|
|
|
126
|
|
|
if( !class_exists( $classname ) ) { |
127
|
|
|
throw new \Aimeos\Base\DB\Exception( sprintf( 'Database connection "%1$s" not found', $this->type ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return new $classname( $config ); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|