Skip to content
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

Allow doctrine/annotations 2.x #25

Merged
merged 2 commits into from
May 8, 2023
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": "^8.1",
"spiral/filters": "^3.1",
"spiral/validation": "^3.1",
"doctrine/annotations": "^1.13",
"doctrine/annotations": "^1.13 || ^2.0",
"symfony/cache": "^6.0",
"symfony/psr-http-message-bridge": "^2",
"symfony/validator": "^6.0",
Expand Down
16 changes: 8 additions & 8 deletions src/Bootloader/ValidatorBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\Constraints\EmailValidator;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class ValidatorBootloader extends Bootloader
{
Expand All @@ -24,7 +25,8 @@ class ValidatorBootloader extends Bootloader
];

protected const SINGLETONS = [
Validation::class => [self::class, 'initValidation'],
Validation::class => Validation::class,
ValidatorInterface::class => [self::class, 'initSymfonyValidator'],
ConstraintValidatorFactoryInterface::class => ContainerConstraintValidatorFactory::class,
];

Expand All @@ -50,14 +52,12 @@ public function boot(ValidationProvider $provider, ValidationBootloader $validat
$validation->setDefaultValidator(FilterDefinition::class);
}

private function initValidation(ConstraintValidatorFactoryInterface $validatorFactory): ValidationInterface
private function initSymfonyValidator(ConstraintValidatorFactoryInterface $validatorFactory): ValidatorInterface
{
return new Validation(
\Symfony\Component\Validator\Validation::createValidatorBuilder()
->enableAnnotationMapping()
->setConstraintValidatorFactory($validatorFactory)
->getValidator()
);
return \Symfony\Component\Validator\Validation::createValidatorBuilder()
->enableAnnotationMapping()
->setConstraintValidatorFactory($validatorFactory)
->getValidator();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/src/Functional/Bootloader/ValidatorBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Validator\Constraints\EmailValidator;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
use Symfony\Component\Validator\Validator\RecursiveValidator;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class ValidatorBootloaderTest extends TestCase
{
Expand Down Expand Up @@ -49,4 +51,9 @@ public function testEmailValidatorShouldBeBound(): void
(new \ReflectionProperty($validator, 'defaultMode'))->getValue($validator)
);
}

public function testSymfonyValidatorShouldBeBoundAsSingleton(): void
{
$this->assertContainerBoundAsSingleton(ValidatorInterface::class, RecursiveValidator::class);
}
}