Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,15 @@
*
* Tables already present in the schema are left as they are.
*/
final class OrmEntitiesSchemaSubscriber implements EventSubscriberInterface
final readonly class OrmEntitiesSchemaSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $entityManager;

/** @var list<class-string> */
private array $entityClasses;

/**
* @param list<class-string> $entityClasses
*/
public function __construct(EntityManagerInterface $entityManager, array $entityClasses)
{
$this->entityManager = $entityManager;
$this->entityClasses = $entityClasses;
public function __construct(
private EntityManagerInterface $entityManager,
private array $entityClasses
) {
}

public static function getSubscribedEvents(): array
Expand All @@ -62,9 +57,7 @@ public function onBuildSchema(SchemaBuilderEvent $event): void
{
$metadataFactory = $this->entityManager->getMetadataFactory();
$classMetadata = array_map(
static function (string $class) use ($metadataFactory): ClassMetadata {
return $metadataFactory->getMetadataFor($class);
},
static fn (string $class): ClassMetadata => $metadataFactory->getMetadataFor($class),
$this->entityClasses,
);

Expand All @@ -77,9 +70,7 @@ static function (string $class) use ($metadataFactory): ClassMetadata {
// own table, regardless of which classes were actually requested here. Only transplant the
// tables that genuinely belong to the requested entities.
$ownTableNames = array_map(
static function (ClassMetadata $metadata): string {
return $metadata->getTableName();
},
static fn (ClassMetadata $metadata): string => $metadata->getTableName(),
$classMetadata,
);

Expand All @@ -97,11 +88,7 @@ static function (ClassMetadata $metadata): string {
}

$ownSequenceNames = array_filter(array_map(
static function (ClassMetadata $metadata): ?string {
return isset($metadata->sequenceGeneratorDefinition['sequenceName'])
? $metadata->sequenceGeneratorDefinition['sequenceName']
: null;
},
static fn (ClassMetadata $metadata): ?string => $metadata->sequenceGeneratorDefinition['sequenceName'] ?? null,
$classMetadata,
));

Expand Down Expand Up @@ -129,8 +116,10 @@ static function (ClassMetadata $metadata): ?string {
* SchemaImporter uses when building a table from a parsed Yaml array, just reading from an
* already-built Table here instead.
*/
private function copyTable(Table $source, Table $target): void
{
private function copyTable(
Table $source,
Table $target
): void {
foreach ($source->getColumns() as $column) {
$options = [
'length' => $column->getLength(),
Expand Down Expand Up @@ -170,8 +159,8 @@ private function copyTable(Table $source, Table $target): void
foreach ($source->getForeignKeys() as $foreignKey) {
$target->addForeignKeyConstraint(
$foreignKey->getForeignTableName(),
$foreignKey->getUnquotedLocalColumns(),
$foreignKey->getUnquotedForeignColumns(),
array_values($foreignKey->getUnquotedLocalColumns()),
array_values($foreignKey->getUnquotedForeignColumns()),
$foreignKey->getOptions(),
$foreignKey->getName(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -49,9 +47,10 @@ final class OrmEntitiesSchemaSubscriberTest extends TestCase
protected function setUp(): void
{
$configuration = new Configuration();
$configuration->setMetadataDriverImpl(new SimplifiedXmlDriver([
__DIR__ . '/Fixtures/orm' => 'Ibexa\Tests\Bundle\RepositoryInstaller\Event\Subscriber\Fixtures\Entity',
]));
$configuration->setMetadataDriverImpl(new SimplifiedXmlDriver(
[__DIR__ . '/Fixtures/orm' => 'Ibexa\Tests\Bundle\RepositoryInstaller\Event\Subscriber\Fixtures\Entity'],
isXsdValidationEnabled: true,
));
$configuration->setProxyDir(sys_get_temp_dir());
$configuration->setProxyNamespace('Ibexa\Tests\Bundle\RepositoryInstaller\Event\Subscriber\Proxies');

Expand All @@ -77,13 +76,14 @@ public function testAddsTablesOfListedEntitiesAsSchemaToolGeneratesThem(Abstract
}

/**
* @return iterable<string, array{\Doctrine\DBAL\Platforms\AbstractPlatform}>
* @return iterable<string, array{AbstractPlatform}>
*/
public static function providePlatforms(): iterable
{
yield 'MySQL' => [new MySQL57Platform()];
yield 'PostgreSQL' => [new PostgreSQL94Platform()];
yield 'SQLite' => [new SqlitePlatform()];
yield 'MySQL' => [new MySQLPlatform()];
yield 'PostgreSQL' => [new PostgreSQLPlatform()];
// The SQLite platform class was renamed between DBAL 3 and 4 - let DBAL pick it.
yield 'SQLite' => [DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true])->getDatabasePlatform()];
}

public function testAddsSequencesOfListedEntitiesAsSchemaToolGeneratesThem(): void
Expand Down Expand Up @@ -123,9 +123,7 @@ public function testLeavesTablesAlreadyInTheSchemaUntouched(): void
$schema = $this->dispatch($schema);

$columnNames = array_map(
static function (Column $column): string {
return $column->getName();
},
static fn (Column $column): string => $column->getName(),
array_values($schema->getTable('test_orm_category')->getColumns()),
);

Expand All @@ -138,27 +136,19 @@ private function getSchemaToolSchema(): Schema
$metadataFactory = $this->entityManager->getMetadataFactory();

return (new SchemaTool($this->entityManager))->getSchemaFromMetadata(array_map(
static function (string $class) use ($metadataFactory) {
return $metadataFactory->getMetadataFor($class);
},
static fn (string $class) => $metadataFactory->getMetadataFor($class),
self::ENTITY_CLASSES,
));
}

/**
* @return array<string, array<string>>
* @return list<string>
*/
private function getCreateTablesSql(Schema $schema, AbstractPlatform $platform): array
{
return array_map(
static function (Table $table) use ($platform): array {
return $platform->getCreateTableSQL(
$table,
AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS,
);
},
$schema->getTables(),
);
private function getCreateTablesSql(
Schema $schema,
AbstractPlatform $platform
): array {
return $platform->getCreateTablesSQL($schema->getTables());
}

/**
Expand All @@ -168,12 +158,10 @@ static function (Table $table) use ($platform): array {
*/
private function getCreateSequencesSql(Schema $schema): array
{
$platform = new PostgreSQL94Platform();
$platform = new PostgreSQLPlatform();

return array_map(
static function (Sequence $sequence) use ($platform): string {
return $platform->getCreateSequenceSQL($sequence);
},
static fn (Sequence $sequence): string => $platform->getCreateSequenceSQL($sequence),
$schema->getSequences(),
);
}
Expand Down
Loading