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\DefaultRepositoryFactory; |
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 \Doctrine\ODM\CouchDB\DocumentManager |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
protected function buildManager() |
93
|
|
|
{ |
94
|
|
|
$config = new Configuration; |
95
|
|
|
|
96
|
|
|
$this->setupAnnotationMetadata(); |
97
|
|
|
$config->setMetadataDriverImpl($this->getMetadataMappingDriver()); |
98
|
|
|
|
99
|
|
|
$config->setProxyDir($this->getProxiesPath()); |
100
|
|
|
$config->setProxyNamespace($this->getProxiesNamespace()); |
101
|
|
|
$config->setAutoGenerateProxyClasses($this->getProxiesAutoGeneration()); |
102
|
|
|
|
103
|
|
|
$config->setMetadataCacheImpl($this->getMetadataCacheDriver()); |
104
|
|
|
|
105
|
|
|
if ($this->getLuceneHandlerName() !== null) { |
106
|
|
|
$config->setLuceneHandlerName($this->getLuceneHandlerName()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$documentManager = DocumentManager::create($this->getOption('connection'), $config, $this->getEventManager()); |
110
|
|
|
|
111
|
|
|
if ($this->getRepositoryFactory() !== null) { |
112
|
|
|
$documentManager->setRepositoryFactory($this->getRepositoryFactory()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->getDefaultRepositoryClass() !== null) { |
116
|
|
|
$documentManager->setDefaultRepositoryClassName($this->getDefaultRepositoryClass()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $documentManager; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
protected function getAnnotationMetadataDriver(array $paths) |
126
|
|
|
{ |
127
|
|
|
return new AnnotationDriver(new AnnotationReader, $paths); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
protected function getXmlMetadataDriver(array $paths, $extension = null) |
134
|
|
|
{ |
135
|
|
|
return new XmlDriver($paths, $extension ?: XmlDriver::DEFAULT_FILE_EXTENSION); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
protected function getYamlMetadataDriver(array $paths, $extension = null) |
142
|
|
|
{ |
143
|
|
|
return new YamlDriver($paths, $extension ?: YamlDriver::DEFAULT_FILE_EXTENSION); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
* |
149
|
|
|
* @throws \InvalidArgumentException |
150
|
|
|
* |
151
|
|
|
* @return DefaultRepositoryFactory|null |
152
|
|
|
*/ |
153
|
|
|
protected function getRepositoryFactory() |
154
|
|
|
{ |
155
|
|
|
if (!array_key_exists('repository_factory', $this->options)) { |
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$repositoryFactory = $this->options['repository_factory']; |
160
|
|
|
|
161
|
|
|
if (!$repositoryFactory instanceof DefaultRepositoryFactory) { |
162
|
|
|
throw new \InvalidArgumentException(sprintf( |
163
|
|
|
'Invalid factory class "%s". It must be a Jgut\Doctrine\ManagerBuilder\CouchDB\RepositoryFactory.', |
164
|
|
|
get_class($repositoryFactory) |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $repositoryFactory; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get Lucene handler name. |
173
|
|
|
* |
174
|
|
|
* @return string|null |
175
|
|
|
*/ |
176
|
|
|
protected function getLuceneHandlerName() |
177
|
|
|
{ |
178
|
|
|
return $this->hasOption('lucene_handler_name') ? (string) $this->getOption('lucene_handler_name') : null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
* |
184
|
|
|
* @throws \InvalidArgumentException |
185
|
|
|
* @throws \RuntimeException |
186
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
187
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
188
|
|
|
* @throws \UnexpectedValueException |
189
|
|
|
* |
190
|
|
|
* @return Command[] |
191
|
|
|
*/ |
192
|
|
|
public function getConsoleCommands() |
193
|
|
|
{ |
194
|
|
|
$commands = [ |
195
|
|
|
// CouchDB |
196
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\ReplicationStartCommand, |
197
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\ReplicationCancelCommand, |
198
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\ViewCleanupCommand, |
199
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\CompactDatabaseCommand, |
200
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\CompactViewCommand, |
201
|
|
|
new \Doctrine\CouchDB\Tools\Console\Command\MigrationCommand, |
202
|
|
|
|
203
|
|
|
// ODM |
204
|
|
|
new \Doctrine\ODM\CouchDB\Tools\Console\Command\GenerateProxiesCommand, |
205
|
|
|
new \Doctrine\ODM\CouchDB\Tools\Console\Command\UpdateDesignDocCommand, |
206
|
|
|
]; |
207
|
|
|
$commandPrefix = (string) $this->getName(); |
208
|
|
|
|
209
|
|
|
if ($commandPrefix !== '') { |
210
|
|
|
$commands = array_map( |
211
|
|
|
function (Command $command) use ($commandPrefix) { |
212
|
|
|
$commandNames = array_map( |
213
|
|
|
function ($commandName) use ($commandPrefix) { |
214
|
|
|
return preg_replace('/^couchdb:/', $commandPrefix . ':', $commandName); |
215
|
|
|
}, |
216
|
|
|
array_merge([$command->getName()], $command->getAliases()) |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
$command->setName(array_shift($commandNames)); |
220
|
|
|
$command->setAliases($commandNames); |
221
|
|
|
|
222
|
|
|
return $command; |
223
|
|
|
}, |
224
|
|
|
$commands |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $commands; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritdoc} |
233
|
|
|
* |
234
|
|
|
* @throws \InvalidArgumentException |
235
|
|
|
* @throws \RuntimeException |
236
|
|
|
* @throws \UnexpectedValueException |
237
|
|
|
*/ |
238
|
|
|
public function getConsoleHelperSet() |
239
|
|
|
{ |
240
|
|
|
$documentManager = $this->getManager(); |
241
|
|
|
|
242
|
|
|
return new HelperSet([ |
243
|
|
|
'dm' => new CouchDBHelper($documentManager->getCouchDBClient(), $documentManager), |
244
|
|
|
]); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.