From 5fc7647c404086dfe1e108e12812f364489b49c0 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Mon, 7 Sep 2020 23:53:44 +0000 Subject: [PATCH] [MODULE][DB] Added support for loading entity definitions from modules --- src/Core/ModuleManager.php | 20 ++++++++++++++++--- .../Compiler/ModuleManagerPass.php | 8 +------- src/Kernel.php | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Core/ModuleManager.php b/src/Core/ModuleManager.php index 9ddf9030e2..239c1e087b 100644 --- a/src/Core/ModuleManager.php +++ b/src/Core/ModuleManager.php @@ -34,12 +34,13 @@ namespace App\Core; use App\Util\Formatting; - use AppendIterator; use FilesystemIterator; use Functional as F; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; class ModuleManager { @@ -82,18 +83,31 @@ class ModuleManager } } - public static function process() + public static function process(ContainerBuilder $container) { $module_paths = array_merge(glob(INSTALLDIR . '/components/*/*.php'), glob(INSTALLDIR . '/plugins/*/*.php')); $module_manager = new self(); + $entity_paths = []; + $default_driver = $container->findDefinition('doctrine.orm.default_metadata_driver'); foreach ($module_paths as $path) { // 'modules' and 'plugins' have the same length $type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path)); - $module = basename(dirname($path)); + $dir = dirname($path); + $module = basename($dir); $fqcn = "\\{$type}\\{$module}\\{$module}"; $module_manager->add($fqcn, $path); + if (file_exists($dir = $dir . '/Entity') && is_dir($dir)) { + $entity_paths[] = $dir; + $default_driver->addMethodCall( + 'addDriver', + [new Reference('app.core.schemadef_driver'), "{$type}\\{$module}\\Entity"] + ); + } } + $container->findDefinition('app.core.schemadef_driver') + ->addMethodCall('addPaths', ['$paths' => $entity_paths]); + $module_manager->preRegisterEvents(); file_put_contents(CACHE_FILE, "findDefinition('doctrine.orm.default_metadata_driver') - ->addMethodCall('addDriver', - [new Reference('app.core.schemadef_driver'), 'Plugin\\Entity']) - ->addMethodCall('addDriver', - [new Reference('app.core.schemadef_driver'), 'Component\\Entity']); + ModuleManager::process($container); } } diff --git a/src/Kernel.php b/src/Kernel.php index 726b804746..6a4c4eb7e8 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -121,7 +121,7 @@ class Kernel extends BaseKernel { parent::build($container); - $container->addCompilerPass(new SchemaDefPass()); $container->addCompilerPass(new ModuleManagerPass()); + $container->addCompilerPass(new SchemaDefPass()); } }