13
13
14
14
use ONGR \ElasticsearchBundle \Annotation \Index ;
15
15
use ONGR \ElasticsearchBundle \DependencyInjection \Configuration ;
16
+ use ONGR \ElasticsearchBundle \Exception \DocumentIndexParserException ;
16
17
use ONGR \ElasticsearchBundle \Mapping \Converter ;
17
18
use ONGR \ElasticsearchBundle \Mapping \DocumentParser ;
18
19
use ONGR \ElasticsearchBundle \Mapping \IndexSettings ;
19
20
use ONGR \ElasticsearchBundle \Service \IndexService ;
20
21
use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
22
+ use Symfony \Component \DependencyInjection \Container ;
21
23
use Symfony \Component \DependencyInjection \ContainerBuilder ;
22
24
use Symfony \Component \DependencyInjection \Definition ;
23
25
@@ -36,9 +38,65 @@ class MappingPass implements CompilerPassInterface
36
38
public function process (ContainerBuilder $ container )
37
39
{
38
40
$ kernelDir = $ container ->getParameter ('kernel.project_dir ' );
41
+ $ parser = $ container ->get (DocumentParser::class);
39
42
43
+ $ indexClasses = [];
44
+ $ indexSettingsArray = [];
40
45
foreach ($ container ->getParameter (Configuration::ONGR_SOURCE_DIR ) as $ dir ) {
41
- $ this ->handleDirectoryMapping ($ container , $ kernelDir . $ dir );
46
+ foreach ($ this ->getNamespaces ($ kernelDir . $ dir ) as $ namespace ) {
47
+ $ indexClasses [$ namespace ] = $ namespace ;
48
+ }
49
+ }
50
+
51
+ $ overwrittenClasses = [];
52
+ $ indexOverrides = $ container ->getParameter (Configuration::ONGR_INDEXES_OVERRIDE );
53
+
54
+ foreach ($ indexOverrides as $ name => $ indexOverride ) {
55
+ $ class = isset ($ indexOverride ['class ' ]) ? $ indexOverride ['class ' ] : $ name ;
56
+
57
+ if (!isset ($ indexClasses [$ class ])) {
58
+ throw new \RuntimeException (
59
+ sprintf (
60
+ 'Document `%s` defined in ongr_elasticsearch.indexes config could not been found ' ,
61
+ $ class ,
62
+ )
63
+ );
64
+ }
65
+
66
+ $ indexSettings = $ this ->parseIndexSettingsFromClass ($ parser , $ class );
67
+
68
+ if ($ class !== $ name ) {
69
+ $ indexSettings ->setIndexName ('ongr.es.index. ' .$ name );
70
+ }
71
+
72
+ if (isset ($ indexOverride ['alias ' ])) {
73
+ $ indexSettings ->setAlias ($ indexOverride ['alias ' ]);
74
+ }
75
+
76
+ if (isset ($ indexOverride ['settings ' ])) {
77
+ $ indexSettings ->setIndexMetadata ($ indexOverride ['settings ' ]);
78
+ }
79
+
80
+ if (isset ($ indexOverride ['hosts ' ])) {
81
+ $ indexSettings ->setHosts ($ indexOverride ['hosts ' ]);
82
+ }
83
+
84
+ if (isset ($ indexOverride ['default ' ])) {
85
+ $ indexSettings ->setDefaultIndex ($ indexOverride ['default ' ]);
86
+ }
87
+
88
+ $ indexSettingsArray [$ name ] = $ indexSettings ;
89
+ $ overwrittenClasses [$ class ] = $ class ;
90
+ }
91
+
92
+ foreach (array_diff ($ indexClasses , $ overwrittenClasses ) as $ indexClass ) {
93
+ try {
94
+ $ indexSettingsArray [$ indexClass ] = $ this ->parseIndexSettingsFromClass ($ parser , $ indexClass );
95
+ } catch (DocumentIndexParserException $ e ) {}
96
+ }
97
+
98
+ foreach ($ indexSettingsArray as $ indexSettings ) {
99
+ $ this ->createIndex ($ container , $ indexSettings );
42
100
}
43
101
44
102
$ container ->setParameter (Configuration::ONGR_INDEXES , $ this ->indexes );
@@ -48,88 +106,84 @@ public function process(ContainerBuilder $container)
48
106
);
49
107
}
50
108
51
- /**
52
- * @param ContainerBuilder $container
53
- * @param string $dir
54
- *
55
- * @throws \ReflectionException
56
- */
57
- private function handleDirectoryMapping (ContainerBuilder $ container , string $ dir ): void
109
+ private function parseIndexSettingsFromClass (DocumentParser $ parser , string $ className ) : IndexSettings
58
110
{
59
- /** @var DocumentParser $parser */
60
- $ parser = $ container ->get (DocumentParser::class);
61
- $ indexesOverride = $ container ->getParameter (Configuration::ONGR_INDEXES_OVERRIDE );
62
- $ converterDefinition = $ container ->getDefinition (Converter::class);
111
+ $ class = new \ReflectionClass ($ className );
63
112
64
- foreach ( $ this -> getNamespaces ( $ dir ) as $ namespace ) {
65
- $ class = new \ ReflectionClass ( $ namespace );
113
+ /** @var Index $document */
114
+ $ document = $ parser -> getIndexAnnotation ( $ class );
66
115
67
- if (isset ($ indexesOverride [$ namespace ]['alias ' ]) && $ indexesOverride [$ namespace ]['alias ' ]) {
68
- $ indexAlias = $ indexesOverride [$ namespace ]['alias ' ];
69
- } else {
70
- $ indexAlias = $ parser ->getIndexAliasName ($ class );
71
- }
116
+ if ($ document === null ) {
117
+ throw new DocumentIndexParserException ();
118
+ }
72
119
73
- /** @var Index $document */
74
- $ document = $ parser ->getIndexAnnotation ($ class );
75
- $ indexMetadata = $ parser ->getIndexMetadata ($ class );
76
-
77
- if (!empty ($ indexMetadata )) {
78
- $ indexMetadata ['settings ' ] = array_filter (array_merge_recursive (
79
- $ indexMetadata ['settings ' ] ?? [],
80
- [
81
- 'number_of_replicas ' => $ document ->numberOfReplicas ,
82
- 'number_of_shards ' => $ document ->numberOfShards ,
83
- ],
84
- $ indexesOverride [$ namespace ]['settings ' ] ?? []
85
- ));
86
-
87
- $ indexSettings = new Definition (
88
- IndexSettings::class,
89
- [
90
- $ namespace ,
91
- $ indexAlias ,
92
- $ indexAlias ,
93
- $ indexMetadata ,
94
- $ indexesOverride [$ namespace ]['hosts ' ] ?? $ document ->hosts ,
95
- $ indexesOverride [$ namespace ]['default ' ] ?? $ document ->default ,
96
- ]
97
- );
120
+ $ indexSettings = new IndexSettings (
121
+ $ className ,
122
+ $ className ,
123
+ $ parser ->getIndexAliasName ($ class ),
124
+ $ parser ->getIndexMetadata ($ class ),
125
+ $ parser ->getPropertyMetadata ($ class ),
126
+ $ document ->hosts ,
127
+ $ parser ->isDefaultIndex ($ class )
128
+ );
98
129
99
- $ indexServiceDefinition = new Definition (IndexService::class, [
100
- $ namespace ,
101
- $ converterDefinition ,
102
- $ container ->getDefinition ('event_dispatcher ' ),
103
- $ indexSettings ,
104
- $ container ->getParameter (Configuration::ONGR_PROFILER_CONFIG )
105
- ? $ container ->getDefinition ('ongr.esb.tracer ' ) : null
106
- ]);
107
- $ indexServiceDefinition ->setPublic (true );
108
- $ converterDefinition ->addMethodCall (
109
- 'addClassMetadata ' ,
110
- [
111
- $ namespace ,
112
- $ parser ->getPropertyMetadata ($ class )
113
- ]
114
- );
130
+ $ indexSettings ->setIndexMetadata (['settings ' => [
131
+ 'number_of_replicas ' => $ document ->numberOfReplicas ,
132
+ 'number_of_shards ' => $ document ->numberOfShards ,
133
+ ]]);
115
134
116
- $ container ->setDefinition ($ namespace , $ indexServiceDefinition );
117
- $ this ->indexes [$ indexAlias ] = $ namespace ;
118
- $ isCurrentIndexDefault = $ parser ->isDefaultIndex ($ class );
119
- if ($ this ->defaultIndex && $ isCurrentIndexDefault ) {
120
- throw new \RuntimeException (
121
- sprintf (
122
- 'Only one index can be set as default. We found 2 indexes as default ones `%s` and `%s` ' ,
123
- $ this ->defaultIndex ,
124
- $ indexAlias
125
- )
126
- );
127
- }
128
-
129
- if ($ isCurrentIndexDefault ) {
130
- $ this ->defaultIndex = $ indexAlias ;
131
- }
132
- }
135
+ return $ indexSettings ;
136
+ }
137
+
138
+ private function createIndex (Container $ container , IndexSettings $ indexSettings ) {
139
+ $ converterDefinition = $ container ->getDefinition (Converter::class);
140
+
141
+ $ indexSettingsDefinition = new Definition (
142
+ IndexSettings::class,
143
+ [
144
+ $ indexSettings ->getNamespace (),
145
+ $ indexSettings ->getAlias (),
146
+ $ indexSettings ->getAlias (),
147
+ $ indexSettings ->getIndexMetadata (),
148
+ $ indexSettings ->getPropertyMetadata (),
149
+ $ indexSettings ->getHosts (),
150
+ $ indexSettings ->isDefaultIndex (),
151
+ ]
152
+ );
153
+
154
+ $ indexServiceDefinition = new Definition (IndexService::class, [
155
+ $ indexSettings ->getNamespace (),
156
+ $ converterDefinition ,
157
+ $ container ->getDefinition ('event_dispatcher ' ),
158
+ $ indexSettingsDefinition ,
159
+ $ container ->getParameter (Configuration::ONGR_PROFILER_CONFIG )
160
+ ? $ container ->getDefinition ('ongr.esb.tracer ' ) : null
161
+ ]);
162
+
163
+ $ indexServiceDefinition ->setPublic (true );
164
+ $ converterDefinition ->addMethodCall (
165
+ 'addClassMetadata ' ,
166
+ [
167
+ $ indexSettings ->getNamespace (),
168
+ $ indexSettings ->getPropertyMetadata ()
169
+ ]
170
+ );
171
+
172
+ $ container ->setDefinition ($ indexSettings ->getIndexName (), $ indexServiceDefinition );
173
+ $ this ->indexes [$ indexSettings ->getAlias ()] = $ indexSettings ->getIndexName ();
174
+ $ isCurrentIndexDefault = $ indexSettings ->isDefaultIndex ();
175
+ if ($ this ->defaultIndex && $ isCurrentIndexDefault ) {
176
+ throw new \RuntimeException (
177
+ sprintf (
178
+ 'Only one index can be set as default. We found 2 indexes as default ones `%s` and `%s` ' ,
179
+ $ this ->defaultIndex ,
180
+ $ indexSettings ->getAlias ()
181
+ )
182
+ );
183
+ }
184
+
185
+ if ($ isCurrentIndexDefault ) {
186
+ $ this ->defaultIndex = $ indexSettings ->getAlias ();
133
187
}
134
188
}
135
189
0 commit comments