|
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\DBAL\Connection; |
|
16
|
|
|
use Doctrine\DBAL\Types\Type; |
|
17
|
|
|
use Doctrine\ORM\Configuration; |
|
18
|
|
|
use Doctrine\ORM\EntityManager; |
|
19
|
|
|
use Doctrine\ORM\Mapping\DefaultQuoteStrategy; |
|
20
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
|
21
|
|
|
use Doctrine\ORM\Mapping\NamingStrategy; |
|
22
|
|
|
use Doctrine\ORM\Mapping\QuoteStrategy; |
|
23
|
|
|
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; |
|
24
|
|
|
use Doctrine\ORM\Mapping\Driver\XmlDriver; |
|
25
|
|
|
use Doctrine\ORM\Mapping\Driver\YamlDriver; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Doctrine Entity Manager service builder |
|
29
|
|
|
*/ |
|
30
|
|
|
class EntityManagerBuilder |
|
31
|
|
|
{ |
|
32
|
|
|
use ObjectManagerTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Default configuration options. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $defaultOptions = [ |
|
40
|
|
|
'connection' => null, |
|
41
|
|
|
'cache_driver' => null, |
|
42
|
|
|
'cache_namespace' => null, |
|
43
|
|
|
'annotation_files' => [], |
|
44
|
|
|
'annotation_namespaces' => [], |
|
45
|
|
|
'annotation_autoloaders' => [], |
|
46
|
|
|
'annotation_paths' => null, |
|
47
|
|
|
'xml_paths' => null, |
|
48
|
|
|
'yaml_paths' => null, |
|
49
|
|
|
'php_paths' => null, |
|
50
|
|
|
'naming_strategy' => null, |
|
51
|
|
|
'quote_strategy' => null, |
|
52
|
|
|
'proxy_path' => null, |
|
53
|
|
|
'proxies_namespace' => 'DoctrineORMProxy', |
|
54
|
|
|
'auto_generate_proxies' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
|
55
|
|
|
'sql_logger' => null, |
|
56
|
|
|
'event_manager' => null, |
|
57
|
|
|
'custom_types' => [], |
|
58
|
|
|
'string_functions' => [], |
|
59
|
|
|
'numeric_functions' => [], |
|
60
|
|
|
'datetime_functions' => [], |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create a Doctrine entity manager. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $options |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
69
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
70
|
|
|
* @throws \InvalidArgumentException |
|
71
|
|
|
* @throws \RuntimeException |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Doctrine\ORM\EntityManager |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function build(array $options) |
|
76
|
|
|
{ |
|
77
|
|
|
$options = array_merge(static::$defaultOptions, $options); |
|
78
|
|
|
|
|
79
|
|
|
static::setupAnnotationMetadata($options); |
|
80
|
|
|
|
|
81
|
|
|
$config = static::getConfiguration($options); |
|
82
|
|
|
static::setupMetadataDriver($config, self::getMetadataDriver($options)); |
|
83
|
|
|
static::setupNamingStrategy($config, $options); |
|
84
|
|
|
static::setupQuoteStrategy($config, $options); |
|
85
|
|
|
static::setupProxy($config, $options); |
|
86
|
|
|
static::setupSQLLogger($config, $options); |
|
87
|
|
|
static::setupCustomDQLFunctions($config, $options); |
|
88
|
|
|
|
|
89
|
|
|
$entityManager = EntityManager::create( |
|
90
|
|
|
$options['connection'], |
|
91
|
|
|
$config, |
|
92
|
|
|
$options['event_manager'] |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$connection = $entityManager->getConnection(); |
|
96
|
|
|
static::setupCustomDBALTypes($connection, $options); |
|
97
|
|
|
|
|
98
|
|
|
return $entityManager; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Create Doctrine ORM bare configuration. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $options |
|
105
|
|
|
* |
|
106
|
|
|
* @throws \InvalidArgumentException |
|
107
|
|
|
* |
|
108
|
|
|
* @return \Doctrine\ORM\Configuration |
|
109
|
|
|
*/ |
|
110
|
|
|
protected static function getConfiguration(array $options) |
|
111
|
|
|
{ |
|
112
|
|
|
$cacheDriver = static::getCacheDriver( |
|
113
|
|
|
$options['cache_driver'], |
|
114
|
|
|
$options['cache_namespace'] ?: 'orm_dc2_' . sha1($options['proxy_path'] ?: sys_get_temp_dir()) . '_' |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$config = new Configuration(); |
|
118
|
|
|
$config->setMetadataCacheImpl($cacheDriver); |
|
119
|
|
|
$config->setQueryCacheImpl($cacheDriver); |
|
120
|
|
|
$config->setResultCacheImpl($cacheDriver); |
|
121
|
|
|
|
|
122
|
|
|
return $config; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Create metadata driver. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $options |
|
129
|
|
|
* |
|
130
|
|
|
* @throws \RuntimeException |
|
131
|
|
|
* |
|
132
|
|
|
* @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver |
|
|
|
|
|
|
133
|
|
|
*/ |
|
134
|
|
|
protected static function getMetadataDriver(array $options) |
|
135
|
|
|
{ |
|
136
|
|
|
if ($options['annotation_paths']) { |
|
137
|
|
|
return new AnnotationDriver(new AnnotationReader, (array) $options['annotation_paths']); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if ($options['xml_paths']) { |
|
141
|
|
|
return new XmlDriver((array) $options['xml_paths'], '.xml'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($options['yaml_paths']) { |
|
145
|
|
|
return new YamlDriver((array) $options['yaml_paths'], '.yml'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($options['php_paths']) { |
|
149
|
|
|
return new StaticPHPDriver((array) $options['php_paths']); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
throw new \RuntimeException('No Metadata paths defined'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Setup naming strategy. |
|
157
|
|
|
* |
|
158
|
|
|
* @param \Doctrine\ORM\Configuration $config |
|
159
|
|
|
* @param array $options |
|
160
|
|
|
* |
|
161
|
|
|
* @throws \InvalidArgumentException |
|
162
|
|
|
*/ |
|
163
|
|
|
protected static function setupNamingStrategy(Configuration $config, array $options) |
|
164
|
|
|
{ |
|
165
|
|
|
$namingStrategy = $options['naming_strategy'] ?: new UnderscoreNamingStrategy(CASE_LOWER); |
|
166
|
|
|
if (!$namingStrategy instanceof NamingStrategy) { |
|
167
|
|
|
throw new \InvalidArgumentException('Naming strategy provided is not valid'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$config->setNamingStrategy($namingStrategy); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Setup quote strategy. |
|
175
|
|
|
* |
|
176
|
|
|
* @param \Doctrine\ORM\Configuration $config |
|
177
|
|
|
* @param array $options |
|
178
|
|
|
* |
|
179
|
|
|
* @throws \InvalidArgumentException |
|
180
|
|
|
*/ |
|
181
|
|
|
protected static function setupQuoteStrategy(Configuration $config, array $options) |
|
182
|
|
|
{ |
|
183
|
|
|
$quoteStrategy = $options['quote_strategy'] ?: new DefaultQuoteStrategy(); |
|
184
|
|
|
if (!$quoteStrategy instanceof QuoteStrategy) { |
|
185
|
|
|
throw new \InvalidArgumentException('Quote strategy provided is not valid'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$config->setQuoteStrategy($quoteStrategy); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Setup SQL logger. |
|
193
|
|
|
* |
|
194
|
|
|
* @param \Doctrine\ORM\Configuration $config |
|
195
|
|
|
* @param array $options |
|
196
|
|
|
*/ |
|
197
|
|
|
protected static function setupSQLLogger(Configuration $config, array $options) |
|
198
|
|
|
{ |
|
199
|
|
|
if ($options['sql_logger']) { |
|
200
|
|
|
$config->setSQLLogger($options['sql_logger']); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Setup custom DQL functions. |
|
206
|
|
|
* |
|
207
|
|
|
* @param \Doctrine\ORM\Configuration $config |
|
208
|
|
|
* @param array $options |
|
209
|
|
|
*/ |
|
210
|
|
|
protected static function setupCustomDQLFunctions(Configuration $config, array $options) |
|
211
|
|
|
{ |
|
212
|
|
|
$config->setCustomStringFunctions($options['string_functions']); |
|
213
|
|
|
|
|
214
|
|
|
$config->setCustomNumericFunctions($options['numeric_functions']); |
|
215
|
|
|
|
|
216
|
|
|
$config->setCustomDatetimeFunctions($options['datetime_functions']); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Setup Custom DBAL types. |
|
221
|
|
|
* |
|
222
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
|
223
|
|
|
* @param array $options |
|
224
|
|
|
* |
|
225
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
226
|
|
|
* @throws \RuntimeException |
|
227
|
|
|
*/ |
|
228
|
|
|
protected static function setupCustomDBALTypes(Connection $connection, array $options) |
|
229
|
|
|
{ |
|
230
|
|
|
$platform = $connection->getDatabasePlatform(); |
|
231
|
|
|
|
|
232
|
|
|
foreach ($options['custom_types'] as $type => $class) { |
|
233
|
|
|
if (Type::hasType($type)) { |
|
234
|
|
|
throw new \RuntimeException(sprintf('Type "%s" is already registered', $type)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
Type::addType($type, $class); |
|
238
|
|
|
$platform->registerDoctrineTypeMapping($type, $type); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.