|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Neo4j\Neo4jBundle\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use GraphAware\Bolt\Driver as BoltDriver; |
|
8
|
|
|
use GraphAware\Neo4j\Client\Connection\Connection; |
|
9
|
|
|
use GraphAware\Neo4j\OGM\EntityManager; |
|
10
|
|
|
use GraphAware\Neo4j\Client\HttpDriver\Driver as HttpDriver; |
|
11
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
18
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Neo4jExtension extends Extension |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function load(array $configs, ContainerBuilder $container) |
|
29
|
|
|
{ |
|
30
|
4 |
|
$configuration = $this->getConfiguration($configs, $container); |
|
31
|
4 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
4 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
34
|
4 |
|
$loader->load('services.xml'); |
|
35
|
|
|
|
|
36
|
4 |
|
$this->handleConnections($config, $container); |
|
37
|
4 |
|
$clientServiceIds = $this->handleClients($config, $container); |
|
38
|
|
|
|
|
39
|
4 |
|
if ($this->validateEntityManagers($config)) { |
|
40
|
|
|
$loader->load('entity_manager.xml'); |
|
41
|
|
|
$this->handleEntityMangers($config, $container, $clientServiceIds); |
|
42
|
|
|
$container->setAlias('neo4j.entity_manager', 'neo4j.entity_manager.default'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// add aliases for the default services |
|
46
|
4 |
|
$container->setAlias('neo4j.connection', 'neo4j.connection.default'); |
|
47
|
4 |
|
$container->setAlias('neo4j.client', 'neo4j.client.default'); |
|
48
|
|
|
|
|
49
|
|
|
// Configure toolbar |
|
50
|
4 |
|
if ($this->isConfigEnabled($container, $config['profiling'])) { |
|
51
|
2 |
|
$loader->load('data-collector.xml'); |
|
52
|
|
|
} |
|
53
|
4 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function getConfiguration(array $config, ContainerBuilder $container): Configuration |
|
59
|
|
|
{ |
|
60
|
4 |
|
return new Configuration($container->getParameter('kernel.debug')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public function getAlias(): string |
|
67
|
|
|
{ |
|
68
|
4 |
|
return 'neo4j'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $config |
|
73
|
|
|
* @param ContainerBuilder $container |
|
74
|
|
|
* |
|
75
|
|
|
* @return array with service ids |
|
76
|
|
|
*/ |
|
77
|
4 |
|
private function handleClients(array &$config, ContainerBuilder $container): array |
|
78
|
|
|
{ |
|
79
|
4 |
|
if (empty($config['clients'])) { |
|
80
|
|
|
// Add default entity manager if none set. |
|
81
|
4 |
|
$config['clients']['default'] = ['connections' => ['default']]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
$serviceIds = []; |
|
85
|
4 |
|
foreach ($config['clients'] as $name => $data) { |
|
86
|
4 |
|
$serviceIds[$name] = $serviceId = sprintf('neo4j.client.%s', $name); |
|
87
|
4 |
|
foreach ($data['connections'] as $connectionName) { |
|
88
|
4 |
|
if (empty($config['connections'][$connectionName])) { |
|
89
|
|
|
throw new InvalidConfigurationException(sprintf( |
|
90
|
|
|
'Client "%s" is configured to use connection named "%s" but there is no such connection', |
|
91
|
|
|
$name, |
|
92
|
|
|
$connectionName |
|
93
|
|
|
)); |
|
94
|
|
|
} |
|
95
|
4 |
|
$connections[] = $connectionName; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
4 |
|
if (empty($connections)) { |
|
98
|
|
|
$connections[] = 'default'; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$container |
|
102
|
4 |
|
->setDefinition($serviceId, new DefinitionDecorator('neo4j.client.abstract')) |
|
103
|
4 |
|
->setArguments([$connections]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
return $serviceIds; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $config |
|
111
|
|
|
* @param ContainerBuilder $container |
|
112
|
|
|
* @param array $clientServiceIds |
|
113
|
|
|
* |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
private function handleEntityMangers(array &$config, ContainerBuilder $container, array $clientServiceIds): array |
|
117
|
|
|
{ |
|
118
|
|
|
$serviceIds = []; |
|
119
|
|
|
foreach ($config['entity_managers'] as $name => $data) { |
|
120
|
|
|
$serviceIds[] = $serviceId = sprintf('neo4j.entity_manager.%s', $name); |
|
121
|
|
|
$clientName = $data['client']; |
|
122
|
|
|
if (empty($clientServiceIds[$clientName])) { |
|
123
|
|
|
throw new InvalidConfigurationException(sprintf( |
|
124
|
|
|
'EntityManager "%s" is configured to use client named "%s" but there is no such client', |
|
125
|
|
|
$name, |
|
126
|
|
|
$clientName |
|
127
|
|
|
)); |
|
128
|
|
|
} |
|
129
|
|
|
$container |
|
130
|
|
|
->setDefinition($serviceId, new DefinitionDecorator('neo4j.entity_manager.abstract')) |
|
131
|
|
|
->setArguments([ |
|
132
|
|
|
$container->getDefinition($clientServiceIds[$clientName]), |
|
133
|
|
|
empty($data['cache_dir']) ? $container->getParameter('kernel.cache_dir').'/neo4j' : $data['cache_dir'], |
|
134
|
|
|
]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $serviceIds; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param array $config |
|
142
|
|
|
* @param ContainerBuilder $container |
|
143
|
|
|
* |
|
144
|
|
|
* @return array with service ids |
|
145
|
|
|
*/ |
|
146
|
4 |
|
private function handleConnections(array &$config, ContainerBuilder $container): array |
|
147
|
|
|
{ |
|
148
|
4 |
|
$serviceIds = []; |
|
149
|
4 |
|
$firstName = null; |
|
150
|
4 |
|
foreach ($config['connections'] as $name => $data) { |
|
151
|
4 |
|
if ($firstName === null || $name === 'default') { |
|
152
|
4 |
|
$firstName = $name; |
|
153
|
|
|
} |
|
154
|
4 |
|
$def = new Definition(Connection::class); |
|
155
|
4 |
|
$def->addArgument($name); |
|
156
|
4 |
|
$def->addArgument($this->getUrl($data)); |
|
157
|
4 |
|
$serviceIds[$name] = $serviceId = 'neo4j.connection.'.$name; |
|
158
|
4 |
|
$container->setDefinition($serviceId, $def); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// Make sure we got a 'default' |
|
162
|
4 |
|
if ($firstName !== 'default') { |
|
163
|
|
|
$config['connections']['default'] = $config['connections'][$firstName]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Add connections to connection manager |
|
167
|
4 |
|
$connectionManager = $container->getDefinition('neo4j.connection_manager'); |
|
168
|
4 |
|
foreach ($serviceIds as $name => $serviceId) { |
|
169
|
4 |
|
$connectionManager->addMethodCall('registerExistingConnection', [$name, new Reference($serviceId)]); |
|
170
|
|
|
} |
|
171
|
4 |
|
$connectionManager->addMethodCall('setMaster', [$firstName]); |
|
172
|
|
|
|
|
173
|
4 |
|
return $serviceIds; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get URL form config. |
|
178
|
|
|
* |
|
179
|
|
|
* @param array $config |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
4 |
|
private function getUrl(array $config): string |
|
184
|
|
|
{ |
|
185
|
4 |
|
return sprintf( |
|
186
|
4 |
|
'%s://%s:%s@%s:%d', |
|
187
|
4 |
|
$config['schema'], |
|
188
|
4 |
|
$config['username'], |
|
189
|
4 |
|
$config['password'], |
|
190
|
4 |
|
$config['host'], |
|
191
|
4 |
|
$this->getPort($config) |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Return the correct default port if not manually set. |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $config |
|
199
|
|
|
* |
|
200
|
|
|
* @return int |
|
201
|
|
|
*/ |
|
202
|
4 |
|
private function getPort(array $config) |
|
203
|
|
|
{ |
|
204
|
4 |
|
if (isset($config['port'])) { |
|
205
|
3 |
|
return $config['port']; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
1 |
|
return 'http' == $config['schema'] ? HttpDriver::DEFAULT_HTTP_PORT : BoltDriver::DEFAULT_TCP_PORT; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Make sure the EntityManager is installed if we have configured it. |
|
213
|
|
|
* |
|
214
|
|
|
* @param array &$config |
|
215
|
|
|
* |
|
216
|
|
|
* @return bool true if "graphaware/neo4j-php-ogm" is installed |
|
217
|
|
|
* |
|
218
|
|
|
* @thorws \LogicException if EntityManagers os not installed but they are configured. |
|
219
|
|
|
*/ |
|
220
|
4 |
|
private function validateEntityManagers(array &$config): bool |
|
221
|
|
|
{ |
|
222
|
4 |
|
$dependenciesInstalled = class_exists(EntityManager::class); |
|
223
|
4 |
|
$entityManagersConfigured = !empty($config['entity_managers']); |
|
224
|
|
|
|
|
225
|
4 |
|
if ($dependenciesInstalled && !$entityManagersConfigured) { |
|
226
|
|
|
// Add default entity manager if none set. |
|
227
|
|
|
$config['entity_managers']['default'] = ['client' => 'default']; |
|
228
|
4 |
|
} elseif (!$dependenciesInstalled && $entityManagersConfigured) { |
|
229
|
|
|
throw new \LogicException( |
|
230
|
|
|
'You need to install "graphaware/neo4j-php-ogm" to be able to use the EntityManager' |
|
231
|
|
|
); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
4 |
|
return $dependenciesInstalled; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: