Skip to content

Commit

Permalink
Merge pull request #25 from msmakouz/feature/annotations-2.0
Browse files Browse the repository at this point in the history
Allow doctrine/annotations 2.x
  • Loading branch information
msmakouz authored May 8, 2023
2 parents 01b8b79 + 91387c8 commit 2894e90
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
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);
}
}

0 comments on commit 2894e90

Please sign in to comment.