|
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\Annotations\AnnotationReader; |
|
11
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
12
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
|
13
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
14
|
|
|
use Doctrine\Common\Cache\Cache; |
|
15
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
|
16
|
|
|
use Doctrine\Common\EventManager; |
|
17
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
|
18
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
|
19
|
|
|
use Doctrine\ORM\Configuration; |
|
20
|
|
|
use Doctrine\ORM\EntityManager; |
|
21
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
22
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
|
23
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
24
|
|
|
use Doctrine\ORM\Tools\SchemaValidator; |
|
25
|
|
|
use Gedmo\DoctrineExtensions; |
|
26
|
|
|
use Gedmo\Timestampable\TimestampableListener; |
|
27
|
|
|
use GravityMedia\Commander\Commander\TaskManager; |
|
28
|
|
|
use Monolog\Handler\StreamHandler; |
|
29
|
|
|
use Monolog\Logger; |
|
30
|
|
|
use Psr\Log\LoggerInterface; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Commander class. |
|
34
|
|
|
* |
|
35
|
|
|
* @package GravityMedia\Commander |
|
36
|
|
|
*/ |
|
37
|
|
|
class Commander |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* The entity namespace. |
|
41
|
|
|
*/ |
|
42
|
|
|
const ENTITY_NAMESPACE = 'GravityMedia\Commander\ORM'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Config |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $config; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The cache. |
|
51
|
|
|
* |
|
52
|
|
|
* @var Cache |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $cache; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The mapping driver. |
|
58
|
|
|
* |
|
59
|
|
|
* @var MappingDriver |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $mappingDriver; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The entity manager config. |
|
65
|
|
|
* |
|
66
|
|
|
* @var Configuration |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $entityManagerConfig; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The entity manager. |
|
72
|
|
|
* |
|
73
|
|
|
* @var EntityManagerInterface |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $entityManager; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The schema validator. |
|
79
|
|
|
* |
|
80
|
|
|
* @var SchemaValidator |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $schemaValidator; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The schema tool. |
|
86
|
|
|
* |
|
87
|
|
|
* @var SchemaTool |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $schemaTool; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* The logger. |
|
93
|
|
|
* |
|
94
|
|
|
* @var LoggerInterface |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $logger; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* The task manager. |
|
100
|
|
|
* |
|
101
|
|
|
* @var TaskManager |
|
102
|
|
|
*/ |
|
103
|
|
|
protected $taskManager; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Create commander object. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Config $config |
|
109
|
|
|
*/ |
|
110
|
|
|
public function __construct(Config $config) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->config = $config; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get cache. |
|
117
|
|
|
* |
|
118
|
|
|
* @return Cache |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getCache() |
|
121
|
|
|
{ |
|
122
|
|
|
if (null === $this->cache) { |
|
123
|
|
|
$directory = $this->config->getDatabaseCacheDirectory(); |
|
124
|
|
|
if (null === $directory) { |
|
125
|
|
|
$this->cache = new ArrayCache(); |
|
126
|
|
|
|
|
127
|
|
|
return $this->cache; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->cache = new FilesystemCache($directory); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->cache; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get mapping driver. |
|
138
|
|
|
* |
|
139
|
|
|
* @return MappingDriver |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getMappingDriver() |
|
142
|
|
|
{ |
|
143
|
|
|
if (null === $this->mappingDriver) { |
|
144
|
|
|
AnnotationRegistry::registerFile( |
|
145
|
|
|
__DIR__ . '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
$driverChain = new MappingDriverChain(); |
|
149
|
|
|
$reader = new CachedReader(new AnnotationReader(), $this->getCache()); |
|
150
|
|
|
|
|
151
|
|
|
DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $reader); |
|
152
|
|
|
|
|
153
|
|
|
$annotationDriver = new AnnotationDriver($reader, [__DIR__ . '/ORM']); |
|
|
|
|
|
|
154
|
|
|
$driverChain->addDriver($annotationDriver, static::ENTITY_NAMESPACE); |
|
155
|
|
|
|
|
156
|
|
|
$this->mappingDriver = $driverChain; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this->mappingDriver; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get entity manager config. |
|
164
|
|
|
* |
|
165
|
|
|
* @return Configuration |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getEntityManagerConfig() |
|
168
|
|
|
{ |
|
169
|
|
|
if (null === $this->entityManagerConfig) { |
|
170
|
|
|
$proxyDirectory = $this->config->getDatabaseCacheDirectory(); |
|
171
|
|
|
if (null === $proxyDirectory) { |
|
172
|
|
|
$proxyDirectory = sys_get_temp_dir(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$config = new Configuration(); |
|
176
|
|
|
$config->setAutoGenerateProxyClasses(true); |
|
177
|
|
|
$config->setProxyDir($proxyDirectory); |
|
178
|
|
|
$config->setProxyNamespace(static::ENTITY_NAMESPACE); |
|
179
|
|
|
$config->setMetadataDriverImpl($this->getMappingDriver()); |
|
180
|
|
|
$config->setMetadataCacheImpl($this->getCache()); |
|
181
|
|
|
$config->setQueryCacheImpl($this->getCache()); |
|
182
|
|
|
$config->setResultCacheImpl($this->getCache()); |
|
183
|
|
|
$config->setHydrationCacheImpl($this->getCache()); |
|
184
|
|
|
|
|
185
|
|
|
$this->entityManagerConfig = $config; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $this->entityManagerConfig; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Get entity manager. |
|
193
|
|
|
* |
|
194
|
|
|
* @return EntityManagerInterface |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getEntityManager() |
|
197
|
|
|
{ |
|
198
|
|
|
if (null === $this->entityManager) { |
|
199
|
|
|
$connection = [ |
|
200
|
|
|
'driver' => 'pdo_sqlite', |
|
201
|
|
|
'path' => $this->config->getDatabaseFilePath() |
|
202
|
|
|
]; |
|
203
|
|
|
|
|
204
|
|
|
$config = $this->getEntityManagerConfig(); |
|
205
|
|
|
$metadataDriver = $config->getMetadataDriverImpl(); |
|
206
|
|
|
|
|
207
|
|
|
$timestampableListener = new TimestampableListener(); |
|
208
|
|
|
if ($metadataDriver instanceof AnnotationDriver) { |
|
209
|
|
|
$timestampableListener->setAnnotationReader($metadataDriver->getReader()); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$eventManager = new EventManager(); |
|
213
|
|
|
$eventManager->addEventSubscriber($timestampableListener); |
|
214
|
|
|
|
|
215
|
|
|
$this->entityManager = EntityManager::create($connection, $config, $eventManager); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $this->entityManager; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get schema validator. |
|
223
|
|
|
* |
|
224
|
|
|
* @return SchemaValidator |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getSchemaValidator() |
|
227
|
|
|
{ |
|
228
|
|
|
if (null === $this->schemaValidator) { |
|
229
|
|
|
$entityManager = $this->getEntityManager(); |
|
230
|
|
|
|
|
231
|
|
|
$this->schemaValidator = new SchemaValidator($entityManager); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $this->schemaValidator; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get schema tool. |
|
239
|
|
|
* |
|
240
|
|
|
* @return SchemaTool |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getSchemaTool() |
|
243
|
|
|
{ |
|
244
|
|
|
if (null === $this->schemaTool) { |
|
245
|
|
|
$entityManager = $this->getEntityManager(); |
|
246
|
|
|
|
|
247
|
|
|
$this->schemaTool = new SchemaTool($entityManager); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return $this->schemaTool; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Get logger. |
|
255
|
|
|
* |
|
256
|
|
|
* @return LoggerInterface |
|
257
|
|
|
*/ |
|
258
|
|
|
public function getLogger() |
|
259
|
|
|
{ |
|
260
|
|
|
if (null === $this->logger) { |
|
261
|
|
|
$logger = new Logger('COMMANDER'); |
|
262
|
|
|
|
|
263
|
|
|
$path = $this->config->getLogFilePath(); |
|
264
|
|
|
if (null !== $path) { |
|
265
|
|
|
$logger->pushHandler(new StreamHandler($path)); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
$this->logger = $logger; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
return $this->logger; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Get task manager. |
|
276
|
|
|
* |
|
277
|
|
|
* @return TaskManager |
|
278
|
|
|
*/ |
|
279
|
|
|
public function getTaskManager() |
|
280
|
|
|
{ |
|
281
|
|
|
if (null === $this->taskManager) { |
|
282
|
|
|
$entityManager = $this->getEntityManager(); |
|
283
|
|
|
|
|
284
|
|
|
$this->taskManager = new TaskManager($entityManager); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return $this->taskManager; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: