1
|
|
|
<?php namespace Comodojo\Extender\Components; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
4
|
|
|
use \Comodojo\Foundation\Base\ConfigurationTrait; |
5
|
|
|
use \Doctrine\ORM\Tools\Setup; |
6
|
|
|
use \Doctrine\ORM\Tools\SchemaValidator; |
7
|
|
|
use \Doctrine\ORM\EntityManager; |
8
|
|
|
use \Doctrine\DBAL\DriverManager; |
9
|
|
|
use \Doctrine\DBAL\Configuration as DoctrineConfiguration; |
10
|
|
|
use \Doctrine\ORM\Query\ResultSetMappingBuilder; |
11
|
|
|
use \Doctrine\Common\Cache\ApcCache; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @package Comodojo Extender |
15
|
|
|
* @author Marco Giovinazzi <[email protected]> |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* LICENSE: |
19
|
|
|
* |
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
23
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
26
|
|
|
* THE SOFTWARE. |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
class Database { |
30
|
|
|
|
31
|
|
|
use ConfigurationTrait; |
32
|
|
|
|
33
|
|
|
protected $entity_manager; |
34
|
|
|
|
35
|
|
|
public function __construct(Configuration $configuration) { |
36
|
|
|
|
37
|
|
|
$this->setConfiguration($configuration); |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getConnection() { |
42
|
|
|
|
43
|
|
|
$configuration = $this->getConfiguration(); |
44
|
|
|
$connection_params = self::getConnectionParameters($configuration); |
45
|
|
|
$driverConfig = new DoctrineConfiguration(); |
46
|
|
|
|
47
|
|
|
$devmode = (bool) $configuration->get('database-devmode'); |
48
|
|
|
$database_cache = $configuration->get('database-cache'); |
49
|
|
|
|
50
|
|
|
// currently only ApcCache driver is supported |
51
|
|
|
if ( |
52
|
|
|
$devmode === false && |
53
|
|
|
strcasecmp('apc', $database_cache) === 0 |
54
|
|
|
// && php_sapi_name() !== 'cli' |
55
|
|
|
) { |
56
|
|
|
|
57
|
|
|
$cache = new ApcCache(); |
|
|
|
|
58
|
|
|
$driverConfig->setResultCacheImpl($cache); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return DriverManager::getConnection($connection_params, $driverConfig); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getEntityManager() { |
67
|
|
|
|
68
|
|
|
if ( $this->entity_manager === null ) { |
69
|
|
|
|
70
|
|
|
$configuration = $this->getConfiguration(); |
71
|
|
|
|
72
|
|
|
$base_folder = $configuration->get('base-path'); |
|
|
|
|
73
|
|
|
$connection_params = self::getConnectionParameters($configuration); |
74
|
|
|
$entity_repositories = self::getEntityRepositories($configuration); |
75
|
|
|
$proxies_folder = self::getProxiesFolder($configuration); |
76
|
|
|
$devmode = (bool) $configuration->get('database-devmode'); |
77
|
|
|
$metadata_mode = $configuration->get('database-metadata'); |
78
|
|
|
$database_cache = $configuration->get('database-cache'); |
79
|
|
|
|
80
|
|
|
$config_args = [ |
81
|
|
|
$entity_repositories, |
82
|
|
|
$devmode, |
83
|
|
|
$proxies_folder, |
84
|
|
|
null, |
85
|
|
|
false |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
switch (strtoupper($metadata_mode)) { |
89
|
|
|
|
90
|
|
|
case 'YAML': |
91
|
|
|
$db_config = Setup::createYAMLMetadataConfiguration(...$config_args); |
|
|
|
|
92
|
|
|
break; |
93
|
|
|
|
94
|
|
|
case 'XML': |
95
|
|
|
$db_config = Setup::createXMLMetadataConfiguration(...$config_args); |
|
|
|
|
96
|
|
|
break; |
97
|
|
|
|
98
|
|
|
case 'ANNOTATIONS': |
99
|
|
|
default: |
100
|
|
|
$db_config = Setup::createAnnotationMetadataConfiguration(...$config_args); |
|
|
|
|
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ( $devmode === false ) { |
105
|
|
|
|
106
|
|
|
$db_config->setAutoGenerateProxyClasses(false); |
107
|
|
|
|
108
|
|
|
if ( |
109
|
|
|
strcasecmp('apc', $database_cache) === 0 |
110
|
|
|
// && php_sapi_name() !== 'cli' |
111
|
|
|
) { |
112
|
|
|
$cache = new ApcCache(); |
|
|
|
|
113
|
|
|
$db_config->setQueryCacheImpl($cache); |
114
|
|
|
$db_config->setResultCacheImpl($cache); |
115
|
|
|
$db_config->setMetadataCacheImpl($cache); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} else { |
119
|
|
|
|
120
|
|
|
$db_config->setAutoGenerateProxyClasses(true); |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->entity_manager = EntityManager::create($connection_params, $db_config); |
125
|
|
|
|
126
|
|
|
} else { |
127
|
|
|
|
128
|
|
|
$this->entity_manager->clear(); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->entity_manager; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setEntityManager(EntityManager $manager) { |
137
|
|
|
|
138
|
|
|
$this->entity_manager = $manager; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public static function init(Configuration $configuration) { |
145
|
|
|
|
146
|
|
|
return new Database($configuration); |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function validate(Configuration $configuration) { |
151
|
|
|
|
152
|
|
|
$db = new Database($configuration); |
153
|
|
|
|
154
|
|
|
$em = $db->getEntityManager(); |
155
|
|
|
$validator = new SchemaValidator($em); |
156
|
|
|
|
157
|
|
|
$result = [ |
158
|
|
|
'MAPPING' => empty($validator->validateMapping()), |
159
|
|
|
'SYNC' => $validator->schemaInSyncWithMetadata() |
160
|
|
|
]; |
161
|
|
|
|
162
|
|
|
unset($validator); |
163
|
|
|
$em->getConnection()->close(); |
164
|
|
|
$em->close(); |
165
|
|
|
unset($db); |
166
|
|
|
|
167
|
|
|
return $result; |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected static function getConnectionParameters(Configuration $configuration) { |
172
|
|
|
|
173
|
|
|
$connection_params = $configuration->get('database-params'); |
174
|
|
|
$base_folder = $configuration->get('base-path'); |
175
|
|
|
|
176
|
|
|
if ( isset($connection_params['path']) ) $connection_params['path'] = $base_folder."/".$connection_params['path']; |
177
|
|
|
|
178
|
|
|
return $connection_params; |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected static function getEntityRepositories(Configuration $configuration) { |
183
|
|
|
|
184
|
|
|
$base_folder = $configuration->get('base-path'); |
185
|
|
|
$repos = $configuration->get('database-repositories'); |
186
|
|
|
|
187
|
|
|
return array_map(function($repo) use ($base_folder) { |
188
|
|
|
return substr($repo, 0, 1 ) === "/" ? $repo : "$base_folder/$repo"; |
189
|
|
|
}, $repos); |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected static function getProxiesFolder(Configuration $configuration) { |
194
|
|
|
|
195
|
|
|
$base_folder = $configuration->get('base-path'); |
196
|
|
|
$folder = $configuration->get('database-proxies'); |
197
|
|
|
|
198
|
|
|
return substr($folder, 0, 1 ) === "/" ? $folder : "$base_folder/$folder"; |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|