[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;
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, "<?php\nreturn " . var_export($module_manager, true) . ';');

View File

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

View File

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