44
55namespace Neo4j \Neo4jBundle \DependencyInjection ;
66
7+ use GraphAware \Bolt \Driver as BoltDriver ;
78use GraphAware \Neo4j \Client \Connection \Connection ;
9+ use GraphAware \Neo4j \OGM \EntityManager ;
10+ use GraphAware \Neo4j \Client \HttpDriver \Driver as HttpDriver ;
811use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
912use Symfony \Component \Config \FileLocator ;
1013use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -32,7 +35,10 @@ public function load(array $configs, ContainerBuilder $container)
3235
3336 $ this ->handleConnections ($ config , $ container );
3437 $ 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+ }
3642
3743 // add aliases for the default services
3844 $ container ->setAlias ('neo4j.connection ' , 'neo4j.connection.default ' );
@@ -45,6 +51,22 @@ public function load(array $configs, ContainerBuilder $container)
4551 }
4652 }
4753
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+
4870 /**
4971 * @param array $config
5072 * @param ContainerBuilder $container
@@ -92,11 +114,6 @@ private function handleClients(array &$config, ContainerBuilder $container): arr
92114 */
93115 private function handleEntityMangers (array &$ config , ContainerBuilder $ container , array $ clientServiceIds ): array
94116 {
95- if (empty ($ config ['entity_managers ' ])) {
96- // Add default entity manager if none set.
97- $ config ['entity_managers ' ]['default ' ] = ['client ' => 'default ' ];
98- }
99-
100117 $ serviceIds = [];
101118 foreach ($ config ['entity_managers ' ] as $ name => $ data ) {
102119 $ serviceIds [] = $ serviceId = sprintf ('neo4j.entity_manager.%s ' , $ name );
@@ -170,20 +187,49 @@ private function getUrl(array $config): string
170187 $ config ['username ' ],
171188 $ config ['password ' ],
172189 $ config ['host ' ],
173- $ config[ ' port ' ]
190+ $ this -> getPort ( $ config)
174191 );
175192 }
176193
177194 /**
178- * {@inheritdoc}
195+ * Return the correct default port if not manually set.
196+ *
197+ * @param array $config
198+ *
199+ * @return int
179200 */
180- public function getConfiguration (array $ config, ContainerBuilder $ container ): Configuration
201+ private function getPort (array $ config)
181202 {
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 ;
183208 }
184209
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
186220 {
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 ;
188234 }
189235}
0 commit comments