[DB] Merge definition of SchemaDefDriver with SchemaDefPass for clarity

This commit is contained in:
Hugo Sales 2021-02-19 23:02:40 +00:00
parent fc015c6fdf
commit 74e586182d
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 38 additions and 68 deletions

View File

@ -24,10 +24,9 @@ services:
App\Core\Router\RouteLoader: App\Core\Router\RouteLoader:
tags: ['routing.loader'] tags: ['routing.loader']
# Wrapper arround Doctrine's StaticPHP metadata driver # Wrapper arround Doctrine's StaticPHP metadata driver
app.core.schemadef_driver: app.schemadef_driver:
class: App\Core\DB\SchemaDefDriver class: App\DependencyInjection\Compiler\SchemaDefDriver
arguments: arguments:
- '%kernel.project_dir%/src/Entity' - '%kernel.project_dir%/src/Entity'

View File

@ -26,8 +26,8 @@
* @package GNUsocial * @package GNUsocial
* @category Modules * @category Modules
* *
* @author Hugo Sales <hugo@fc.up.pt> * @author Hugo Sales <hugo@hsal.es>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
@ -100,20 +100,20 @@ class ModuleManager
foreach ($module_paths as $path) { foreach ($module_paths as $path) {
$type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path)); $type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path));
$dir = dirname($path); $dir = dirname($path);
$module = basename($dir); $module = basename($dir); // component or plugin
$fqcn = "\\{$type}\\{$module}\\{$module}"; $fqcn = "\\{$type}\\{$module}\\{$module}";
$module_manager->add($fqcn, $path); $module_manager->add($fqcn, $path);
if (!is_null($container) && file_exists($dir = $dir . '/Entity') && is_dir($dir)) { if (!is_null($container) && file_exists($dir = $dir . '/Entity') && is_dir($dir)) {
$entity_paths[] = $dir; $entity_paths[] = $dir;
$container->findDefinition('doctrine.orm.default_metadata_driver')->addMethodCall( $container->findDefinition('doctrine.orm.default_metadata_driver')->addMethodCall(
'addDriver', 'addDriver',
[new Reference('app.core.schemadef_driver'), "{$type}\\{$module}\\Entity"] [new Reference('app.schemadef_driver'), "{$type}\\{$module}\\Entity"]
); );
} }
} }
if (!is_null($container)) { if (!is_null($container)) {
$container->findDefinition('app.core.schemadef_driver') $container->findDefinition('app.schemadef_driver')
->addMethodCall('addPaths', ['$paths' => $entity_paths]); ->addMethodCall('addPaths', ['$paths' => $entity_paths]);
} }

View File

@ -18,24 +18,43 @@
// }}} // }}}
/** /**
* Doctrine metadata driver which implements our old `schemaDef` interface * Compiler pass which triggers Symfony to tell Doctrine to
* use our `SchemaDef` metadata driver
* *
* @package GNUsocial * @package GNUsocial
* @category DB * @category DB
* *
* @author Hugo Sales <hugo@fc.up.pt> * @author Hugo Sales <hugo@hsal.es>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
namespace App\Core\DB; namespace App\DependencyInjection\Compiler;
use App\Core\Log;
use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver; use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
use Functional as F; use Functional as F;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class SchemaDefDriver extends StaticPHPDriver /**
* Register a new ORM driver to allow use to use the old (and better) schemaDef format
*/
class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface
{ {
/**
* Register `app.schemadef_driver` (this class instantiated with argument src/Entity) as a metadata driver
*/
public function process(ContainerBuilder $container)
{
$container->findDefinition('doctrine.orm.default_metadata_driver')
->addMethodCall('addDriver',
[new Reference('app.schemadef_driver'), 'App\\Entity']
);
}
/** /**
* V2 DB type => Doctrine type * V2 DB type => Doctrine type
*/ */
@ -75,6 +94,8 @@ class SchemaDefDriver extends StaticPHPDriver
{ {
$schema = $class_name::schemaDef(); $schema = $class_name::schemaDef();
Log::emergency($class_name);
$metadata->setPrimaryTable([ $metadata->setPrimaryTable([
'name' => $schema['name'], 'name' => $schema['name'],
'indexes' => self::kv_to_name_col($schema['indexes'] ?? []), 'indexes' => self::kv_to_name_col($schema['indexes'] ?? []),

View File

@ -1,50 +0,0 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* Compiler pass which triggers Symfony to tell Doctrine to
* use out `SchemaDef` metadata driver
*
* @package GNUsocial
* @category DB
*
* @author Hugo Sales <hugo@fc.up.pt>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace App\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Register a new ORM driver to allow use to use the old (and better) schemaDef format
*/
class SchemaDefPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->findDefinition('doctrine.orm.default_metadata_driver')
->addMethodCall('addDriver',
[new Reference('app.core.schemadef_driver'), 'App\\Entity']
);
}
}

View File

@ -23,15 +23,15 @@
* @package GNUsocial * @package GNUsocial
* @category Kernel * @category Kernel
* *
* @author Hugo Sales <hugo@fc.up.pt> * @author Hugo Sales <hugo@hsal.es>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
namespace App; namespace App;
use App\DependencyInjection\Compiler\ModuleManagerPass; use App\DependencyInjection\Compiler\ModuleManagerPass;
use App\DependencyInjection\Compiler\SchemaDefPass; use App\DependencyInjection\Compiler\SchemaDefDriver;
use const PHP_VERSION_ID; use const PHP_VERSION_ID;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Loader\LoaderInterface;
@ -152,6 +152,6 @@ class Kernel extends BaseKernel
parent::build($container); parent::build($container);
$container->addCompilerPass(new ModuleManagerPass()); $container->addCompilerPass(new ModuleManagerPass());
$container->addCompilerPass(new SchemaDefPass()); $container->addCompilerPass(new SchemaDefDriver(SRCDIR . '/Entity'));
} }
} }