|
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 InvalidArgumentException; |
|
13
|
|
|
use Doctrine\Common\Cache\Cache; |
|
14
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
15
|
|
|
use Doctrine\ORM\Mapping\NamingStrategy; |
|
16
|
|
|
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; |
|
17
|
|
|
use Doctrine\ORM\EntityManager; |
|
18
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
19
|
|
|
use Doctrine\ORM\Configuration; |
|
20
|
|
|
use Doctrine\ORM\Mapping\Driver\XmlDriver; |
|
21
|
|
|
use Doctrine\ORM\Mapping\Driver\YamlDriver; |
|
22
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Doctrine Entity Manager service builder |
|
26
|
|
|
*/ |
|
27
|
|
|
class EntityManagerBuilder |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Default configuration options. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static $defaultOptions = [ |
|
35
|
|
|
'connection' => null, |
|
36
|
|
|
'proxy_path' => null, |
|
37
|
|
|
'cache_driver' => null, |
|
38
|
|
|
'annotation_files' => [], |
|
39
|
|
|
'annotation_namespaces' => [], |
|
40
|
|
|
'annotation_autoloaders' => [], |
|
41
|
|
|
'annotation_paths' => null, |
|
42
|
|
|
'xml_paths' => null, |
|
43
|
|
|
'yaml_paths' => null, |
|
44
|
|
|
'naming_strategy' => null, //\Doctrine\ORM\Mapping\UnderscoreNamingStrategy |
|
45
|
|
|
'proxies_namespace' => null, |
|
46
|
|
|
'auto_generate_proxies' => AbstractProxyFactory::AUTOGENERATE_NEVER, |
|
47
|
|
|
'sql_logger' => null, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create a Doctrine entity manager. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $options |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \InvalidArgumentException |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Doctrine\ORM\EntityManager |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function build(array $options) |
|
60
|
|
|
{ |
|
61
|
|
|
$options = array_merge(static::$defaultOptions, $options); |
|
62
|
|
|
|
|
63
|
|
|
if ($options['cache_driver'] !== null && !$options['cache_driver'] instanceof Cache) { |
|
64
|
|
|
throw new InvalidArgumentException('Cache Driver provided is not valid'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
static::setupAnnotationMetadata($options); |
|
68
|
|
|
|
|
69
|
|
|
$config = static::createConfiguration($options); |
|
70
|
|
|
if (!$config instanceof Configuration) { |
|
71
|
|
|
throw new InvalidArgumentException('No Metadata Driver defined'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
static::setupNamingStrategy($config, $options); |
|
75
|
|
|
|
|
76
|
|
|
static::setupProxy($config, $options); |
|
77
|
|
|
|
|
78
|
|
|
static::setupSQLLogger($config, $options); |
|
79
|
|
|
|
|
80
|
|
|
return EntityManager::create($options['connection'], $config); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set up annotation metadata. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $options |
|
87
|
|
|
*/ |
|
88
|
|
|
protected static function setupAnnotationMetadata(array $options) |
|
89
|
|
|
{ |
|
90
|
|
|
foreach ($options['annotation_files'] as $file) { |
|
91
|
|
|
AnnotationRegistry::registerFile($file); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
AnnotationRegistry::registerAutoloadNamespaces($options['annotation_namespaces']); |
|
95
|
|
|
|
|
96
|
|
|
foreach ($options['annotation_autoloaders'] as $autoloader) { |
|
97
|
|
|
AnnotationRegistry::registerLoader($autoloader); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Create Doctrine configuration. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $options |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Doctrine\ORM\Configuration|null |
|
107
|
|
|
*/ |
|
108
|
|
|
protected static function createConfiguration(array $options) |
|
109
|
|
|
{ |
|
110
|
|
|
if ($options['annotation_paths']) { |
|
111
|
|
|
return Setup::createAnnotationMetadataConfiguration( |
|
112
|
|
|
static::normalizePaths($options['annotation_paths']), |
|
113
|
|
|
false, |
|
114
|
|
|
$options['proxy_path'], |
|
115
|
|
|
$options['cache_driver'], |
|
116
|
|
|
false |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($options['xml_paths']) { |
|
121
|
|
|
return Setup::createXMLMetadataConfiguration( |
|
122
|
|
|
static::normalizePaths($options['xml_paths']), |
|
123
|
|
|
false, |
|
124
|
|
|
$options['proxy_path'], |
|
125
|
|
|
$options['cache_driver'] |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if ($options['yaml_paths']) { |
|
130
|
|
|
return Setup::createYAMLMetadataConfiguration( |
|
131
|
|
|
static::normalizePaths($options['yaml_paths']), |
|
132
|
|
|
false, |
|
133
|
|
|
$options['proxy_path'], |
|
134
|
|
|
$options['cache_driver'] |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Normalize paths to array. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array|string $paths |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
protected static function normalizePaths($paths) |
|
148
|
|
|
{ |
|
149
|
|
|
return is_array($paths) ? $paths : [$paths]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Setup naming strategy. |
|
154
|
|
|
* |
|
155
|
|
|
* @param \Doctrine\ORM\Configuration $config |
|
156
|
|
|
* @param array $options |
|
157
|
|
|
* |
|
158
|
|
|
* @throws \InvalidArgumentException |
|
159
|
|
|
*/ |
|
160
|
|
|
protected static function setupNamingStrategy(Configuration &$config, array $options) |
|
161
|
|
|
{ |
|
162
|
|
|
$namingStrategy = $options['naming_strategy'] ?: new UnderscoreNamingStrategy(CASE_LOWER); |
|
163
|
|
|
if (!$namingStrategy instanceof NamingStrategy) { |
|
164
|
|
|
throw new InvalidArgumentException('Naming strategy provided is not valid'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$config->setNamingStrategy($namingStrategy); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Setup proxies. |
|
172
|
|
|
* |
|
173
|
|
|
* @param \Doctrine\ORM\Configuration $config |
|
174
|
|
|
* @param array $options |
|
175
|
|
|
*/ |
|
176
|
|
|
protected static function setupProxy(Configuration &$config, array $options) |
|
177
|
|
|
{ |
|
178
|
|
|
if ($options['proxies_namespace']) { |
|
179
|
|
|
$config->setProxyNamespace($options['proxies_namespace']); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$config->setAutoGenerateProxyClasses(intval($options['auto_generate_proxies'])); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Setup SQL logger. |
|
187
|
|
|
* |
|
188
|
|
|
* @param \Doctrine\ORM\Configuration $config |
|
189
|
|
|
* @param array $options |
|
190
|
|
|
*/ |
|
191
|
|
|
protected static function setupSQLLogger(Configuration &$config, array $options) |
|
192
|
|
|
{ |
|
193
|
|
|
if ($options['sql_logger']) { |
|
194
|
|
|
$config->setSQLLogger($options['sql_logger']); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|