1
|
|
|
<?php namespace Comodojo\Extender\Components; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
4
|
|
|
use \Comodojo\Extender\Traits\ConfigurationTrait; |
5
|
|
|
use \Doctrine\ORM\Tools\Setup; |
6
|
|
|
use \Doctrine\ORM\EntityManager; |
7
|
|
|
use \Doctrine\DBAL\DriverManager; |
8
|
|
|
use \Doctrine\DBAL\Configuration as DoctrineConfiguration; |
9
|
|
|
use \Doctrine\ORM\Query\ResultSetMappingBuilder; |
10
|
|
|
use \Doctrine\Common\Cache\ApcCache; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package Comodojo Extender |
14
|
|
|
* @author Marco Giovinazzi <[email protected]> |
15
|
|
|
* @license MIT |
16
|
|
|
* |
17
|
|
|
* LICENSE: |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25
|
|
|
* THE SOFTWARE. |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
class Database { |
29
|
|
|
|
30
|
|
|
use ConfigurationTrait; |
31
|
|
|
|
32
|
|
|
protected $entity_manager; |
33
|
|
|
|
34
|
|
|
public function __construct(Configuration $configuration) { |
35
|
|
|
|
36
|
|
|
$this->setConfiguration($configuration); |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getConnection() { |
41
|
|
|
|
42
|
|
|
$configuration = $this->getConfiguration(); |
43
|
|
|
$connection_params = self::getConnectionParameters($configuration); |
44
|
|
|
$driverConfig = new DoctrineConfiguration(); |
45
|
|
|
|
46
|
|
|
$devmode = $configuration->get('database-devmode') === true ? true : false; |
47
|
|
|
|
48
|
|
|
// currently only ApcCache driver is supported |
49
|
|
|
if ( empty($devmode) ) { |
50
|
|
|
|
51
|
|
|
$cache = new ApcCache(); |
|
|
|
|
52
|
|
|
$driverConfig->setResultCacheImpl($cache); |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return DriverManager::getConnection($connection_params, $driverConfig); |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getEntityManager() { |
61
|
|
|
|
62
|
|
|
if ( $this->entity_manager === null ) { |
63
|
|
|
|
64
|
|
|
$configuration = $this->getConfiguration(); |
65
|
|
|
|
66
|
|
|
$connection_params = self::getConnectionParameters($configuration); |
67
|
|
|
|
68
|
|
|
$entity_repositories = self::getEntityRepositories($configuration); |
69
|
|
|
|
70
|
|
|
$devmode = $configuration->get('database-devmode') === true ? true : false; |
71
|
|
|
$proxies_folder = $configuration->get('database-proxies'); |
72
|
|
|
|
73
|
|
|
$base_folder = $configuration->get('base-path'); |
74
|
|
|
|
75
|
|
|
$driverConfig = new DoctrineConfiguration(); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$db_config = Setup::createAnnotationMetadataConfiguration( |
78
|
|
|
$entity_repositories, |
79
|
|
|
$devmode, |
80
|
|
|
"$base_folder/$proxies_folder", |
81
|
|
|
null, |
82
|
|
|
false |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
if ( $devmode ) { |
86
|
|
|
|
87
|
|
|
$db_config->setAutoGenerateProxyClasses(true); |
88
|
|
|
|
89
|
|
|
} else { |
90
|
|
|
|
91
|
|
|
$cache = new ApcCache(); |
|
|
|
|
92
|
|
|
$db_config->setAutoGenerateProxyClasses(false); |
93
|
|
|
$db_config->setQueryCacheImpl($cache); |
94
|
|
|
$db_config->setResultCacheImpl($cache); |
95
|
|
|
$db_config->setMetadataCacheImpl($cache); |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->entity_manager = EntityManager::create($connection_params, $db_config); |
100
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
|
103
|
|
|
$this->entity_manager->clear(); |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->entity_manager; |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function setEntityManager(EntityManager $manager) { |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->entity_manager = $manager; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public static function init(Configuration $configuration) { |
120
|
|
|
|
121
|
|
|
return new Database($configuration); |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected static function getConnectionParameters(Configuration $configuration) { |
126
|
|
|
|
127
|
|
|
$connection_params = $configuration->get('database-params'); |
128
|
|
|
$base_folder = $configuration->get('base-path'); |
129
|
|
|
|
130
|
|
|
if ( isset($connection_params['path']) ) $connection_params['path'] = $base_folder."/".$connection_params['path']; |
131
|
|
|
|
132
|
|
|
return $connection_params; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected static function getEntityRepositories(Configuration $configuration) { |
137
|
|
|
|
138
|
|
|
$base_folder = $configuration->get('base-path'); |
139
|
|
|
$repos = $configuration->get('database-repositories'); |
140
|
|
|
|
141
|
|
|
return array_map(function($repo) use ($base_folder) { |
142
|
|
|
return "$base_folder/$repo"; |
143
|
|
|
}, $repos); |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.