Skip to content

Commit f44079e

Browse files
committed
make attribute name optional
1 parent 57eec36 commit f44079e

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
@@ -44,6 +44,7 @@
4444
use InvalidArgumentException;
4545
use LogicException;
4646
use ReflectionClass;
47+
use Reflector;
4748
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
4849
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
4950
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;
@@ -949,7 +950,8 @@ public static function cacheConfigurationProvider(): array
949950
];
950951
}
951952

952-
public function testAsDatabaseTypeAttribute(): void
953+
#[DataProvider('provideDatabaseTypeAttribute')]
954+
public function testAsDatabaseTypeAttribute(string $typeClassname, string $expectedName): void
953955
{
954956
$container = $this->getContainer();
955957
$extension = new DoctrineExtension();
@@ -967,17 +969,27 @@ public function testAsDatabaseTypeAttribute(): void
967969
: $container->getAutoconfiguredAttributes();
968970
$this->assertInstanceOf(Closure::class, $attributes[AsDatabaseType::class]);
969971

970-
$reflector = new ReflectionClass(DbalType::class);
972+
/** @var class-string $typeClassname */
973+
$reflector = new ReflectionClass($typeClassname);
971974
$definition = new ChildDefinition('');
972975
$attribute = $reflector->getAttributes(AsDatabaseType::class)[0]->newInstance();
973976

974-
$attributes[AsDatabaseType::class]($definition, $attribute);
977+
$attributes[AsDatabaseType::class]($definition, $attribute, $reflector);
975978

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

984+
/** @return array<array{0: class-string, 1: string}> */
985+
public static function provideDatabaseTypeAttribute(): array
986+
{
987+
return [
988+
'with name' => [DbalType::class, 'dbal_type'],
989+
'without name' => [DbalTypeNoName::class, DbalTypeNoName::class],
990+
];
991+
}
992+
981993
/** @return array<array{0: class-string}> */
982994
public static function provideAttributeExcludedFromContainer(): array
983995
{
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)