[DB] Add table map which allows using table names rather than entities in Doctrine operations

This commit is contained in:
Hugo Sales 2021-04-15 17:01:52 +00:00
parent c44443b52c
commit 9659762726
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 16 additions and 12 deletions

View File

@ -48,6 +48,17 @@ abstract class DB
self::$em = $m;
}
/**
* Table name to class map, used to allow specifying table names instead of classes in doctrine calls
*/
private static array $table_map = [];
public static function initTableMap()
{
foreach (self::$em->getMetadataFactory()->getAllMetadata() as $meta) {
self::$table_map[$meta->getTableName()] = $meta->getMetadataValue('name');
}
}
/**
* Perform a Doctrine Query Language query
*/
@ -162,18 +173,9 @@ abstract class DB
*/
public static function __callStatic(string $name, array $args)
{
if (($args[0] ?? '') == 'favourite') {
$args[0] = 'Plugin\Favourite\Entity\Favourite';
} else {
// TODO Plugins
// If the method is one of the following and the first argument doesn't look like a FQCN, add the prefix
$pref = '\App\Entity\\';
if (in_array($name, ['find', 'getReference', 'getPartialReference', 'getRepository'])
&& preg_match('/\\\\/', $args[0]) === 0
&& Formatting::startsWith($args[0], $pref) === false) {
$args[0] = $pref . ucfirst(Formatting::snakeCaseToCamelCase($args[0]));
$args[0] = preg_replace('/Gsactor/', 'GSActor', $args[0]);
}
if (in_array($name, ['find', 'getReference', 'getPartialReference', 'getRepository'])
&& !Formatting::startsWith($args[0], '\\')) {
$args[0] = self::$table_map[$args[0]];
}
return self::$em->{$name}(...$args);

View File

@ -147,6 +147,8 @@ class GNUsocial implements EventSubscriberInterface
Formatting::setTwig($this->twig);
Cache::setupCache();
DB::initTableMap();
// Events are proloaded on compilation, but set at runtime
$this->module_manager->loadModules();