|
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\ORM\Configuration as EntityManagerConfig; |
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
|
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
16
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
|
17
|
|
|
use Gedmo\Timestampable\TimestampableListener; |
|
18
|
|
|
use GravityMedia\Commander\Commander; |
|
19
|
|
|
use GravityMedia\Commander\Config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Entity manager provider class. |
|
23
|
|
|
* |
|
24
|
|
|
* @package GravityMedia\Commander\Provider |
|
25
|
|
|
*/ |
|
26
|
|
|
class EntityManagerProvider |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The config. |
|
30
|
|
|
* |
|
31
|
|
|
* @var Config |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The cache. |
|
37
|
|
|
* |
|
38
|
|
|
* @var Cache |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $cache; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The mapping driver. |
|
44
|
|
|
* |
|
45
|
|
|
* @var MappingDriver |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $mappingDriver; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The entity manager config. |
|
51
|
|
|
* |
|
52
|
|
|
* @var EntityManagerConfig |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $entityManagerConfig; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The entity manager. |
|
58
|
|
|
* |
|
59
|
|
|
* @var EntityManagerInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $entityManager; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create entity manager provider object. |
|
65
|
|
|
* |
|
66
|
|
|
* @param Config $config |
|
67
|
|
|
* @param Cache $cache |
|
68
|
|
|
* @param MappingDriver $mappingDriver |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct(Config $config, Cache $cache, MappingDriver $mappingDriver) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->config = $config; |
|
73
|
|
|
$this->cache = $cache; |
|
74
|
|
|
$this->mappingDriver = $mappingDriver; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get entity manager config. |
|
79
|
|
|
* |
|
80
|
|
|
* @return EntityManagerConfig |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getEntityManagerConfig() |
|
83
|
|
|
{ |
|
84
|
|
|
if (null === $this->entityManagerConfig) { |
|
85
|
|
|
$cacheDirectory = $this->config->getCacheDirectory(); |
|
86
|
|
|
if (null === $cacheDirectory) { |
|
87
|
|
|
$cacheDirectory = sys_get_temp_dir(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$config = new EntityManagerConfig(); |
|
91
|
|
|
$config->setAutoGenerateProxyClasses(true); |
|
92
|
|
|
$config->setProxyDir(rtrim($cacheDirectory, '/') . '/proxy'); |
|
93
|
|
|
$config->setProxyNamespace(Commander::ENTITY_NAMESPACE); |
|
94
|
|
|
$config->setMetadataDriverImpl($this->mappingDriver); |
|
95
|
|
|
$config->setMetadataCacheImpl($this->cache); |
|
96
|
|
|
$config->setQueryCacheImpl($this->cache); |
|
97
|
|
|
$config->setResultCacheImpl($this->cache); |
|
98
|
|
|
$config->setHydrationCacheImpl($this->cache); |
|
99
|
|
|
|
|
100
|
|
|
$this->entityManagerConfig = $config; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->entityManagerConfig; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get entity manager. |
|
108
|
|
|
* |
|
109
|
|
|
* @return EntityManagerInterface |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getEntityManager() |
|
112
|
|
|
{ |
|
113
|
|
|
if (null === $this->entityManager) { |
|
114
|
|
|
$connection = [ |
|
115
|
|
|
'driver' => 'pdo_sqlite', |
|
116
|
|
|
'path' => $this->config->getDatabaseFilePath() |
|
117
|
|
|
]; |
|
118
|
|
|
|
|
119
|
|
|
$config = $this->getEntityManagerConfig(); |
|
120
|
|
|
$metadataDriver = $config->getMetadataDriverImpl(); |
|
121
|
|
|
|
|
122
|
|
|
$timestampableListener = new TimestampableListener(); |
|
123
|
|
|
if ($metadataDriver instanceof AnnotationDriver) { |
|
124
|
|
|
$timestampableListener->setAnnotationReader($metadataDriver->getReader()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$eventManager = new EventManager(); |
|
128
|
|
|
$eventManager->addEventSubscriber($timestampableListener); |
|
129
|
|
|
|
|
130
|
|
|
$this->entityManager = EntityManager::create($connection, $config, $eventManager); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->entityManager; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|