Skip to content

Commit 18c7f85

Browse files
Nyholmxavismeh
authored andcommitted
Make graphaware/neo4j-php-ogm optional (#8)
* Make graphaware/neo4j-php-ogm optional * code style * Moved ogm to suggest * typo
1 parent 2f4ad18 commit 18c7f85

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

DependencyInjection/Neo4jExtension.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GraphAware\Bolt\Driver as BoltDriver;
88
use GraphAware\Neo4j\Client\Connection\Connection;
9+
use GraphAware\Neo4j\OGM\EntityManager;
910
use GraphAware\Neo4j\Client\HttpDriver\Driver as HttpDriver;
1011
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1112
use Symfony\Component\Config\FileLocator;
@@ -34,7 +35,10 @@ public function load(array $configs, ContainerBuilder $container)
3435

3536
$this->handleConnections($config, $container);
3637
$clientServiceIds = $this->handleClients($config, $container);
37-
$this->handleEntityMangers($config, $container, $clientServiceIds);
38+
if ($this->validateEntityManagers($config)) {
39+
$loader->load('entity_manager.xml');
40+
$this->handleEntityMangers($config, $container, $clientServiceIds);
41+
}
3842

3943
// add aliases for the default services
4044
$container->setAlias('neo4j.connection', 'neo4j.connection.default');
@@ -114,11 +118,6 @@ private function handleClients(array &$config, ContainerBuilder $container): arr
114118
*/
115119
private function handleEntityMangers(array &$config, ContainerBuilder $container, array $clientServiceIds): array
116120
{
117-
if (empty($config['entity_managers'])) {
118-
// Add default entity manager if none set.
119-
$config['entity_managers']['default'] = ['client' => 'default'];
120-
}
121-
122121
$serviceIds = [];
123122
foreach ($config['entity_managers'] as $name => $data) {
124123
$serviceIds[] = $serviceId = sprintf('neo4j.entity_manager.%s', $name);
@@ -204,4 +203,30 @@ private function getPort(array $config)
204203

205204
return 'http' == $config['schema'] ? HttpDriver::DEFAULT_HTTP_PORT : BoltDriver::DEFAULT_TCP_PORT;
206205
}
206+
207+
/**
208+
* Make sure the EntityManager is installed if we have configured it.
209+
*
210+
* @param array &$config
211+
*
212+
* @return bool true if "graphaware/neo4j-php-ogm" is installed
213+
*
214+
* @thorws \LogicException if EntityManagers os not installed but they are configured.
215+
*/
216+
private function validateEntityManagers(array &$config): bool
217+
{
218+
$dependenciesInstalled = class_exists(EntityManager::class);
219+
$entityManagersConfigured = !empty($config['entity_managers']);
220+
221+
if ($dependenciesInstalled && !$entityManagersConfigured) {
222+
// Add default entity manager if none set.
223+
$config['entity_managers']['default'] = ['client' => 'default'];
224+
} elseif (!$dependenciesInstalled && $entityManagersConfigured) {
225+
throw new \LogicException(
226+
'You need to install "graphaware/neo4j-php-ogm" to be able to use the EntityManager'
227+
);
228+
}
229+
230+
return $dependenciesInstalled;
231+
}
207232
}

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Via Composer
1515
$ composer require neo4j/neo4j-bundle
1616
```
1717

18-
At the moment we have a hard dependency on GraphAware OGM. You need to install that as well
18+
If you want to use the an `EntityManager` you need to install a [GraphAware OGM](https://github.com/graphaware/neo4j-php-ogm)
1919

2020
```bash
2121
$ composer require graphaware/neo4j-php-ogm:@beta
@@ -43,7 +43,7 @@ The bundle is a convenient way of registering services. We register `Connections
4343

4444
* neo4j.connection
4545
* neo4j.client
46-
* neo4j.entity_manager
46+
* neo4j.entity_manager*
4747

4848

4949
### Minimal configuration
@@ -57,7 +57,7 @@ neo4j:
5757
With the minimal configuration we have services named:
5858
* neo4j.connection.default
5959
* neo4j.client.default
60-
* neo4j.entity_manager.default
60+
* neo4j.entity_manager.default*
6161
6262
### Full configuration
6363
@@ -92,9 +92,11 @@ With the configuration above we would have services named:
9292
* neo4j.client.default
9393
* neo4j.client.other_client
9494
* neo4j.client.other_foobar
95-
* neo4j.entity_manager.default
95+
* neo4j.entity_manager.default*
9696
9797
98+
\* Note: EntityManagers will only be available if `graphaware/neo4j-php-ogm` is installed.
99+
98100
## Testing
99101

100102
``` bash

Resources/config/entity_manager.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="neo4j.entity_manager.abstract" class="GraphAware\Neo4j\OGM\EntityManager" abstract="true">
8+
</service>
9+
</services>
10+
</container>

Resources/config/services.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@
1111
<service id="neo4j.client.abstract" class="GraphAware\Neo4j\Client\Client" abstract="true">
1212
<factory service="neo4j.factory.client" method="create" />
1313
</service>
14-
<service id="neo4j.entity_manager.abstract" class="GraphAware\Neo4j\OGM\EntityManager" abstract="true">
15-
</service>
1614
</services>
1715
</container>

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
],
1717
"require": {
1818
"php": "^7.0",
19-
"graphaware/neo4j-php-ogm": "^1.0",
2019
"graphaware/neo4j-php-client": "^4.6.4",
2120
"symfony/framework-bundle": "^3.0",
2221
"graphaware/neo4j-bolt": "^1.6"
2322
},
2423
"require-dev": {
2524
"phpunit/phpunit": "^5.4",
2625
"symfony/symfony": "^3.0",
26+
"graphaware/neo4j-php-ogm": "^1.0",
2727
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
2828
},
29+
"suggest": {
30+
"graphaware/neo4j-php-ogm": "To have EntityManager support"
31+
},
2932
"autoload": {
3033
"psr-4": {
3134
"Neo4j\\Neo4jBundle\\": ""

0 commit comments

Comments
 (0)