66
77use DH \Auditor \Provider \ConfigurationInterface ;
88use DH \Auditor \Provider \Doctrine \Persistence \Helper \DoctrineHelper ;
9+ use DH \Auditor \Provider \Doctrine \Persistence \Helper \SchemaHelper ;
910use DH \Auditor \Provider \Doctrine \Persistence \Reader \Reader ;
1011use DH \Auditor \Provider \Doctrine \Persistence \Schema \SchemaManager ;
1112use DH \Auditor \Provider \Doctrine \Service \AuditingService ;
@@ -30,6 +31,10 @@ final class Configuration implements ConfigurationInterface
3031
3132 private ?array $ entities = null ;
3233
34+ private array $ extraFields = [];
35+
36+ private array $ extraIndices = [];
37+
3338 private bool $ isViewerEnabled ;
3439
3540 private int $ viewerPageSize ;
@@ -60,6 +65,20 @@ public function __construct(array $options)
6065 }
6166 }
6267
68+ if (isset ($ config ['extra_fields ' ]) && !empty ($ config ['extra_fields ' ])) {
69+ // use field names as array keys for easier lookup
70+ foreach ($ config ['extra_fields ' ] as $ fieldName => $ fieldOptions ) {
71+ $ this ->extraFields [$ fieldName ] = $ fieldOptions ;
72+ }
73+ }
74+
75+ if (isset ($ config ['extra_indices ' ]) && !empty ($ config ['extra_indices ' ])) {
76+ // use index names as array keys for easier lookup
77+ foreach ($ config ['extra_indices ' ] as $ indexName => $ indexOptions ) {
78+ $ this ->extraIndices [$ indexName ] = $ indexOptions ;
79+ }
80+ }
81+
6382 $ this ->isViewerEnabled = self ::isViewerEnabledInConfig ($ config ['viewer ' ]);
6483 $ this ->viewerPageSize = self ::getViewerPageSizeFromConfig ($ config ['viewer ' ]);
6584 $ this ->storageMapper = $ config ['storage_mapper ' ];
@@ -229,6 +248,65 @@ public function getEntities(): array
229248 return $ this ->entities ?? [];
230249 }
231250
251+ public function getExtraFields (): array
252+ {
253+ return $ this ->extraFields ;
254+ }
255+
256+ public function getAllFields (): array
257+ {
258+ return array_merge (
259+ SchemaHelper::getAuditTableColumns (),
260+ $ this ->extraFields
261+ );
262+ }
263+
264+ /**
265+ * @param array<string, mixed> $extraFields
266+ */
267+ public function setExtraFields (array $ extraFields ): self
268+ {
269+ $ this ->extraFields = $ extraFields ;
270+
271+ return $ this ;
272+ }
273+
274+ public function getExtraIndices (): array
275+ {
276+ return $ this ->extraIndices ;
277+ }
278+
279+ public function prepareExtraIndices (string $ tableName ): array
280+ {
281+ $ indices = [];
282+ foreach ($ this ->extraIndices as $ extraIndexField => $ extraIndexOptions ) {
283+ $ indices [$ extraIndexField ] = [
284+ 'type ' => $ extraIndexOptions ['type ' ] ?? 'index ' ,
285+ 'name ' => sprintf ('%s_%s_idx ' , $ extraIndexOptions ['name_prefix ' ] ?? $ extraIndexField , md5 ($ tableName )),
286+ ];
287+ }
288+
289+ return $ indices ;
290+ }
291+
292+ public function getAllIndices (string $ tableName ): array
293+ {
294+ return array_merge (
295+ SchemaHelper::getAuditTableIndices ($ tableName ),
296+ $ this ->prepareExtraIndices ($ tableName )
297+ );
298+ }
299+
300+ /**
301+ * @param array<string, mixed> $extraIndices
302+ */
303+ public function setExtraIndices (array $ extraIndices ): self
304+ {
305+ $ this ->extraIndices = $ extraIndices ;
306+
307+ return $ this ;
308+ }
309+
232310 /**
233311 * Enables auditing for a specific entity.
234312 *
@@ -296,6 +374,8 @@ private function configureOptions(OptionsResolver $resolver): void
296374 'table_suffix ' => '_audit ' ,
297375 'ignored_columns ' => [],
298376 'entities ' => [],
377+ 'extra_fields ' => [],
378+ 'extra_indices ' => [],
299379 'storage_services ' => [],
300380 'auditing_services ' => [],
301381 'viewer ' => true ,
@@ -305,6 +385,8 @@ private function configureOptions(OptionsResolver $resolver): void
305385 ->setAllowedTypes ('table_suffix ' , 'string ' )
306386 ->setAllowedTypes ('ignored_columns ' , 'array ' )
307387 ->setAllowedTypes ('entities ' , 'array ' )
388+ ->setAllowedTypes ('extra_fields ' , 'array ' )
389+ ->setAllowedTypes ('extra_indices ' , 'array ' )
308390 ->setAllowedTypes ('storage_services ' , 'array ' )
309391 ->setAllowedTypes ('auditing_services ' , 'array ' )
310392 ->setAllowedTypes ('viewer ' , ['bool ' , 'array ' ])
0 commit comments