[MODULE][DB] Added support for loading entity definitions from modules

This commit is contained in:
Hugo Sales 2020-09-07 23:53:44 +00:00 committed by Hugo Sales
parent 7de1654f9a
commit 5fc7647c40
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 19 additions and 11 deletions

View File

@ -34,12 +34,13 @@
namespace App\Core; namespace App\Core;
use App\Util\Formatting; use App\Util\Formatting;
use AppendIterator; use AppendIterator;
use FilesystemIterator; use FilesystemIterator;
use Functional as F; use Functional as F;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class ModuleManager 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_paths = array_merge(glob(INSTALLDIR . '/components/*/*.php'), glob(INSTALLDIR . '/plugins/*/*.php'));
$module_manager = new self(); $module_manager = new self();
$entity_paths = [];
$default_driver = $container->findDefinition('doctrine.orm.default_metadata_driver');
foreach ($module_paths as $path) { foreach ($module_paths as $path) {
// 'modules' and 'plugins' have the same length // 'modules' and 'plugins' have the same length
$type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path)); $type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path));
$module = basename(dirname($path)); $dir = dirname($path);
$module = basename($dir);
$fqcn = "\\{$type}\\{$module}\\{$module}"; $fqcn = "\\{$type}\\{$module}\\{$module}";
$module_manager->add($fqcn, $path); $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(); $module_manager->preRegisterEvents();
file_put_contents(CACHE_FILE, "<?php\nreturn " . var_export($module_manager, true) . ';'); file_put_contents(CACHE_FILE, "<?php\nreturn " . var_export($module_manager, true) . ';');

View File

@ -36,17 +36,11 @@ namespace App\DependencyInjection\Compiler;
use App\Core\ModuleManager; use App\Core\ModuleManager;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class ModuleManagerPass implements CompilerPassInterface class ModuleManagerPass implements CompilerPassInterface
{ {
public function process(ContainerBuilder $container) public function process(ContainerBuilder $container)
{ {
ModuleManager::process(); ModuleManager::process($container);
$container->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']);
} }
} }

View File

@ -121,7 +121,7 @@ class Kernel extends BaseKernel
{ {
parent::build($container); parent::build($container);
$container->addCompilerPass(new SchemaDefPass());
$container->addCompilerPass(new ModuleManagerPass()); $container->addCompilerPass(new ModuleManagerPass());
$container->addCompilerPass(new SchemaDefPass());
} }
} }