|
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 = 2502001; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @const ERR_NO_BASE_TYPE Exception code if the base type is not declared. |
|
23
|
|
|
*/ |
|
24
|
|
|
const ERR_NO_BASE_TYPE = 2502002; |
|
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
|
|
|
$configNbBases = count($configListBases); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$this->module->listBases = []; |
|
37
|
|
|
|
|
38
|
|
|
foreach ($configListBases as $baseInfos) { |
|
39
|
|
|
$this->connectToDatabase($baseInfos); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create the connection to a database |
|
45
|
|
|
* |
|
46
|
|
|
* @param \stdClass $baseInfos Information about the database to connect |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function connectToDatabase($baseInfos) |
|
53
|
|
|
{ |
|
54
|
|
|
if (empty($baseInfos->baseType)) { |
|
55
|
|
|
throw new \Exception( |
|
56
|
|
|
'bfw-sql : All connection should have a type declared.', |
|
57
|
|
|
self::ERR_NO_BASE_TYPE |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$baseKey = $baseInfos->baseKeyName; |
|
62
|
|
|
$moduleConfig = $this->module->getConfig(); |
|
63
|
|
|
$configListBases = $moduleConfig->getValue('bases', 'bases.php'); |
|
64
|
|
|
$configNbBases = count($configListBases); |
|
65
|
|
|
|
|
66
|
|
|
if (empty($baseKey) && $configNbBases > 1) { |
|
67
|
|
|
throw new \Exception( |
|
68
|
|
|
'bfw-sql : They are multiple connection defined,' |
|
69
|
|
|
.' a keyName must be define to each connection.', |
|
70
|
|
|
self::ERR_NO_CONNECTION_KEYNAME |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$usedClass = \BfwSql\UsedClass::getInstance(); |
|
75
|
|
|
$connectClassName = $usedClass->obtainClassNameToUse('SqlConnect'); |
|
76
|
|
|
|
|
77
|
|
|
$this->module->listBases[$baseKey] = new $connectClassName($baseInfos); |
|
78
|
|
|
$this->module->listBases[$baseKey]->createConnection(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.