|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Appserver\Provisioning\Steps\AbstractDatabaseStep |
|
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\Provisioning\Steps; |
|
22
|
|
|
|
|
23
|
|
|
use Doctrine\ORM\EntityManager; |
|
24
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
25
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* An abstract database step implementation. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
33
|
|
|
* @link https://github.com/appserver-io/appserver |
|
34
|
|
|
* @link http://www.appserver.io |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract class AbstractDatabaseStep extends AbstractStep |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The path to the Doctrine entities. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
const PARAM_PATH_TO_ENTITIES = 'pathToEntities'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The DB connection parameter with the path the database file. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
const CONNECTION_PARAM_PATH = 'path'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The DB connection parameter with the driver to use. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
const CONNECTION_PARAM_DRIVER = 'driver'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The DB connection parameter with user to connect. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
const CONNECTION_PARAM_USER = 'user'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The DB connection parameter with the passwort to connect. |
|
69
|
|
|
* |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
const CONNECTION_PARAM_PASSWORD = 'password'; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The DB connection parameter with the databaseName to connect. |
|
76
|
|
|
* |
|
77
|
|
|
* @var string |
|
78
|
|
|
*/ |
|
79
|
|
|
const CONNECTION_PARAM_DATABASENAME = 'dbname'; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* The DB connection parameter with the host to connect. |
|
83
|
|
|
* |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
const CONNECTION_PARAM_HOST = 'host'; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns an instance of the Doctrine Schema Tool. |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Doctrine\ORM\Tools\SchemaTool The Doctrine Schema Tool |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getSchemaTool() |
|
94
|
|
|
{ |
|
95
|
|
|
return new SchemaTool($this->getEntityManager()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Initializes and returns the Doctrine EntityManager instance. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Doctrine\ORM\EntityManager The Doctrine EntityManager instancer |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getEntityManager() |
|
104
|
|
|
{ |
|
105
|
|
|
|
|
106
|
|
|
// check if we have a valid datasource node |
|
107
|
|
|
if ($this->getDatasourceNode() == null) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// prepare the path to the entities |
|
112
|
|
|
$absolutePaths = array(); |
|
113
|
|
|
if ($relativePaths = $this->getStepNode()->getParam(AbstractDatabaseStep::PARAM_PATH_TO_ENTITIES)) { |
|
114
|
|
|
foreach (explode(PATH_SEPARATOR, $relativePaths) as $relativePath) { |
|
115
|
|
|
$absolutePaths[] = $this->getWebappPath() . DIRECTORY_SEPARATOR . $relativePath; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// load the database connection parameters |
|
120
|
|
|
$connectionParameters = $this->getConnectionParameters(); |
|
121
|
|
|
|
|
122
|
|
|
// initialize and load the entity manager and the schema tool |
|
123
|
|
|
$metadataConfiguration = Setup::createAnnotationMetadataConfiguration($absolutePaths, true, null, null, false); |
|
124
|
|
|
return EntityManager::create($connectionParameters, $metadataConfiguration); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Initializes an returns an array with the database connection parameters |
|
129
|
|
|
* necessary to connect to the database using Doctrine. |
|
130
|
|
|
* |
|
131
|
|
|
* @return array An array with the connection parameters |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getConnectionParameters() |
|
134
|
|
|
{ |
|
135
|
|
|
|
|
136
|
|
|
// load the datasource node |
|
137
|
|
|
$datasourceNode = $this->getDatasourceNode(); |
|
138
|
|
|
|
|
139
|
|
|
// initialize the database node |
|
140
|
|
|
$databaseNode = $datasourceNode->getDatabase(); |
|
141
|
|
|
|
|
142
|
|
|
// initialize the connection parameters |
|
143
|
|
|
$connectionParameters = array( |
|
144
|
|
|
AbstractDatabaseStep::CONNECTION_PARAM_DRIVER => $databaseNode->getDriver()->getNodeValue()->__toString(), |
|
145
|
|
|
AbstractDatabaseStep::CONNECTION_PARAM_USER => $databaseNode->getUser()->getNodeValue()->__toString(), |
|
146
|
|
|
AbstractDatabaseStep::CONNECTION_PARAM_PASSWORD => $databaseNode->getPassword()->getNodeValue()->__toString() |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
// initialize the path to the database when we use sqlite for example |
|
150
|
|
|
if ($databaseNode->getPath()) { |
|
151
|
|
|
if ($path = $databaseNode->getPath()->getNodeValue()->__toString()) { |
|
152
|
|
|
$connectionParameters[AbstractDatabaseStep::CONNECTION_PARAM_PATH] = $this->getWebappPath( |
|
153
|
|
|
) . DIRECTORY_SEPARATOR . $path; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// add database name if using another PDO driver than sqlite |
|
158
|
|
|
if ($databaseNode->getDatabaseName()) { |
|
159
|
|
|
$databaseName = $databaseNode->getDatabaseName()->getNodeValue()->__toString(); |
|
160
|
|
|
$connectionParameters[AbstractDatabaseStep::CONNECTION_PARAM_DATABASENAME] = $databaseName; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// add database host if using another PDO driver than sqlite |
|
164
|
|
|
if ($databaseNode->getDatabaseHost()) { |
|
165
|
|
|
$databaseHost = $databaseNode->getDatabaseHost()->getNodeValue()->__toString(); |
|
166
|
|
|
$connectionParameters[AbstractDatabaseStep::CONNECTION_PARAM_HOST] = $databaseHost; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// set the connection parameters |
|
170
|
|
|
return $connectionParameters; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|