1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Commander project. |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Commander\Provider; |
9
|
|
|
|
10
|
|
|
use Doctrine\Common\Cache\Cache; |
11
|
|
|
use Doctrine\Common\EventManager; |
12
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
13
|
|
|
use Doctrine\DBAL\Connection; |
14
|
|
|
use Doctrine\DBAL\DriverManager; |
15
|
|
|
use Doctrine\ORM\Configuration as EntityManagerConfig; |
16
|
|
|
use Doctrine\ORM\EntityManager; |
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
18
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
19
|
|
|
use Gedmo\Timestampable\TimestampableListener; |
20
|
|
|
use GravityMedia\Commander\Commander; |
21
|
|
|
use GravityMedia\Commander\Config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Entity manager provider class. |
25
|
|
|
* |
26
|
|
|
* @package GravityMedia\Commander\Provider |
27
|
|
|
*/ |
28
|
|
|
class EntityManagerProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The config. |
32
|
|
|
* |
33
|
|
|
* @var Config |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The cache. |
39
|
|
|
* |
40
|
|
|
* @var Cache |
41
|
|
|
*/ |
42
|
|
|
protected $cache; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The mapping driver. |
46
|
|
|
* |
47
|
|
|
* @var MappingDriver |
48
|
|
|
*/ |
49
|
|
|
protected $mappingDriver; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The entity manager config. |
53
|
|
|
* |
54
|
|
|
* @var EntityManagerConfig |
55
|
|
|
*/ |
56
|
|
|
protected $entityManagerConfig; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The event manager. |
60
|
|
|
* |
61
|
|
|
* @var EventManager |
62
|
|
|
*/ |
63
|
|
|
protected $eventManager; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The connection. |
67
|
|
|
* |
68
|
|
|
* @var Connection |
69
|
|
|
*/ |
70
|
|
|
protected $connection; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The entity manager. |
74
|
|
|
* |
75
|
|
|
* @var EntityManagerInterface |
76
|
|
|
*/ |
77
|
|
|
protected $entityManager; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create entity manager provider object. |
81
|
|
|
* |
82
|
|
|
* @param Config $config |
83
|
|
|
* @param Cache $cache |
84
|
|
|
* @param MappingDriver $mappingDriver |
85
|
|
|
*/ |
86
|
8 |
|
public function __construct(Config $config, Cache $cache, MappingDriver $mappingDriver) |
87
|
|
|
{ |
88
|
8 |
|
$this->config = $config; |
89
|
8 |
|
$this->cache = $cache; |
90
|
8 |
|
$this->mappingDriver = $mappingDriver; |
91
|
8 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get entity manager config. |
95
|
|
|
* |
96
|
|
|
* @return EntityManagerConfig |
97
|
|
|
*/ |
98
|
8 |
|
public function getEntityManagerConfig() |
99
|
|
|
{ |
100
|
8 |
|
if (null === $this->entityManagerConfig) { |
101
|
8 |
|
$cacheDirectory = $this->config->getCacheDirectory(); |
102
|
8 |
|
if (null === $cacheDirectory) { |
103
|
8 |
|
$cacheDirectory = sys_get_temp_dir(); |
104
|
4 |
|
} |
105
|
|
|
|
106
|
8 |
|
$config = new EntityManagerConfig(); |
107
|
8 |
|
$config->setAutoGenerateProxyClasses(true); |
108
|
8 |
|
$config->setProxyDir(rtrim($cacheDirectory, '/') . '/proxy'); |
109
|
8 |
|
$config->setProxyNamespace(Commander::ENTITY_NAMESPACE); |
110
|
8 |
|
$config->setMetadataDriverImpl($this->mappingDriver); |
111
|
8 |
|
$config->setMetadataCacheImpl($this->cache); |
112
|
8 |
|
$config->setQueryCacheImpl($this->cache); |
113
|
8 |
|
$config->setResultCacheImpl($this->cache); |
114
|
8 |
|
$config->setHydrationCacheImpl($this->cache); |
115
|
|
|
|
116
|
8 |
|
$this->entityManagerConfig = $config; |
117
|
4 |
|
} |
118
|
|
|
|
119
|
8 |
|
return $this->entityManagerConfig; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get event manager. |
124
|
|
|
* |
125
|
|
|
* @return EventManager |
126
|
|
|
*/ |
127
|
6 |
|
public function getEventManager() |
128
|
|
|
{ |
129
|
6 |
|
if (null === $this->eventManager) { |
130
|
6 |
|
$metadataDriver = $this->getEntityManagerConfig()->getMetadataDriverImpl(); |
131
|
|
|
|
132
|
6 |
|
$timestampableListener = new TimestampableListener(); |
133
|
6 |
|
if ($metadataDriver instanceof AnnotationDriver) { |
134
|
2 |
|
$timestampableListener->setAnnotationReader($metadataDriver->getReader()); |
135
|
1 |
|
} |
136
|
|
|
|
137
|
6 |
|
$eventManager = new EventManager(); |
138
|
6 |
|
$eventManager->addEventSubscriber($timestampableListener); |
139
|
|
|
|
140
|
6 |
|
$this->eventManager = $eventManager; |
141
|
3 |
|
} |
142
|
|
|
|
143
|
6 |
|
return $this->eventManager; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get connection. |
148
|
|
|
* |
149
|
|
|
* @return Connection |
150
|
|
|
*/ |
151
|
4 |
|
public function getConnection() |
152
|
|
|
{ |
153
|
4 |
|
if (null === $this->connection) { |
154
|
|
|
$params = [ |
155
|
4 |
|
'driver' => 'pdo_sqlite', |
156
|
4 |
|
'path' => $this->config->getDatabaseFilePath() |
157
|
2 |
|
]; |
158
|
|
|
|
159
|
4 |
|
$config = $this->getEntityManagerConfig(); |
160
|
|
|
|
161
|
4 |
|
$eventManager = $this->getEventManager(); |
162
|
|
|
|
163
|
4 |
|
$this->connection = DriverManager::getConnection($params, $config, $eventManager); |
164
|
2 |
|
} |
165
|
|
|
|
166
|
4 |
|
return $this->connection; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get entity manager. |
171
|
|
|
* |
172
|
|
|
* @return EntityManagerInterface |
173
|
|
|
*/ |
174
|
2 |
|
public function getEntityManager() |
175
|
|
|
{ |
176
|
2 |
|
if (null === $this->entityManager) { |
177
|
2 |
|
$connection = $this->getConnection(); |
178
|
|
|
|
179
|
2 |
|
$config = $this->getEntityManagerConfig(); |
180
|
|
|
|
181
|
2 |
|
$eventManager = $this->getEventManager(); |
182
|
|
|
|
183
|
2 |
|
$this->entityManager = EntityManager::create($connection, $config, $eventManager); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
2 |
|
return $this->entityManager; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|