Storage configuration

A guide to configuring auditor-bundle.

Storage configuration is achieved using the YAML configuration file described in the General configuration section.

Audit tables naming format

doctrine-provider

Audit table names are composed of a prefix, a name and a suffix. By default, the prefix is empty and the suffix is _audit. Though, they can be customized.

dh_auditor:
    providers:
        doctrine:
            table_prefix: ~
            table_suffix: '_audit'

Storage services

doctrine-provider

By default, auditor-bundle stores audits using Doctrine's default entity manager doctrine.orm.default_entity_manager. However, auditor-bundle lets you store audits using several entity managers by adding them to the storage_services list.

dh_auditor:
    providers:
        doctrine:
            # storage entity managers (storage services)
            storage_services:
                - '@doctrine.orm.default_entity_manager'
                - '@doctrine.orm.second_entity_manager'
                - '@doctrine.orm.third_entity_manager'

Storage mapper

doctrine-provider

A storage mapper is a callable that routes audit events to storage services. It's up to the storage mapper to choose which storage service should be used to persist audits logs for a given entity.

A storage mapper expects 2 parameters as arguments and returns an object implementing StorageServiceInterface

  • an entity FQCN (string)
  • the list of registered storage services indexed by name (array)

Example

Below is an example of a storage mapper MyStorageMapper routing audit logs from MyEntity1 and MyEntity2 to a particular storage service, all other audit logs are persisted by another storage service.

<?php
namespace App;

use App\Entity\MyEntity1; 
use App\Entity\MyEntity2; 
use App\Entity\MyEntity3; 
use DH\Auditor\Provider\Service\StorageServiceInterface;

/**
 * MyStorageMapper is an invokable service
 */
class MyStorageMapper
{
    // the service expects 2 parameters and should return an object 
    // implementing StorageServiceInterface
    public function __invoke(string $entity, array $storageServices): StorageServiceInterface {
        return \in_array($entity, [MyEntity1::class, MyEntity2::class], true) ? $storageServices['db1'] : $storageServices['db2'];
    }
}
# config/packages/dh_auditor.yaml
dh_auditor:
    providers:
        doctrine:
            # Invokable service that maps audit events to storage services
            storage_mapper: my_storage_mapper.map_storage