1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Neo4jBundle\DependencyInjection; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\{ |
7
|
|
|
DependencyInjection\ContainerBuilder, |
8
|
|
|
Config\FileLocator, |
9
|
|
|
HttpKernel\DependencyInjection\Extension, |
10
|
|
|
DependencyInjection\Loader, |
11
|
|
|
DependencyInjection\Reference |
12
|
|
|
}; |
13
|
|
|
|
14
|
|
|
class InnmindNeo4jExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
44 |
|
public function load(array $configs, ContainerBuilder $container) |
20
|
|
|
{ |
21
|
44 |
|
$configuration = new Configuration(); |
22
|
44 |
|
$config = $this->processConfiguration($configuration, $configs); |
23
|
44 |
|
$loader = new Loader\YamlFileLoader( |
24
|
|
|
$container, |
25
|
44 |
|
new FileLocator(__DIR__ . '/../Resources/config') |
26
|
|
|
); |
27
|
44 |
|
$loader->load('services.yml'); |
28
|
|
|
|
29
|
|
|
$this |
30
|
44 |
|
->injectPersister( |
31
|
|
|
$container, |
32
|
44 |
|
$config['persister'] |
33
|
|
|
) |
34
|
44 |
|
->configureConnection( |
35
|
|
|
$container, |
36
|
44 |
|
$config['connection'] |
37
|
|
|
) |
38
|
44 |
|
->registerTypes( |
39
|
|
|
$container, |
40
|
44 |
|
$config['types'] |
41
|
|
|
) |
42
|
44 |
|
->injectMetadataConfiguration( |
43
|
|
|
$container, |
44
|
44 |
|
$config['metadata_configuration'] |
45
|
|
|
) |
46
|
44 |
|
->configureGenerators( |
47
|
|
|
$container, |
48
|
44 |
|
$config['identity_generators'] |
49
|
|
|
); |
50
|
44 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Inject the defined service in the unit of work |
54
|
|
|
* |
55
|
|
|
* @param ContainerBuilder $container |
56
|
|
|
* @param string $service |
57
|
|
|
* |
58
|
|
|
* @return self |
59
|
|
|
*/ |
60
|
44 |
|
private function injectPersister( |
61
|
|
|
ContainerBuilder $container, |
62
|
|
|
string $service |
63
|
|
|
): self { |
64
|
|
|
$container |
65
|
44 |
|
->getDefinition('innmind_neo4j.unit_of_work') |
66
|
44 |
|
->replaceArgument(5, new Reference($service)); |
67
|
|
|
|
68
|
44 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Inject value objects to make the connection work |
73
|
|
|
* |
74
|
|
|
* @param ContainerBuilder $container |
75
|
|
|
* @param array $config |
76
|
|
|
* |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
44 |
|
private function configureConnection( |
80
|
|
|
ContainerBuilder $container, |
81
|
|
|
array $config |
82
|
|
|
): self { |
83
|
44 |
|
$transactions = $container->getDefinition('innmind_neo4j.connection.transactions'); |
84
|
44 |
|
$transport = $container->getDefinition('innmind_neo4j.connection.transport'); |
85
|
44 |
|
$server = $container->getDefinition('innmind_neo4j.connection.server'); |
86
|
44 |
|
$authentication = $container->getDefinition('innmind_neo4j.connection.authentication'); |
87
|
|
|
|
88
|
|
|
$server |
89
|
44 |
|
->replaceArgument(0, $config['scheme']) |
90
|
44 |
|
->replaceArgument(1, $config['host']) |
91
|
44 |
|
->replaceArgument(2, $config['port']); |
92
|
|
|
$authentication |
93
|
44 |
|
->replaceArgument(0, $config['user']) |
94
|
44 |
|
->replaceArgument(1, $config['password']); |
95
|
|
|
$transactions |
96
|
44 |
|
->replaceArgument(2, $config['timeout']); |
97
|
|
|
$transport |
98
|
44 |
|
->replaceArgument(4, $config['timeout']); |
99
|
|
|
|
100
|
44 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Register the classes as added property types |
105
|
|
|
* |
106
|
|
|
* @param ContainerBuilder $container |
107
|
|
|
* @param array $types |
108
|
|
|
* |
109
|
|
|
* @return self |
110
|
|
|
*/ |
111
|
44 |
|
private function registerTypes( |
112
|
|
|
ContainerBuilder $container, |
113
|
|
|
array $types |
114
|
|
|
): self { |
115
|
44 |
|
$definition = $container->getDefinition('innmind_neo4j.types'); |
116
|
|
|
|
117
|
44 |
|
foreach ($types as $class) { |
118
|
20 |
|
$definition->addMethodCall('register', [$class]); |
119
|
|
|
} |
120
|
|
|
|
121
|
44 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Inject the configuration object into the metadata builder |
126
|
|
|
* |
127
|
|
|
* @param ContainerBuilder $container |
128
|
|
|
* @param string $config |
129
|
|
|
* |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
44 |
|
private function injectMetadataConfiguration( |
133
|
|
|
ContainerBuilder $container, |
134
|
|
|
string $config |
135
|
|
|
): self { |
136
|
|
|
$container |
137
|
44 |
|
->getDefinition('innmind_neo4j.metadata_builder') |
138
|
44 |
|
->replaceArgument(2, new Reference($config)); |
139
|
|
|
|
140
|
44 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
44 |
View Code Duplication |
private function configureGenerators( |
|
|
|
|
144
|
|
|
ContainerBuilder $container, |
145
|
|
|
array $generators |
146
|
|
|
): self { |
147
|
44 |
|
$definition = $container->getDefinition('innmind_neo4j.generators'); |
148
|
|
|
|
149
|
44 |
|
foreach ($generators as $class => $generator) { |
150
|
20 |
|
$definition->addMethodCall( |
151
|
20 |
|
'register', |
152
|
20 |
|
[$class, new Reference($generator)] |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
44 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.