Skip to content

Commit a4967ad

Browse files
committed
Merge remote-tracking branch 'origin/master' into Nyholm-datacollector
2 parents a4a7eb5 + 18c7f85 commit a4967ad

File tree

7 files changed

+101
-28
lines changed

7 files changed

+101
-28
lines changed

Changelog.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## UNRELEASED
6+
7+
## 0.1.0
8+
9+
First release

DependencyInjection/Configuration.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public function getConfigTreeBuilder()
8585
->prototype('array')
8686
->addDefaultsIfNotSet()
8787
->children()
88-
->enumNode('schema')->values(['http', 'bolt'])->defaultValue('http')->end()
88+
->enumNode('schema')->values(['http', 'bolt'])->defaultValue('bolt')->end()
8989
->scalarNode('host')->defaultValue('localhost')->end()
90-
->integerNode('port')->defaultValue(7474)->end()
90+
->integerNode('port')->end()
9191
->scalarNode('username')->defaultValue('neo4j')->end()
9292
->scalarNode('password')->defaultValue('neo4j')->end()
9393
->end()

DependencyInjection/Neo4jExtension.php

+58-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Neo4j\Neo4jBundle\DependencyInjection;
66

7+
use GraphAware\Bolt\Driver as BoltDriver;
78
use GraphAware\Neo4j\Client\Connection\Connection;
9+
use GraphAware\Neo4j\OGM\EntityManager;
10+
use GraphAware\Neo4j\Client\HttpDriver\Driver as HttpDriver;
811
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
912
use Symfony\Component\Config\FileLocator;
1013
use 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
}

README.md

+11-9
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
@@ -67,11 +67,11 @@ neo4j:
6767
enabled: true
6868
connections:
6969
default:
70-
schema: http #default
71-
host: localhost #default
72-
port: 7474 #default
73-
username: neo4j #default
74-
password: neo4j #default
70+
schema: bolt # default (must be either "http" or "bolt")
71+
host: localhost # default
72+
port: 7474 # optional, will be set to the proper driver's default port if not provided
73+
username: neo4j # default
74+
password: neo4j # default
7575
second_connection:
7676
username: foo
7777
password: bar
@@ -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

+10
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

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@
1515
<factory service="neo4j.factory.client" method="create" />
1616

1717
</service>
18-
<service id="neo4j.entity_manager.abstract" class="GraphAware\Neo4j\OGM\EntityManager" abstract="true">
19-
</service>
2018
</services>
2119
</container>

composer.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@
88
{
99
"name": "Tobias Nyholm",
1010
"email": "[email protected]"
11+
},
12+
{
13+
"name": "Xavier Coureau",
14+
"email": "[email protected]"
1115
}
1216
],
1317
"require": {
1418
"php": "^7.0",
15-
"graphaware/neo4j-php-ogm": "^1.0",
16-
"graphaware/neo4j-php-client": "^4.6",
17-
"symfony/framework-bundle": "^3.0"
19+
"graphaware/neo4j-php-client": "^4.6.4",
20+
"symfony/framework-bundle": "^3.0",
21+
"graphaware/neo4j-bolt": "^1.6"
1822
},
1923
"require-dev": {
2024
"phpunit/phpunit": "^5.4",
2125
"symfony/symfony": "^3.0",
26+
"graphaware/neo4j-php-ogm": "^1.0",
2227
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
2328
},
29+
"suggest": {
30+
"graphaware/neo4j-php-ogm": "To have EntityManager support"
31+
},
2432
"autoload": {
2533
"psr-4": {
2634
"Neo4j\\Neo4jBundle\\": ""

0 commit comments

Comments
 (0)