Skip to content

Commit efbb00c

Browse files
committed
make attribute name optional
1 parent 1fa853e commit efbb00c

5 files changed

Lines changed: 41 additions & 8 deletions

File tree

docs/en/dbal-type.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ DBAL Types
33

44
Custom DBAL types can be registered using the ``AsDatabaseType`` attribute. This
55
attribute allows you to define a name for your custom type directly in the class
6-
definition.
6+
definition. If the name is not provided, the class name will be used as the default.
77

88
To register a custom DBAL type, create a class that extends
99
``Doctrine\DBAL\Types\Type`` and add the ``#[AsDatabaseType]`` attribute to it:

src/Attribute/AsDatabaseType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#[Attribute(Attribute::TARGET_CLASS)]
1010
final readonly class AsDatabaseType
1111
{
12-
public function __construct(public string $name)
12+
public function __construct(public string|null $name = null)
1313
{
1414
}
1515
}

src/DependencyInjection/DoctrineExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use InvalidArgumentException;
4444
use LogicException;
4545
use ReflectionClass;
46+
use Reflector;
4647
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
4748
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
4849
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
@@ -552,10 +553,11 @@ private function dbalLoad(array $config, ContainerBuilder $container): void
552553
$this->loadDbalConnection($name, $connection, $container);
553554
}
554555

555-
$container->registerAttributeForAutoconfiguration(AsDatabaseType::class, static function (ChildDefinition $definition, AsDatabaseType $type): void {
556+
$container->registerAttributeForAutoconfiguration(AsDatabaseType::class, static function (ChildDefinition $definition, AsDatabaseType $type, Reflector $reflector): void {
557+
assert($reflector instanceof ReflectionClass);
556558
$tag = 'doctrine.dbal.type';
557559
$attributes = [
558-
'name' => $type->name,
560+
'name' => $type->name ?? $reflector->name,
559561
];
560562

561563
// Determine if the version of symfony/dependency-injection is >= 7.3

tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
1313
use Doctrine\Bundle\DoctrineBundle\Tests\Builder\BundleConfigurationBuilder;
1414
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\DbalType;
15+
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\DbalTypeNoName;
1516
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\Php8EntityListener;
1617
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\Php8EventListener;
1718
use Doctrine\DBAL\Connection;
@@ -950,7 +951,9 @@ public static function cacheConfigurationProvider(): array
950951
];
951952
}
952953

953-
public function testAsDatabaseTypeAttribute(): void
954+
/** @param class-string $typeClassname */
955+
#[DataProvider('provideDatabaseTypeAttribute')]
956+
public function testAsDatabaseTypeAttribute(string $typeClassname, string $expectedName): void
954957
{
955958
$container = $this->getContainer();
956959
$extension = new DoctrineExtension();
@@ -968,17 +971,26 @@ public function testAsDatabaseTypeAttribute(): void
968971
: $container->getAutoconfiguredAttributes();
969972
$this->assertInstanceOf(Closure::class, $attributes[AsDatabaseType::class]);
970973

971-
$reflector = new ReflectionClass(DbalType::class);
974+
$reflector = new ReflectionClass($typeClassname);
972975
$definition = new ChildDefinition('');
973976
$attribute = $reflector->getAttributes(AsDatabaseType::class)[0]->newInstance();
974977

975-
$attributes[AsDatabaseType::class]($definition, $attribute);
978+
$attributes[AsDatabaseType::class]($definition, $attribute, $reflector);
976979

977-
$expected = ['name' => 'dbal_type'];
980+
$expected = ['name' => $expectedName];
978981
$this->assertSame([$expected], $definition->getTag('doctrine.dbal.type'));
979982
$this->assertSame([['source' => 'by tag "doctrine.dbal.type"']], $definition->getTag('container.excluded'));
980983
}
981984

985+
/** @return array<array{0: class-string, 1: string}> */
986+
public static function provideDatabaseTypeAttribute(): array
987+
{
988+
return [
989+
'with name' => [DbalType::class, 'dbal_type'],
990+
'without name' => [DbalTypeNoName::class, DbalTypeNoName::class],
991+
];
992+
}
993+
982994
/** @return array<array{0: class-string}> */
983995
public static function provideAttributeExcludedFromContainer(): array
984996
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures;
6+
7+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDatabaseType;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
9+
use Doctrine\DBAL\Types\Type;
10+
11+
#[AsDatabaseType]
12+
class DbalTypeNoName extends Type
13+
{
14+
/** @param array<string, mixed> $column */
15+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
16+
{
17+
return 'dbal_type_no_name';
18+
}
19+
}

0 commit comments

Comments
 (0)