1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slim3 Doctrine integration (https://github.com/juliangut/slim-doctrine) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/slim-doctrine for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://raw.githubusercontent.com/juliangut/slim-doctrine/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Slim\Doctrine; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver; |
13
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory; |
14
|
|
|
use Doctrine\MongoDB\Connection; |
15
|
|
|
use Doctrine\ODM\MongoDB\Configuration; |
16
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
17
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; |
18
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver; |
19
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Doctrine Document Manager service builder. |
23
|
|
|
*/ |
24
|
|
|
class DocumentManagerBuilder |
25
|
|
|
{ |
26
|
|
|
use ObjectManagerTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Default configuration options. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $defaultOptions = [ |
34
|
|
|
'connection' => null, |
35
|
|
|
'cache_driver' => null, |
36
|
|
|
'cache_namespace' => null, |
37
|
|
|
'annotation_files' => [], |
38
|
|
|
'annotation_namespaces' => [], |
39
|
|
|
'annotation_autoloaders' => [], |
40
|
|
|
'annotation_paths' => null, |
41
|
|
|
'xml_paths' => null, |
42
|
|
|
'yaml_paths' => null, |
43
|
|
|
'php_paths' => null, |
44
|
|
|
'default_database' => null, |
45
|
|
|
'proxy_path' => null, |
46
|
|
|
'proxies_namespace' => 'DoctrineODMProxy', |
47
|
|
|
'auto_generate_proxies' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
48
|
|
|
'hydrator_path' => null, |
49
|
|
|
'hydrators_namespace' => 'DoctrineODMHydrator', |
50
|
|
|
'auto_generate_hydrators' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
51
|
|
|
'logger_callable' => null, |
52
|
|
|
'event_manager' => null, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a Doctrine document manager. |
57
|
|
|
* |
58
|
|
|
* @param array $options |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
* @throws \RuntimeException |
62
|
|
|
* |
63
|
|
|
* @return \Doctrine\ODM\MongoDB\DocumentManager |
64
|
|
|
*/ |
65
|
|
|
public static function build(array $options) |
66
|
|
|
{ |
67
|
|
|
$options = array_merge(static::$defaultOptions, $options); |
68
|
|
|
|
69
|
|
|
AnnotationDriver::registerAnnotationClasses(); |
70
|
|
|
static::setupAnnotationMetadata($options); |
71
|
|
|
|
72
|
|
|
$config = static::getConfiguration($options); |
73
|
|
|
static::setupMetadataDriver($config, $options); |
74
|
|
|
static::setupDefaultDatabase($config, $options); |
75
|
|
|
static::setupProxy($config, $options); |
76
|
|
|
static::setupHydrator($config, $options); |
77
|
|
|
static::setupLogger($config, $options); |
78
|
|
|
|
79
|
|
|
return DocumentManager::create( |
80
|
|
|
self::getConnection($config, $options), |
81
|
|
|
$config, |
82
|
|
|
$options['event_manager'] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create Doctrine ODM bare configuration. |
88
|
|
|
* |
89
|
|
|
* @param array $options |
90
|
|
|
* |
91
|
|
|
* @throws \InvalidArgumentException |
92
|
|
|
* |
93
|
|
|
* @return \Doctrine\ODM\MongoDB\Configuration |
94
|
|
|
*/ |
95
|
|
|
protected static function getConfiguration(array $options) |
96
|
|
|
{ |
97
|
|
|
$cacheDriver = static::getCacheDriver( |
98
|
|
|
$options['cache_driver'], |
99
|
|
|
$options['cache_namespace'] ?: 'odm_dc2_' . sha1($options['proxy_path'] ?: sys_get_temp_dir()) . '_' |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$config = new Configuration(); |
103
|
|
|
$config->setMetadataCacheImpl($cacheDriver); |
104
|
|
|
|
105
|
|
|
return $config; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Create Doctrine ODM configuration. |
110
|
|
|
* |
111
|
|
|
* @param \Doctrine\ODM\MongoDB\Configuration $config |
112
|
|
|
* @param array $options |
113
|
|
|
* |
114
|
|
|
* @throws \RuntimeException |
115
|
|
|
*/ |
116
|
|
|
protected static function setupMetadataDriver(Configuration $config, array $options) |
117
|
|
|
{ |
118
|
|
|
if ($options['annotation_paths']) { |
119
|
|
|
$config->setMetadataDriverImpl( |
120
|
|
|
$config->newDefaultAnnotationDriver((array) $options['annotation_paths']) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($options['xml_paths']) { |
127
|
|
|
$config->setMetadataDriverImpl(new XmlDriver((array) $options['xml_paths'])); |
128
|
|
|
|
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($options['yaml_paths']) { |
133
|
|
|
$config->setMetadataDriverImpl(new YamlDriver((array) $options['yaml_paths'])); |
134
|
|
|
|
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($options['php_paths']) { |
139
|
|
|
$config->setMetadataDriverImpl(new StaticPHPDriver((array) $options['php_paths'])); |
140
|
|
|
|
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
throw new \RuntimeException('No Metadata Driver defined'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Setup default database. |
149
|
|
|
* |
150
|
|
|
* @param \Doctrine\ODM\MongoDB\Configuration $config |
151
|
|
|
* @param array $options |
152
|
|
|
*/ |
153
|
|
|
protected static function setupDefaultDatabase(Configuration $config, array $options) |
154
|
|
|
{ |
155
|
|
|
if ($options['default_database']) { |
156
|
|
|
$config->setDefaultDB($options['default_database']); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Setup hydrators. |
162
|
|
|
* |
163
|
|
|
* @param \Doctrine\ODM\MongoDB\Configuration $config |
164
|
|
|
* @param array $options |
165
|
|
|
*/ |
166
|
|
|
protected static function setupHydrator(Configuration $config, array $options) |
167
|
|
|
{ |
168
|
|
|
$config->setHydratorDir((string) ($options['hydrator_path'] ?: sys_get_temp_dir())); |
169
|
|
|
|
170
|
|
|
$config->setHydratorNamespace((string) $options['hydrators_namespace']); |
171
|
|
|
|
172
|
|
|
$config->setAutoGenerateHydratorClasses((bool) $options['auto_generate_hydrators']); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Setup logger. |
177
|
|
|
* |
178
|
|
|
* @param \Doctrine\ODM\MongoDB\Configuration $config |
179
|
|
|
* @param array $options |
180
|
|
|
*/ |
181
|
|
|
protected static function setupLogger(Configuration $config, array $options) |
182
|
|
|
{ |
183
|
|
|
if ($options['logger_callable']) { |
184
|
|
|
$config->setLoggerCallable($options['logger_callable']); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Create MongoDB Connection. |
190
|
|
|
* |
191
|
|
|
* @param \Doctrine\ODM\MongoDB\Configuration $config |
192
|
|
|
* @param array $options |
193
|
|
|
* |
194
|
|
|
* @throws \InvalidArgumentException |
195
|
|
|
* @throws \RuntimeException |
196
|
|
|
* |
197
|
|
|
* @return \Doctrine\MongoDB\Connection |
198
|
|
|
*/ |
199
|
|
|
protected static function getConnection(Configuration $config, array $options) |
200
|
|
|
{ |
201
|
|
|
$connection = $options['connection']; |
202
|
|
|
|
203
|
|
|
switch (true) { |
204
|
|
|
case (is_array($connection)): |
205
|
|
|
$connection = new Connection( |
206
|
|
|
isset($options['connection']['server']) ? $options['connection']['server'] : null, |
207
|
|
|
isset($options['connection']['options']) ? $options['connection']['options'] : [], |
208
|
|
|
$config, |
209
|
|
|
$options['event_manager'] |
210
|
|
|
); |
211
|
|
|
break; |
212
|
|
|
|
213
|
|
|
case ($connection instanceof Connection): |
214
|
|
|
if ($options['event_manager'] !== null |
215
|
|
|
&& $connection->getEventManager() !== $options['event_manager'] |
216
|
|
|
) { |
217
|
|
|
throw new \RuntimeException( |
218
|
|
|
'Cannot use different EventManager instances for DocumentManager and Connection.' |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
break; |
222
|
|
|
|
223
|
|
|
default: |
224
|
|
|
throw new \InvalidArgumentException('Invalid argument: ' . $connection); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $connection; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|