1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BfwSql\Runners; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Connect to all declared databases |
7
|
|
|
* It's a runner, so is called when the module is initialized. |
8
|
|
|
* |
9
|
|
|
* @package bfw-sql |
10
|
|
|
* @author Vermeulen Maxime <[email protected]> |
11
|
|
|
* @version 2.0 |
12
|
|
|
*/ |
13
|
|
|
class ConnectDB extends AbstractRunner |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @const ERR_NO_CONNECTION_KEYNAME Exception code if the connection has no |
17
|
|
|
* keyName and there are many connection declared. |
18
|
|
|
*/ |
19
|
|
|
const ERR_NO_CONNECTION_KEYNAME = 2602001; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @const ERR_NO_BASE_TYPE Exception code if the base type is not declared. |
23
|
|
|
*/ |
24
|
|
|
const ERR_NO_BASE_TYPE = 2602002; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
* Run the system to connect to all database declared. |
29
|
|
|
*/ |
30
|
|
|
public function run() |
31
|
|
|
{ |
32
|
|
|
$moduleConfig = $this->module->getConfig(); |
33
|
|
|
$configListBases = $moduleConfig->getValue('bases', 'bases.php'); |
34
|
|
|
|
35
|
|
|
$this->module->listBases = []; |
36
|
|
|
|
37
|
|
|
foreach ($configListBases as $baseInfos) { |
38
|
|
|
$this->connectToDatabase($baseInfos); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create the connection to a database |
44
|
|
|
* |
45
|
|
|
* @param object $baseInfos Information about the database to connect |
46
|
|
|
* |
47
|
|
|
* @throws \Exception |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected function connectToDatabase($baseInfos) |
52
|
|
|
{ |
53
|
|
|
if (empty($baseInfos->baseType)) { |
54
|
|
|
throw new \Exception( |
55
|
|
|
'bfw-sql : All connection should have a type declared.', |
56
|
|
|
self::ERR_NO_BASE_TYPE |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$baseKey = $baseInfos->baseKeyName; |
61
|
|
|
$moduleConfig = $this->module->getConfig(); |
62
|
|
|
$configListBases = $moduleConfig->getValue('bases', 'bases.php'); |
63
|
|
|
$configNbBases = count($configListBases); |
64
|
|
|
|
65
|
|
|
if (empty($baseKey) && $configNbBases > 1) { |
66
|
|
|
throw new \Exception( |
67
|
|
|
'bfw-sql : They are multiple connection defined,' |
68
|
|
|
.' a keyName must be define to each connection.', |
69
|
|
|
self::ERR_NO_CONNECTION_KEYNAME |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$usedClass = \BfwSql\UsedClass::getInstance(); |
74
|
|
|
$connectClassName = $usedClass->obtainClassNameToUse('SqlConnect'); |
75
|
|
|
|
76
|
|
|
$this->module->listBases[$baseKey] = new $connectClassName($baseInfos); |
77
|
|
|
$this->module->listBases[$baseKey]->createConnection(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|