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