2020-03-15 21:21:11 +00:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// {{{ 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/>.
|
|
|
|
// }}}
|
|
|
|
|
2020-03-21 20:18:05 +00:00
|
|
|
/**
|
2021-02-19 23:02:40 +00:00
|
|
|
* Compiler pass which triggers Symfony to tell Doctrine to
|
|
|
|
* use our `SchemaDef` metadata driver
|
2020-03-21 20:18:05 +00:00
|
|
|
*
|
2021-02-19 23:02:40 +00:00
|
|
|
* @package GNUsocial
|
2020-03-21 20:18:05 +00:00
|
|
|
* @category DB
|
|
|
|
*
|
2021-02-19 23:02:40 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-03-21 20:18:05 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2021-02-19 23:02:40 +00:00
|
|
|
namespace App\DependencyInjection\Compiler;
|
2020-03-15 21:21:11 +00:00
|
|
|
|
2021-09-06 23:47:28 +01:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
2021-10-10 09:26:18 +01:00
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata;
|
2020-03-15 21:21:11 +00:00
|
|
|
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
|
2021-10-10 09:26:18 +01:00
|
|
|
use Exception;
|
2020-03-28 15:40:28 +00:00
|
|
|
use Functional as F;
|
2021-02-19 23:02:40 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2020-03-15 21:21:11 +00:00
|
|
|
|
2021-02-19 23:02:40 +00:00
|
|
|
/**
|
|
|
|
* Register a new ORM driver to allow use to use the old (and better) schemaDef format
|
|
|
|
*/
|
|
|
|
class SchemaDefDriver extends StaticPHPDriver implements CompilerPassInterface
|
2020-03-15 21:21:11 +00:00
|
|
|
{
|
2021-02-19 23:02:40 +00:00
|
|
|
/**
|
|
|
|
* Register `app.schemadef_driver` (this class instantiated with argument src/Entity) as a metadata driver
|
2021-07-20 15:07:22 +01:00
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2021-02-19 23:02:40 +00:00
|
|
|
*/
|
|
|
|
public function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$container->findDefinition('doctrine.orm.default_metadata_driver')
|
2021-10-10 09:26:18 +01:00
|
|
|
->addMethodCall(
|
|
|
|
'addDriver',
|
|
|
|
[new Reference('app.schemadef_driver'), 'App\\Entity'],
|
|
|
|
);
|
2021-02-19 23:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:52:08 +00:00
|
|
|
/**
|
2020-07-05 14:12:35 +01:00
|
|
|
* V2 DB type => Doctrine type
|
2020-03-18 10:52:08 +00:00
|
|
|
*/
|
|
|
|
private const types = [
|
2020-07-26 01:18:15 +01:00
|
|
|
'varchar' => 'string',
|
|
|
|
'char' => 'string', // char is a fixed witdh varchar
|
|
|
|
'int' => 'integer',
|
|
|
|
'serial' => 'integer',
|
|
|
|
'tinyint' => 'smallint', // no portable tinyint
|
|
|
|
'bigint' => 'bigint',
|
|
|
|
'bool' => 'boolean',
|
|
|
|
'numeric' => 'decimal',
|
|
|
|
'text' => 'text',
|
|
|
|
'datetime' => 'datetime',
|
|
|
|
'timestamp' => 'datetime',
|
|
|
|
'phone_number' => 'phone_number',
|
2020-03-18 10:52:08 +00:00
|
|
|
// Unused in V2, but might start being used
|
|
|
|
'date' => 'date',
|
|
|
|
'time' => 'time',
|
|
|
|
'datetimez' => 'datetimez',
|
|
|
|
'object' => 'object',
|
|
|
|
'array' => 'array',
|
|
|
|
'simplearray' => 'simplearray',
|
|
|
|
'json_array' => 'json_array',
|
|
|
|
'float' => 'float',
|
|
|
|
'guid' => 'guid',
|
|
|
|
'blob' => 'blob',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2020-03-30 18:15:30 +01:00
|
|
|
* Fill in the database $metadata for $class_name
|
2021-10-21 14:45:18 +01:00
|
|
|
*
|
|
|
|
* @param class-string $class_name
|
|
|
|
* @param ClassMetadataInfo $metadata ClassMetadataInfo is the real type, but we need to override the method
|
2020-03-18 10:52:08 +00:00
|
|
|
*/
|
2022-03-06 23:50:40 +00:00
|
|
|
public function loadMetadataForClass($class_name, ClassMetadata $metadata): void
|
2020-03-15 21:21:11 +00:00
|
|
|
{
|
2020-03-30 18:15:30 +01:00
|
|
|
$schema = $class_name::schemaDef();
|
2020-03-18 10:52:08 +00:00
|
|
|
|
2020-09-10 21:42:17 +01:00
|
|
|
$metadata->setPrimaryTable([
|
|
|
|
'name' => $schema['name'],
|
|
|
|
'indexes' => self::kv_to_name_col($schema['indexes'] ?? []),
|
|
|
|
'uniqueConstraints' => self::kv_to_name_col($schema['unique keys'] ?? []),
|
|
|
|
'options' => ['comment' => $schema['description'] ?? ''],
|
2020-03-30 15:43:45 +01:00
|
|
|
]);
|
2020-03-18 10:52:08 +00:00
|
|
|
|
|
|
|
foreach ($schema['fields'] as $name => $opts) {
|
2021-03-11 22:22:42 +00:00
|
|
|
$unique = null;
|
|
|
|
foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) {
|
2021-10-10 09:26:18 +01:00
|
|
|
if (\in_array($name, $uniq_arr)) {
|
2021-03-11 22:22:42 +00:00
|
|
|
$unique = $key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 22:17:26 +00:00
|
|
|
// @phpstan-ignore-next-line
|
2021-04-11 19:32:28 +01:00
|
|
|
if (false && $opts['foreign key'] ?? false) {
|
2021-07-20 15:07:22 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
// TODO: Get foreign keys working
|
2021-03-11 22:22:42 +00:00
|
|
|
foreach (['target', 'multiplicity'] as $f) {
|
|
|
|
if (!isset($opts[$f])) {
|
2021-10-10 09:26:18 +01:00
|
|
|
throw new Exception("{$class_name}.{$name} doesn't have the required field `{$f}`");
|
2021-03-11 22:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 17:13:46 +00:00
|
|
|
// See Doctrine\ORM\Mapping::associationMappings
|
|
|
|
|
|
|
|
// TODO still need to map nullability, comment, fk name and such, but
|
|
|
|
// the interface doesn't seem to support it currently
|
2021-07-20 15:07:22 +01:00
|
|
|
[$target_entity, $target_field] = explode('.', $opts['target']);
|
|
|
|
$map = [
|
2021-03-11 22:22:42 +00:00
|
|
|
'fieldName' => $name,
|
|
|
|
'targetEntity' => $target_entity,
|
|
|
|
'joinColumns' => [[
|
|
|
|
'name' => $name,
|
|
|
|
'referencedColumnName' => $target_field,
|
|
|
|
]],
|
2021-10-10 09:26:18 +01:00
|
|
|
'id' => \in_array($name, $schema['primary key']),
|
2021-03-11 22:22:42 +00:00
|
|
|
'unique' => $unique,
|
|
|
|
];
|
|
|
|
|
2021-02-21 17:13:46 +00:00
|
|
|
switch ($opts['multiplicity']) {
|
|
|
|
case 'one to one':
|
2021-03-11 22:22:42 +00:00
|
|
|
$metadata->mapOneToOne($map);
|
|
|
|
break;
|
|
|
|
case 'many to one':
|
|
|
|
$metadata->mapManyToOne($map);
|
|
|
|
break;
|
|
|
|
case 'one to many':
|
|
|
|
$map['mappedBy'] = $target_field;
|
|
|
|
$metadata->mapOneToMany($map);
|
|
|
|
break;
|
|
|
|
case 'many to many':
|
|
|
|
$metadata->mapManyToMany($map);
|
2020-03-28 15:40:28 +00:00
|
|
|
break;
|
2021-02-21 17:13:46 +00:00
|
|
|
default:
|
2021-10-10 09:26:18 +01:00
|
|
|
throw new Exception("Invalid multiplicity specified: '${opts['multiplicity']}' in class: {$class_name}");
|
2021-02-21 17:13:46 +00:00
|
|
|
}
|
2021-07-20 15:07:22 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-02-21 17:13:46 +00:00
|
|
|
} else {
|
|
|
|
// Convert old to new types
|
|
|
|
// For ints, prepend the size (smallint)
|
|
|
|
// The size field doesn't exist otherwise
|
2021-03-11 22:22:42 +00:00
|
|
|
$type = self::types[($opts['size'] ?? '') . $opts['type']];
|
2021-02-21 17:13:46 +00:00
|
|
|
$default = $opts['default'] ?? null;
|
|
|
|
|
|
|
|
$field = [
|
|
|
|
// boolean, optional
|
2021-10-10 09:26:18 +01:00
|
|
|
'id' => \in_array($name, $schema['primary key']),
|
2021-02-21 17:13:46 +00:00
|
|
|
// string
|
|
|
|
'fieldName' => $name,
|
|
|
|
// string
|
|
|
|
'type' => $type,
|
|
|
|
// string, optional
|
|
|
|
'unique' => $unique,
|
|
|
|
// String length, ignored if not a string
|
|
|
|
// int, optional
|
|
|
|
'length' => $opts['length'] ?? null,
|
|
|
|
// boolean, optional
|
|
|
|
'nullable' => !($opts['not null'] ?? false),
|
|
|
|
// Numeric precision and scale, ignored if not a number
|
|
|
|
// integer, optional
|
|
|
|
'precision' => $opts['precision'] ?? null,
|
|
|
|
// integer, optional
|
|
|
|
'scale' => $opts['scale'] ?? null,
|
|
|
|
'options' => [
|
|
|
|
'comment' => $opts['description'] ?? null,
|
|
|
|
'default' => $default,
|
|
|
|
'unsigned' => $opts['unsigned'] ?? null,
|
|
|
|
// bool, optional
|
|
|
|
'fixed' => $opts['type'] === 'char',
|
|
|
|
// 'collation' => string, unused
|
|
|
|
// 'check', unused
|
|
|
|
],
|
|
|
|
// 'columnDefinition', unused
|
|
|
|
];
|
|
|
|
// The optional feilds from earlier were populated with null, remove them
|
|
|
|
$field = array_filter($field, F\not('is_null'));
|
|
|
|
$field['options'] = array_filter($field['options'], F\not('is_null'));
|
|
|
|
|
|
|
|
$metadata->mapField($field);
|
|
|
|
if ($opts['type'] === 'serial') {
|
|
|
|
$metadata->setIdGeneratorType($metadata::GENERATOR_TYPE_AUTO);
|
|
|
|
}
|
2020-03-30 18:15:30 +01:00
|
|
|
}
|
2020-03-18 10:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override StaticPHPDriver's method,
|
|
|
|
* we care about classes that have the method `schemaDef`,
|
|
|
|
* instead of `loadMetadata`.
|
|
|
|
*/
|
2021-10-10 09:26:18 +01:00
|
|
|
public function isTransient($class_name): bool
|
2020-03-18 10:52:08 +00:00
|
|
|
{
|
2020-03-30 18:15:30 +01:00
|
|
|
return !method_exists($class_name, 'schemaDef');
|
2020-03-18 10:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert [$key => $val] to ['name' => $key, 'columns' => $val]
|
|
|
|
*/
|
|
|
|
private static function kv_to_name_col(array $arr): array
|
|
|
|
{
|
|
|
|
$res = [];
|
|
|
|
foreach ($arr as $name => $cols) {
|
|
|
|
$res[] = ['name' => $name, 'columns' => $cols];
|
|
|
|
}
|
|
|
|
return $res;
|
2020-03-15 21:21:11 +00:00
|
|
|
}
|
|
|
|
}
|