|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Slim3 Doctrine integration (https://github.com/juliangut/slim-doctrine). |
|
5
|
|
|
* |
|
6
|
|
|
* @license BSD-3-Clause |
|
7
|
|
|
* @link https://github.com/juliangut/slim-doctrine |
|
8
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Jgut\Slim\Doctrine; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
14
|
|
|
use Jgut\Doctrine\ManagerBuilder\AbstractBuilderCollection; |
|
15
|
|
|
use Jgut\Doctrine\ManagerBuilder\CouchDBBuilder; |
|
16
|
|
|
use Jgut\Doctrine\ManagerBuilder\ManagerBuilder as Builder; |
|
17
|
|
|
use Jgut\Doctrine\ManagerBuilder\MongoDBBuilder; |
|
18
|
|
|
use Jgut\Doctrine\ManagerBuilder\RelationalBuilder; |
|
19
|
|
|
use Jgut\Doctrine\ManagerBuilder\Util\OptionsTrait; |
|
20
|
|
|
use Symfony\Component\Console\Application; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Slim-Doctrine managers integration. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ManagerBuilder extends AbstractBuilderCollection |
|
26
|
|
|
{ |
|
27
|
|
|
use OptionsTrait; |
|
28
|
|
|
|
|
29
|
|
|
const METADATA_MAPPING_ANNOTATION = Builder::METADATA_MAPPING_ANNOTATION; |
|
30
|
|
|
const METADATA_MAPPING_XML = Builder::METADATA_MAPPING_XML; |
|
31
|
|
|
const METADATA_MAPPING_YAML = Builder::METADATA_MAPPING_YAML; |
|
32
|
|
|
const METADATA_MAPPING_PHP = Builder::METADATA_MAPPING_PHP; |
|
33
|
|
|
|
|
34
|
|
|
const RELATIONAL_MANAGER_KEY = 'relational_manager_key'; |
|
35
|
|
|
const MONGODB_MANAGER_KEY = 'mongodb_manager_key'; |
|
36
|
|
|
const COUCHDB_MANAGER_KEY = 'couchdb_manager_key'; |
|
37
|
|
|
|
|
38
|
|
|
const RELATIONAL_MANAGER_NAME = 'relational_manager_name'; |
|
39
|
|
|
const MONGODB_MANAGER_NAME = 'mongodb_manager_name'; |
|
40
|
|
|
const COUCHDB_MANAGER_NAME = 'couchdb_manager_name'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Global annotation loader control. |
|
44
|
|
|
* |
|
45
|
|
|
* @var bool |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $globalLoaderRegister = true; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* ManagerBuilder constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $options |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(array $options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$options = array_merge( |
|
57
|
|
|
[ |
|
58
|
|
|
static::RELATIONAL_MANAGER_KEY => 'entity_manager', |
|
59
|
|
|
static::MONGODB_MANAGER_KEY => 'mongodb_document_manager', |
|
60
|
|
|
static::COUCHDB_MANAGER_KEY => 'couchdb_document_manager', |
|
61
|
|
|
static::RELATIONAL_MANAGER_NAME => 'entityManager', |
|
62
|
|
|
static::MONGODB_MANAGER_NAME => 'mongoDocumentManager', |
|
63
|
|
|
static::COUCHDB_MANAGER_NAME => 'couchDocumentManager', |
|
64
|
|
|
], |
|
65
|
|
|
$options |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->setOptions($options); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Load Doctrine managers from settings array. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $settings |
|
75
|
|
|
* |
|
76
|
|
|
* @throws \RuntimeException |
|
77
|
|
|
* |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function loadSettings(array $settings) |
|
81
|
|
|
{ |
|
82
|
|
|
$relationalManagerKey = $this->getOption(static::RELATIONAL_MANAGER_KEY); |
|
83
|
|
|
if (array_key_exists($relationalManagerKey, $settings)) { |
|
84
|
|
|
$this->registerEntityManagers((array) $settings[$relationalManagerKey]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$mongoDBManagerKey = $this->getOption(static::MONGODB_MANAGER_KEY); |
|
88
|
|
|
if (array_key_exists($mongoDBManagerKey, $settings)) { |
|
89
|
|
|
$this->registerMongoDBDocumentManagers((array) $settings[$mongoDBManagerKey]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$couchDBManagerKey = $this->getOption(static::COUCHDB_MANAGER_KEY); |
|
93
|
|
|
if (array_key_exists($couchDBManagerKey, $settings)) { |
|
94
|
|
|
$this->registerCouchDBDocumentManagers((array) $settings[$couchDBManagerKey]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Register ORM entity managers. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $settings |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \RuntimeException |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function registerEntityManagers(array $settings) |
|
108
|
|
|
{ |
|
109
|
|
|
if (array_key_exists('connection', $settings)) { |
|
110
|
|
|
$settings = [$settings]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
foreach ($settings as $name => $config) { |
|
114
|
|
|
if (!is_string($name)) { |
|
115
|
|
|
$name = $this->getOption(static::RELATIONAL_MANAGER_NAME); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->addBuilder(new RelationalBuilder($config, $name)); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Register MongoDB ODM document managers. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $settings |
|
126
|
|
|
* |
|
127
|
|
|
* @throws \RuntimeException |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function registerMongoDBDocumentManagers(array $settings) |
|
130
|
|
|
{ |
|
131
|
|
|
if (array_key_exists('connection', $settings)) { |
|
132
|
|
|
$settings = [$settings]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
foreach ($settings as $name => $config) { |
|
136
|
|
|
if (!is_string($name)) { |
|
137
|
|
|
$name = $this->getOption(static::MONGODB_MANAGER_NAME); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->addBuilder(new MongoDBBuilder($config, $name)); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Register CouchDB ODM document managers. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $settings |
|
148
|
|
|
* |
|
149
|
|
|
* @throws \RuntimeException |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function registerCouchDBDocumentManagers(array $settings) |
|
152
|
|
|
{ |
|
153
|
|
|
if (array_key_exists('connection', $settings)) { |
|
154
|
|
|
$settings = [$settings]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
foreach ($settings as $name => $config) { |
|
158
|
|
|
if (!is_string($name)) { |
|
159
|
|
|
$name = $this->getOption(static::COUCHDB_MANAGER_NAME); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->addBuilder(new CouchDBBuilder($config, $name)); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get registered builder's managers. |
|
168
|
|
|
* |
|
169
|
|
|
* @return \Doctrine\Common\Persistence\ObjectManager[] |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getManagers() |
|
172
|
|
|
{ |
|
173
|
|
|
$managers = array_map( |
|
174
|
|
|
function (Builder $builder) { |
|
175
|
|
|
return $builder->getManager(); |
|
176
|
|
|
}, |
|
177
|
|
|
$this->builders |
|
178
|
|
|
); |
|
179
|
|
|
|
|
180
|
|
|
$this->registerGlobalAnnotationLoader(); |
|
181
|
|
|
|
|
182
|
|
|
return $managers; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get registered builder's manager. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $name |
|
189
|
|
|
* |
|
190
|
|
|
* @throws \RuntimeException |
|
191
|
|
|
* |
|
192
|
|
|
* @return \Doctrine\Common\Persistence\ObjectManager |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getManager($name) |
|
195
|
|
|
{ |
|
196
|
|
|
$builder = $this->getBuilder($name); |
|
197
|
|
|
if (!$builder instanceof Builder) { |
|
198
|
|
|
throw new \RuntimeException(sprintf('"%s" is not a registered manager', $name)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$manager = $builder->getManager(); |
|
202
|
|
|
|
|
203
|
|
|
$this->registerGlobalAnnotationLoader(); |
|
204
|
|
|
|
|
205
|
|
|
return $manager; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get console application. |
|
210
|
|
|
* |
|
211
|
|
|
* @return Application |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getCLIApplication() |
|
214
|
|
|
{ |
|
215
|
|
|
$application = new Application('Doctrine Manager Builder Command Line Interface'); |
|
216
|
|
|
$application->setCatchExceptions(true); |
|
217
|
|
|
|
|
218
|
|
|
foreach ($this->builders as $builder) { |
|
219
|
|
|
$helperSet = $builder->getConsoleHelperSet(); |
|
220
|
|
|
|
|
221
|
|
|
foreach ($builder->getConsoleCommands() as $command) { |
|
222
|
|
|
$application->add($command)->setHelperSet($helperSet); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
$this->registerGlobalAnnotationLoader(); |
|
227
|
|
|
|
|
228
|
|
|
return $application; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Register global annotation loader. |
|
233
|
|
|
* class_exists function. |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function registerGlobalAnnotationLoader() |
|
236
|
|
|
{ |
|
237
|
|
|
if ($this->globalLoaderRegister) { |
|
238
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
239
|
|
|
|
|
240
|
|
|
$this->globalLoaderRegister = false; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|