|
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; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\Common\Cache\Cache; |
|
11
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
|
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
13
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
14
|
|
|
use Doctrine\ORM\Tools\SchemaValidator; |
|
15
|
|
|
use GravityMedia\Commander\Commander\TaskManager; |
|
16
|
|
|
use GravityMedia\Commander\Provider\CacheProvider; |
|
17
|
|
|
use GravityMedia\Commander\Provider\EntityManagerProvider; |
|
18
|
|
|
use GravityMedia\Commander\Provider\LoggerProvider; |
|
19
|
|
|
use GravityMedia\Commander\Provider\MappingDriverProvider; |
|
20
|
|
|
use GravityMedia\Commander\Provider\SchemaToolProvider; |
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Commander class. |
|
25
|
|
|
* |
|
26
|
|
|
* @package GravityMedia\Commander |
|
27
|
|
|
*/ |
|
28
|
|
|
class Commander |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* The entity namespace. |
|
32
|
|
|
*/ |
|
33
|
|
|
const ENTITY_NAMESPACE = 'GravityMedia\Commander\ORM'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The logger name. |
|
37
|
|
|
*/ |
|
38
|
|
|
const LOGGER_NAME = 'COMMANDER'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $initialized; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Config |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $config; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The cache. |
|
52
|
|
|
* |
|
53
|
|
|
* @var Cache |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $cache; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The mapping driver. |
|
59
|
|
|
* |
|
60
|
|
|
* @var MappingDriver |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $mappingDriver; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The entity manager. |
|
66
|
|
|
* |
|
67
|
|
|
* @var EntityManagerInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $entityManager; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The schema tool. |
|
73
|
|
|
* |
|
74
|
|
|
* @var SchemaTool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $schemaTool; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* The schema validator. |
|
80
|
|
|
* |
|
81
|
|
|
* @var SchemaValidator |
|
82
|
|
|
*/ |
|
83
|
|
|
protected $schemaValidator; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The logger. |
|
87
|
|
|
* |
|
88
|
|
|
* @var LoggerInterface |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $logger; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* The task manager. |
|
94
|
|
|
* |
|
95
|
|
|
* @var TaskManager |
|
96
|
|
|
*/ |
|
97
|
|
|
protected $taskManager; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create commander object. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Config $config |
|
103
|
|
|
*/ |
|
104
|
12 |
|
public function __construct(Config $config) |
|
105
|
|
|
{ |
|
106
|
12 |
|
$this->initialized = false; |
|
107
|
12 |
|
$this->config = $config; |
|
108
|
12 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Initialize commander. |
|
112
|
|
|
* |
|
113
|
|
|
* @return $this |
|
114
|
|
|
*/ |
|
115
|
8 |
|
public function initialize() |
|
116
|
|
|
{ |
|
117
|
8 |
|
if ($this->initialized) { |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
8 |
|
if ($this->getSchemaValidator()->schemaInSyncWithMetadata()) { |
|
122
|
6 |
|
$this->initialized = true; |
|
123
|
|
|
|
|
124
|
6 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
2 |
|
$classes = $this->getEntityManager()->getMetadataFactory()->getAllMetadata(); |
|
128
|
2 |
|
$this->getSchemaTool()->updateSchema($classes); |
|
129
|
|
|
|
|
130
|
2 |
|
$this->initialized = true; |
|
131
|
|
|
|
|
132
|
2 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get cache. |
|
137
|
|
|
* |
|
138
|
|
|
* @return Cache |
|
139
|
|
|
*/ |
|
140
|
8 |
|
public function getCache() |
|
141
|
|
|
{ |
|
142
|
8 |
|
if (null === $this->cache) { |
|
143
|
8 |
|
$provider = new CacheProvider($this->config); |
|
144
|
|
|
|
|
145
|
8 |
|
$this->cache = $provider->getCache(); |
|
146
|
4 |
|
} |
|
147
|
|
|
|
|
148
|
8 |
|
return $this->cache; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get mapping driver. |
|
153
|
|
|
* |
|
154
|
|
|
* @return MappingDriver |
|
155
|
|
|
*/ |
|
156
|
6 |
|
public function getMappingDriver() |
|
157
|
|
|
{ |
|
158
|
6 |
|
if (null === $this->mappingDriver) { |
|
159
|
6 |
|
$provider = new MappingDriverProvider($this->getCache()); |
|
160
|
|
|
|
|
161
|
6 |
|
$this->mappingDriver = $provider->getMappingDriver(); |
|
162
|
3 |
|
} |
|
163
|
|
|
|
|
164
|
6 |
|
return $this->mappingDriver; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get entity manager. |
|
169
|
|
|
* |
|
170
|
|
|
* @return EntityManagerInterface |
|
171
|
|
|
* |
|
172
|
|
|
* @throws \LogicException |
|
173
|
|
|
*/ |
|
174
|
6 |
|
public function getEntityManager() |
|
175
|
|
|
{ |
|
176
|
6 |
|
if (!$this->initialized) { |
|
177
|
2 |
|
throw new \LogicException('The entity manager is not available before initialization'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
4 |
|
if (null === $this->entityManager) { |
|
181
|
4 |
|
$provider = new EntityManagerProvider($this->config, $this->getCache(), $this->getMappingDriver()); |
|
182
|
|
|
|
|
183
|
4 |
|
$this->entityManager = $provider->getEntityManager(); |
|
184
|
2 |
|
} |
|
185
|
|
|
|
|
186
|
4 |
|
return $this->entityManager; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get schema tool. |
|
191
|
|
|
* |
|
192
|
|
|
* @return SchemaTool |
|
193
|
|
|
*/ |
|
194
|
2 |
|
public function getSchemaTool() |
|
195
|
|
|
{ |
|
196
|
2 |
|
if (null === $this->schemaTool) { |
|
197
|
2 |
|
$provider = new SchemaToolProvider($this->getEntityManager()); |
|
198
|
|
|
|
|
199
|
2 |
|
$this->schemaTool = $provider->getSchemaTool(); |
|
200
|
1 |
|
} |
|
201
|
|
|
|
|
202
|
2 |
|
return $this->schemaTool; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get schema validator. |
|
207
|
|
|
* |
|
208
|
|
|
* @return SchemaValidator |
|
209
|
|
|
*/ |
|
210
|
2 |
|
public function getSchemaValidator() |
|
211
|
|
|
{ |
|
212
|
2 |
|
if (null === $this->schemaValidator) { |
|
213
|
2 |
|
$provider = new SchemaToolProvider($this->getEntityManager()); |
|
214
|
|
|
|
|
215
|
2 |
|
$this->schemaValidator = $provider->getSchemaValidator(); |
|
216
|
1 |
|
} |
|
217
|
|
|
|
|
218
|
2 |
|
return $this->schemaValidator; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get logger. |
|
223
|
|
|
* |
|
224
|
|
|
* @return LoggerInterface |
|
225
|
|
|
*/ |
|
226
|
2 |
|
public function getLogger() |
|
227
|
|
|
{ |
|
228
|
2 |
|
if (null === $this->logger) { |
|
229
|
2 |
|
$provider = new LoggerProvider($this->config); |
|
230
|
|
|
|
|
231
|
2 |
|
$this->logger = $provider->getLogger(); |
|
232
|
1 |
|
} |
|
233
|
|
|
|
|
234
|
2 |
|
return $this->logger; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get task manager. |
|
239
|
|
|
* |
|
240
|
|
|
* @return TaskManager |
|
241
|
|
|
*/ |
|
242
|
2 |
|
public function getTaskManager() |
|
243
|
|
|
{ |
|
244
|
2 |
|
if (null === $this->taskManager) { |
|
245
|
2 |
|
$this->taskManager = new TaskManager($this->getEntityManager()); |
|
246
|
1 |
|
} |
|
247
|
|
|
|
|
248
|
2 |
|
return $this->taskManager; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|