Storage mapper not seen as callable
Lehren opened this issue · comments
Q | A |
---|---|
auditor-bundle version |
5.0.0 |
PHP version | 7.4.28 |
Database | MySQL |
Summary
When using a storage mapper, it is not seen as callable. I think this is because the method is_callable
is called with just the class name. And this is not how you use it. This example code shows the problem.
<?php
class StorageMapper
{
public function __invoke(string $entity, array $storageServices): StorageServiceInterface
{
$globalEntities = [
'Customer::class',
'CustomerSetting::class',
'UserSetting::class',
];
return \in_array($entity, $globalEntities, true) ?
$storageServices['db1'] :
$storageServices['db2'];
}
public function __call($name, $args) {
// Hello
}
public static function __callStatic($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
echo(is_callable(StorageMapper::class) ? 'true' : 'false');
Current behavior
I cannot register a storage mapper, it will always crash
How to reproduce
Use a storage mapper, see sandbox link
Expected behavior
That I can use a storage mapper properly.
Hi @Lehren, you should be able to use closures or invokable classes as storage mapper out of the box.
(I just updated relevant test in damienharper/auditor
repository to reflect this)
I suspect you do not provide an instance of your mapper class to the DoctrineProvider::setStorageMapper()
method.
Usage example:
class FakeStorageMapper
{
public function __invoke(string $entity, array $storageServices): StorageServiceInterface
{
// Use your own logic here to return relevant storage service depending on $entity value
}
}
// configures `DoctrineProvider` to use the above invokable class as the storage mapper
$provider->setStorageMapper(new FakeStorageMapper());
@Lehren still struggling with storage mapper?
Your fix works. The documentation did not say I should call setStorageMapper
though, so might want to add that. And where the $provider
comes from