1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* doctrine-manager-builder (https://github.com/juliangut/doctrine-manager-builder). |
5
|
|
|
* Doctrine2 managers builder. |
6
|
|
|
* |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @link https://github.com/juliangut/doctrine-manager-builder |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jgut\Doctrine\ManagerBuilder; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
15
|
|
|
use Doctrine\CouchDB\Tools\Console\Helper\CouchDBHelper; |
16
|
|
|
use Doctrine\ODM\CouchDB\Configuration; |
17
|
|
|
use Doctrine\ODM\CouchDB\DocumentRepository; |
18
|
|
|
use Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver; |
19
|
|
|
use Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver; |
20
|
|
|
use Doctrine\ODM\CouchDB\Mapping\Driver\YamlDriver; |
21
|
|
|
use Jgut\Doctrine\ManagerBuilder\CouchDB\DocumentManager; |
22
|
|
|
use Jgut\Doctrine\ManagerBuilder\CouchDB\Repository\RepositoryFactory; |
23
|
|
|
use Symfony\Component\Console\Command\Command; |
24
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Doctrine CouchDB Document Manager builder. |
28
|
|
|
*/ |
29
|
|
|
class CouchDBBuilder extends AbstractManagerBuilder |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Document Manager. |
33
|
|
|
* |
34
|
|
|
* @var DocumentManager |
35
|
|
|
*/ |
36
|
|
|
protected $manager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function getDefaultOptions() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
'connection' => [], // Array or \Doctrine\CouchDB\CouchDBClient |
45
|
|
|
'proxies_namespace' => 'DoctrineCouchDBODMProxy', |
46
|
|
|
'metadata_cache_namespace' => 'DoctrineCouchDBODMMetadataCache', |
47
|
|
|
'default_repository_class' => DocumentRepository::class, |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
* @throws \RuntimeException |
56
|
|
|
* @throws \UnexpectedValueException |
57
|
|
|
* |
58
|
|
|
* @return DocumentManager |
59
|
|
|
*/ |
60
|
|
|
public function getManager($force = false) |
61
|
|
|
{ |
62
|
|
|
if ($force === true) { |
63
|
|
|
$this->wipe(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$this->manager instanceof DocumentManager) { |
67
|
|
|
$this->manager = $this->buildManager(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->manager; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
protected function wipe() |
77
|
|
|
{ |
78
|
|
|
parent::wipe(); |
79
|
|
|
|
80
|
|
|
$this->manager = null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Build Doctrine CouchDB Document Manager. |
85
|
|
|
* |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
* @throws \RuntimeException |
88
|
|
|
* @throws \UnexpectedValueException |
89
|
|
|
* |
90
|
|
|
* @return DocumentManager |
91
|
|
|
*/ |
92
|
|
|
protected function buildManager() |
93
|
|
|
{ |
94
|
|
|
$config = new Configuration; |
95
|
|
|
|
96
|
|
|
$this->setUpGeneralConfigurations($config); |
97
|
|
|
$this->setUpSpecificConfigurations($config); |
98
|
|
|
|
99
|
|
|
$documentManager = DocumentManager::create($this->getOption('connection'), $config, $this->getEventManager()); |
100
|
|
|
|
101
|
|
|
if ($this->getRepositoryFactory() !== null) { |
102
|
|
|
$documentManager->setRepositoryFactory($this->getRepositoryFactory()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($this->getDefaultRepositoryClass() !== null) { |
106
|
|
|
$documentManager->setDefaultRepositoryClassName($this->getDefaultRepositoryClass()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $documentManager; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set up general manager configurations. |
114
|
|
|
* |
115
|
|
|
* @param Configuration $config |
116
|
|
|
*/ |
117
|
|
|
protected function setUpGeneralConfigurations(Configuration $config) |
118
|
|
|
{ |
119
|
|
|
$this->setupAnnotationMetadata(); |
120
|
|
|
$config->setMetadataDriverImpl($this->getMetadataMappingDriver()); |
121
|
|
|
|
122
|
|
|
$config->setProxyDir($this->getProxiesPath()); |
123
|
|
|
$config->setProxyNamespace($this->getProxiesNamespace()); |
124
|
|
|
$config->setAutoGenerateProxyClasses($this->getProxiesAutoGeneration()); |
125
|
|
|
|
126
|
|
|
$config->setMetadataCacheImpl($this->getMetadataCacheDriver()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set up manager specific configurations. |
131
|
|
|
* |
132
|
|
|
* @param Configuration $config |
133
|
|
|
*/ |
134
|
|
|
protected function setUpSpecificConfigurations(Configuration $config) |
135
|
|
|
{ |
136
|
|
|
if ($this->getLuceneHandlerName() !== null) { |
137
|
|
|
$config->setLuceneHandlerName($this->getLuceneHandlerName()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
protected function getAnnotationMappingDriver(array $paths) |
145
|
|
|
{ |
146
|
|
|
return new AnnotationDriver(new AnnotationReader, $paths); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
protected function getXmlMappingDriver(array $paths, $extension = null) |
153
|
|
|
{ |
154
|
|
|
$extension = $extension ?: XmlDriver::DEFAULT_FILE_EXTENSION; |
155
|
|
|
|
156
|
|
|
return new XmlDriver($paths, $extension); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
protected function getYamlMappingDriver(array $paths, $extension = null) |
163
|
|
|
{ |
164
|
|
|
$extension = $extension ?: YamlDriver::DEFAULT_FILE_EXTENSION; |
165
|
|
|
|
166
|
|
|
return new YamlDriver($paths, $extension); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
* |
172
|
|
|
* @throws \InvalidArgumentException |
173
|
|
|
* |
174
|
|
|
* @return RepositoryFactory|null |
175
|
|
|
*/ |
176
|
|
|
protected function getRepositoryFactory() |
177
|
|
|
{ |
178
|
|
|
if (!array_key_exists('repository_factory', $this->options)) { |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$repositoryFactory = $this->options['repository_factory']; |
183
|
|
|
|
184
|
|
|
if (!$repositoryFactory instanceof RepositoryFactory) { |
185
|
|
|
throw new \InvalidArgumentException(sprintf( |
186
|
|
|
'Invalid factory class "%s". It must be a Jgut\Doctrine\ManagerBuilder\CouchDB\RepositoryFactory.', |
187
|
|
|
get_class($repositoryFactory) |
188
|
|
|
)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $repositoryFactory; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get Lucene handler name. |
196
|
|
|
* |
197
|
|
|
* @return string|null |
198
|
|
|
*/ |
199
|
|
|
protected function getLuceneHandlerName() |
200
|
|
|
{ |
201
|
|
|
return $this->hasOption('lucene_handler_name') ? (string) $this->getOption('lucene_handler_name') : null; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
* |
207
|
|
|
* @throws \InvalidArgumentException |
208
|
|
|
* @throws \RuntimeException |
209
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
210
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
211
|
|
|
* @throws \UnexpectedValueException |
212
|
|
|
* |
213
|
|
|
* @return Command[] |
214
|
|
|
*/ |
215
|
|
|
public function getConsoleCommands() |
216
|
|
|
{ |
217
|
|
|
$commands = [ |
218
|
|
|
// CouchDB |
219
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\ReplicationStartCommand, |
220
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\ReplicationCancelCommand, |
221
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\ViewCleanupCommand, |
222
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\CompactDatabaseCommand, |
223
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\CompactViewCommand, |
224
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\MigrationCommand, |
225
|
|
|
|
226
|
|
|
// ODM |
227
|
|
|
new \Doctrine\ODM\CouchDB\Tools\Console\Command\GenerateProxiesCommand, |
228
|
|
|
new \Doctrine\ODM\CouchDB\Tools\Console\Command\UpdateDesignDocCommand, |
229
|
|
|
]; |
230
|
|
|
$commandPrefix = (string) $this->getName(); |
231
|
|
|
|
232
|
|
|
if ($commandPrefix !== '') { |
233
|
|
|
$commands = array_map( |
234
|
|
|
function (Command $command) use ($commandPrefix) { |
235
|
|
|
$commandNames = array_map( |
236
|
|
|
function ($commandName) use ($commandPrefix) { |
237
|
|
|
return preg_replace('/^couchdb:/', $commandPrefix . ':', $commandName); |
238
|
|
|
}, |
239
|
|
|
array_merge([$command->getName()], $command->getAliases()) |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
$command->setName(array_shift($commandNames)); |
243
|
|
|
$command->setAliases($commandNames); |
244
|
|
|
|
245
|
|
|
return $command; |
246
|
|
|
}, |
247
|
|
|
$commands |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $commands; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* {@inheritdoc} |
256
|
|
|
* |
257
|
|
|
* @throws \InvalidArgumentException |
258
|
|
|
* @throws \RuntimeException |
259
|
|
|
* @throws \UnexpectedValueException |
260
|
|
|
*/ |
261
|
|
|
public function getConsoleHelperSet() |
262
|
|
|
{ |
263
|
|
|
$documentManager = $this->getManager(); |
264
|
|
|
|
265
|
|
|
return new HelperSet([ |
266
|
|
|
'dm' => new CouchDBHelper($documentManager->getCouchDBClient(), $documentManager), |
267
|
|
|
]); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|