4
4
5
5
namespace Neo4j \Neo4jBundle \DependencyInjection ;
6
6
7
+ use GraphAware \Bolt \Driver as BoltDriver ;
7
8
use GraphAware \Neo4j \Client \Connection \Connection ;
9
+ use GraphAware \Neo4j \OGM \EntityManager ;
10
+ use GraphAware \Neo4j \Client \HttpDriver \Driver as HttpDriver ;
8
11
use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
9
12
use Symfony \Component \Config \FileLocator ;
10
13
use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -32,7 +35,10 @@ public function load(array $configs, ContainerBuilder $container)
32
35
33
36
$ this ->handleConnections ($ config , $ container );
34
37
$ clientServiceIds = $ this ->handleClients ($ config , $ container );
35
- $ this ->handleEntityMangers ($ config , $ container , $ clientServiceIds );
38
+ if ($ this ->validateEntityManagers ($ config )) {
39
+ $ loader ->load ('entity_manager.xml ' );
40
+ $ this ->handleEntityMangers ($ config , $ container , $ clientServiceIds );
41
+ }
36
42
37
43
// add aliases for the default services
38
44
$ container ->setAlias ('neo4j.connection ' , 'neo4j.connection.default ' );
@@ -45,6 +51,22 @@ public function load(array $configs, ContainerBuilder $container)
45
51
}
46
52
}
47
53
54
+ /**
55
+ * {@inheritdoc}
56
+ */
57
+ public function getConfiguration (array $ config , ContainerBuilder $ container ): Configuration
58
+ {
59
+ return new Configuration ($ container ->getParameter ('kernel.debug ' ));
60
+ }
61
+
62
+ /**
63
+ * {@inheritdoc}
64
+ */
65
+ public function getAlias (): string
66
+ {
67
+ return 'neo4j ' ;
68
+ }
69
+
48
70
/**
49
71
* @param array $config
50
72
* @param ContainerBuilder $container
@@ -92,11 +114,6 @@ private function handleClients(array &$config, ContainerBuilder $container): arr
92
114
*/
93
115
private function handleEntityMangers (array &$ config , ContainerBuilder $ container , array $ clientServiceIds ): array
94
116
{
95
- if (empty ($ config ['entity_managers ' ])) {
96
- // Add default entity manager if none set.
97
- $ config ['entity_managers ' ]['default ' ] = ['client ' => 'default ' ];
98
- }
99
-
100
117
$ serviceIds = [];
101
118
foreach ($ config ['entity_managers ' ] as $ name => $ data ) {
102
119
$ serviceIds [] = $ serviceId = sprintf ('neo4j.entity_manager.%s ' , $ name );
@@ -170,20 +187,49 @@ private function getUrl(array $config): string
170
187
$ config ['username ' ],
171
188
$ config ['password ' ],
172
189
$ config ['host ' ],
173
- $ config[ ' port ' ]
190
+ $ this -> getPort ( $ config)
174
191
);
175
192
}
176
193
177
194
/**
178
- * {@inheritdoc}
195
+ * Return the correct default port if not manually set.
196
+ *
197
+ * @param array $config
198
+ *
199
+ * @return int
179
200
*/
180
- public function getConfiguration (array $ config, ContainerBuilder $ container ): Configuration
201
+ private function getPort (array $ config)
181
202
{
182
- return new Configuration ($ container ->getParameter ('kernel.debug ' ));
203
+ if (isset ($ config ['port ' ])) {
204
+ return $ config ['port ' ];
205
+ }
206
+
207
+ return 'http ' == $ config ['schema ' ] ? HttpDriver::DEFAULT_HTTP_PORT : BoltDriver::DEFAULT_TCP_PORT ;
183
208
}
184
209
185
- public function getAlias (): string
210
+ /**
211
+ * Make sure the EntityManager is installed if we have configured it.
212
+ *
213
+ * @param array &$config
214
+ *
215
+ * @return bool true if "graphaware/neo4j-php-ogm" is installed
216
+ *
217
+ * @thorws \LogicException if EntityManagers os not installed but they are configured.
218
+ */
219
+ private function validateEntityManagers (array &$ config ): bool
186
220
{
187
- return 'neo4j ' ;
221
+ $ dependenciesInstalled = class_exists (EntityManager::class);
222
+ $ entityManagersConfigured = !empty ($ config ['entity_managers ' ]);
223
+
224
+ if ($ dependenciesInstalled && !$ entityManagersConfigured ) {
225
+ // Add default entity manager if none set.
226
+ $ config ['entity_managers ' ]['default ' ] = ['client ' => 'default ' ];
227
+ } elseif (!$ dependenciesInstalled && $ entityManagersConfigured ) {
228
+ throw new \LogicException (
229
+ 'You need to install "graphaware/neo4j-php-ogm" to be able to use the EntityManager '
230
+ );
231
+ }
232
+
233
+ return $ dependenciesInstalled ;
188
234
}
189
235
}
0 commit comments