-
-
Notifications
You must be signed in to change notification settings - Fork 480
feat: add AsDbalType attribute #2197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
syl20b
wants to merge
7
commits into
doctrine:3.2.x
Choose a base branch
from
syl20b:add-as-doctrine-type-attribute
base: 3.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+258
−3
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c20d4c5
feat: add AsDoctrineType attribute
syl20b 822ae08
rename AsDoctrineType to AsDbalType
syl20b 4e2dd40
use resources tags
syl20b ba7b961
fix phpstan
syl20b 82ce149
rename compiler pass
syl20b 9996357
add tests
syl20b ea214eb
add compatibility with Symfony < 7.3
syl20b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Bundle\DoctrineBundle\Attribute; | ||
|
|
||
| use Attribute; | ||
|
|
||
| #[Attribute(Attribute::TARGET_CLASS)] | ||
| final readonly class AsDbalType | ||
| { | ||
| public function __construct(public string $name) | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
|
||
| use Doctrine\DBAL\Types\Type; | ||
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
|
|
||
| use function array_key_exists; | ||
| use function is_subclass_of; | ||
| use function method_exists; | ||
| use function sprintf; | ||
|
|
||
| /** @internal */ | ||
| final class RegisterDbalTypePass implements CompilerPassInterface | ||
| { | ||
| public function process(ContainerBuilder $container): void | ||
| { | ||
| $types = $container->getParameter('doctrine.dbal.connection_factory.types'); | ||
|
|
||
| foreach ($this->findTaggedResourceIds($container) as $id => $tags) { | ||
| foreach ($tags as $tag) { | ||
| if (! array_key_exists('name', $tag)) { | ||
| throw new InvalidArgumentException(sprintf('The "name" attribute is mandatory for the "doctrine.dbal.type" tag on the "%s" type.', $id)); | ||
| } | ||
|
|
||
| $class = $container->getDefinition($id)->getClass(); | ||
| if (! $class) { | ||
| throw new InvalidArgumentException(sprintf('The definition of "%s" must define its class.', $id)); | ||
| } | ||
|
|
||
| if (! is_subclass_of($class, Type::class)) { | ||
| throw new InvalidArgumentException(sprintf('The "%s" class must extends "%s".', $class, Type::class)); | ||
| } | ||
|
|
||
| $types[$tag['name']] = ['class' => $class]; | ||
| } | ||
| } | ||
|
|
||
| $container->setParameter('doctrine.dbal.connection_factory.types', $types); | ||
| } | ||
|
|
||
| /** @return array<string, array<array{name?: string}>> */ | ||
| private function findTaggedResourceIds(ContainerBuilder $container): array | ||
| { | ||
| $tagName = 'doctrine.dbal.type'; | ||
|
|
||
| // Determine if the version of symfony/dependency-injection is >= 7.3 | ||
| /** @phpstan-ignore function.alreadyNarrowedType */ | ||
| if (method_exists($container, 'findTaggedResourceIds')) { | ||
| return $container->findTaggedResourceIds($tagName); | ||
| } | ||
|
|
||
| // Needed to keep compatibility with symfony/dependency-injection < 7.3 | ||
| $tags = []; | ||
| foreach ($container->getDefinitions() as $id => $definition) { | ||
| if (! $definition->hasTag($tagName)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! $definition->hasTag('container.excluded')) { | ||
| throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" is missing the "container.excluded" tag.', $id, $tagName)); | ||
| } | ||
|
|
||
| $class = $container->getParameterBag()->resolveValue($definition->getClass()); | ||
| if (! $class || $definition->isAbstract()) { | ||
| throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" must have a class and not be abstract.', $id, $tagName)); | ||
| } | ||
|
|
||
| if ($definition->getClass() !== $class) { | ||
| $definition->setClass($class); | ||
| } | ||
|
|
||
| $tags[$id] = $definition->getTag($tagName); | ||
| } | ||
|
|
||
| return $tags; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/DependencyInjection/Compiler/RegisterDbalTypePassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Compiler; | ||
|
|
||
| use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RegisterDbalTypePass; | ||
| use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
| use Doctrine\DBAL\Types\Type; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
|
|
||
| use function sprintf; | ||
|
|
||
| class RegisterDbalTypePassTest extends TestCase | ||
| { | ||
| public function testTaggedTypeAreAdded(): void | ||
| { | ||
| $container = new ContainerBuilder(); | ||
| $container->addCompilerPass(new RegisterDbalTypePass()); | ||
|
|
||
| $container->setParameter('doctrine.dbal.connection_factory.types', []); | ||
|
|
||
| $container->register(BarType::class) | ||
| ->addTag('doctrine.dbal.type', ['name' => 'bar']) | ||
| ->addTag('container.excluded'); | ||
|
|
||
| $container->compile(); | ||
|
|
||
| self::assertSame(['bar' => ['class' => BarType::class]], $container->getParameter('doctrine.dbal.connection_factory.types')); | ||
| } | ||
|
|
||
| public function testTagMustHaveANameAttribute(): void | ||
| { | ||
| $container = new ContainerBuilder(); | ||
| $container->addCompilerPass(new RegisterDbalTypePass()); | ||
|
|
||
| $container->setParameter('doctrine.dbal.connection_factory.types', []); | ||
|
|
||
| $container->register(BarType::class) | ||
| ->addTag('doctrine.dbal.type') | ||
| ->addTag('container.excluded'); | ||
|
|
||
| $this->expectException(InvalidArgumentException::class); | ||
| $this->expectExceptionMessage( | ||
| sprintf('The "name" attribute is mandatory for the "doctrine.dbal.type" tag on the "%s" type.', BarType::class), | ||
| ); | ||
|
|
||
| $container->compile(); | ||
| } | ||
|
|
||
| public function testTypeMustBeASubclassOfTheDbalBaseType(): void | ||
| { | ||
| $container = new ContainerBuilder(); | ||
| $container->addCompilerPass(new RegisterDbalTypePass()); | ||
|
|
||
| $container->setParameter('doctrine.dbal.connection_factory.types', []); | ||
|
|
||
| $container->register(NotASubClassOfDbalBaseType::class) | ||
| ->addTag('doctrine.dbal.type', ['name' => 'invalid_type']) | ||
| ->addTag('container.excluded'); | ||
|
|
||
| $this->expectException(InvalidArgumentException::class); | ||
| $this->expectExceptionMessage(sprintf('The "%s" class must extends "%s".', NotASubClassOfDbalBaseType::class, Type::class)); | ||
|
|
||
| $container->compile(); | ||
| } | ||
| } | ||
|
|
||
| class BarType extends Type | ||
| { | ||
| /** @param array<string, mixed> $column */ | ||
| public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
| { | ||
| return 'bar'; | ||
| } | ||
| } | ||
|
|
||
| class NotASubClassOfDbalBaseType | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures; | ||
|
|
||
| use Doctrine\Bundle\DoctrineBundle\Attribute\AsDbalType; | ||
| use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
| use Doctrine\DBAL\Types\Type; | ||
|
|
||
| #[AsDbalType(name: 'dbal_type')] | ||
| class DbalType extends Type | ||
| { | ||
| /** @param array<string, mixed> $column */ | ||
| public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
| { | ||
| return 'dbal_type'; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jean-Beru I used
array_mapto be consistent with the others similar tests.Plus, the return type of the
getAttributeAutoconfigurators()method is an array with the attribute FQCN as a key and the closure as first entry of the sub array :So calling
array_columnas is would not give us the expected behavior.WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, my bad 🙂