1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Appserver\Doctrine\Utils\ConnectionUtil |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/appserver |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Appserver\Doctrine\Utils; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Lang\String; |
24
|
|
|
use AppserverIo\Lang\Boolean; |
25
|
|
|
use AppserverIo\Psr\Application\ApplicationInterface; |
26
|
|
|
use AppserverIo\Psr\ApplicationServer\Configuration\DatabaseConfigurationInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Utility class that helps to prepare the Doctrine DBAL connections. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/appserver-io/appserver |
35
|
|
|
* @link http://www.appserver.io |
36
|
|
|
*/ |
37
|
|
|
class ConnectionUtil |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The application instance. |
42
|
|
|
* |
43
|
|
|
* @var \AppserverIo\Psr\Application\ApplicationInterface |
44
|
|
|
*/ |
45
|
|
|
protected $application; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* This is a utility class, so protect it against direct instantiation. |
49
|
|
|
* |
50
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
51
|
|
|
*/ |
52
|
|
|
private function __construct(ApplicationInterface $application) |
53
|
|
|
{ |
54
|
|
|
$this->application = $application; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* This is a utility class, so protect it against cloning. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
private function __clone() |
63
|
|
|
{ |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the application instance. |
68
|
|
|
* |
69
|
|
|
* @return \AppserverIo\Psr\Application\ApplicationInterface The application instance |
70
|
|
|
*/ |
71
|
|
|
public function getApplication() |
72
|
|
|
{ |
73
|
|
|
return $this->application; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates a new helper instance. |
78
|
|
|
* |
79
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
80
|
|
|
* |
81
|
|
|
* @return \AppserverIo\Appserver\Doctrine\Utils\ConnectionUtil The instance |
82
|
|
|
*/ |
83
|
|
|
public static function get(ApplicationInterface $application) |
84
|
|
|
{ |
85
|
|
|
return new ConnectionUtil($application); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates an array with the connection parameters for a Doctrine DBAL connection from |
90
|
|
|
* the passed database node. |
91
|
|
|
* |
92
|
|
|
* @param \AppserverIo\Psr\ApplicationServer\Configuration\DatabaseConfigurationInterface $databaseNode The database node to create the connection parameters from |
93
|
|
|
* |
94
|
|
|
* @return array The DBAL connection parameters |
95
|
|
|
*/ |
96
|
|
|
public function fromDatabaseNode(DatabaseConfigurationInterface $databaseNode) |
97
|
|
|
{ |
98
|
|
|
|
99
|
|
|
// initialize the connection parameters with the mandatory driver |
100
|
|
|
$connectionParameters = array( |
101
|
|
|
'driver' => $databaseNode->getDriver()->getNodeValue()->__toString(), |
102
|
|
|
'encryption' => 'none' |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
// initialize the path/memory to the database when we use sqlite for example |
106
|
|
|
if ($pathNode = $databaseNode->getPath()) { |
107
|
|
|
$connectionParameters['path'] = $pathNode->getNodeValue()->__toString(); |
108
|
|
|
} elseif ($memoryNode = $databaseNode->getMemory()) { |
109
|
|
|
$connectionParameters['memory'] = Boolean::valueOf(new String($memoryNode->getNodeValue()->__toString()))->booleanValue(); |
110
|
|
|
} else { |
111
|
|
|
// do nothing here, because there is NO option |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// add username, if specified |
115
|
|
|
if ($userNode = $databaseNode->getUser()) { |
116
|
|
|
$connectionParameters['user'] = $userNode->getNodeValue()->__toString(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// add password, if specified |
120
|
|
|
if ($passwordNode = $databaseNode->getPassword()) { |
121
|
|
|
$connectionParameters['password'] = $passwordNode->getNodeValue()->__toString(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// add database name if using another PDO driver than sqlite |
125
|
|
|
if ($databaseNameNode = $databaseNode->getDatabaseName()) { |
126
|
|
|
$connectionParameters['dbname'] = $databaseNameNode->getNodeValue()->__toString(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// add database host if using another PDO driver than sqlite |
130
|
|
|
if ($databaseHostNode = $databaseNode->getDatabaseHost()) { |
131
|
|
|
$connectionParameters['host'] = $databaseHostNode->getNodeValue()->__toString(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// add database port if using another PDO driver than sqlite |
135
|
|
|
if ($databasePortNode = $databaseNode->getDatabasePort()) { |
136
|
|
|
$connectionParameters['port'] = $databasePortNode->getNodeValue()->__toString(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// adds the optional encryption option |
140
|
|
|
if ($encryptionNode = $databaseNode->getEncryption()) { |
141
|
|
|
$connectionParameters['encryption'] = $encryptionNode->getNodeValue()->__toString(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// add charset, if specified |
145
|
|
|
if ($charsetNode = $databaseNode->getCharset()) { |
146
|
|
|
$connectionParameters['charset'] = $charsetNode->getNodeValue()->__toString(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// add driver options, if specified |
150
|
|
|
if ($unixSocketNode = $databaseNode->getUnixSocket()) { |
151
|
|
|
$connectionParameters['unix_socket'] = $unixSocketNode->getNodeValue()->__toString(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// add server version, if specified |
155
|
|
|
if ($serverVersionNode = $databaseNode->getServerVersion()) { |
156
|
|
|
$connectionParameters['server_version'] = $serverVersionNode->getNodeValue()->__toString(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// set platform, if specified |
160
|
|
|
if ($platformNode = $databaseNode->getPlatform()) { |
161
|
|
|
$platform = $platformNode->getNodeValue()->__toString(); |
162
|
|
|
$connectionParameters['platform'] = new $platform(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// add driver options, if specified |
166
|
|
|
if ($driverOptionsNode = $databaseNode->getDriverOptions()) { |
167
|
|
|
// explode the raw options separated with a semicolon |
168
|
|
|
$rawOptions = explode(';', $driverOptionsNode->getNodeValue()->__toString()); |
169
|
|
|
|
170
|
|
|
// prepare the array with the driver options key/value pair (separated with a =) |
171
|
|
|
$options = array(); |
172
|
|
|
foreach ($rawOptions as $rawOption) { |
173
|
|
|
list ($key, $value) = explode('=', $rawOption); |
174
|
|
|
$options[$key] = $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// set the driver options |
178
|
|
|
$connectionParameters['driverOptions'] = $options; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// returns the connection parameters |
182
|
|
|
return $connectionParameters; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|