From 9659762726a1e084f9974342765d97859f7c2cde Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 15 Apr 2021 17:01:52 +0000 Subject: [PATCH] [DB] Add table map which allows using table names rather than entities in Doctrine operations --- src/Core/DB/DB.php | 26 ++++++++++++++------------ src/Core/GNUsocial.php | 2 ++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Core/DB/DB.php b/src/Core/DB/DB.php index 95a7d5b6c1..9934c7abde 100644 --- a/src/Core/DB/DB.php +++ b/src/Core/DB/DB.php @@ -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); diff --git a/src/Core/GNUsocial.php b/src/Core/GNUsocial.php index a6879acdbf..5135adc150 100644 --- a/src/Core/GNUsocial.php +++ b/src/Core/GNUsocial.php @@ -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();