DoctrineValidationPass.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. /**
  14. * Registers additional validators.
  15. *
  16. * @author Benjamin Eberlei <kontakt@beberlei.de>
  17. */
  18. class DoctrineValidationPass implements CompilerPassInterface
  19. {
  20. private $managerType;
  21. public function __construct(string $managerType)
  22. {
  23. $this->managerType = $managerType;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function process(ContainerBuilder $container)
  29. {
  30. $this->updateValidatorMappingFiles($container, 'xml', 'xml');
  31. $this->updateValidatorMappingFiles($container, 'yaml', 'yml');
  32. }
  33. /**
  34. * Gets the validation mapping files for the format and extends them with
  35. * files matching a doctrine search pattern (Resources/config/validation.orm.xml).
  36. */
  37. private function updateValidatorMappingFiles(ContainerBuilder $container, string $mapping, string $extension)
  38. {
  39. if (!$container->hasParameter('validator.mapping.loader.'.$mapping.'_files_loader.mapping_files')) {
  40. return;
  41. }
  42. $files = $container->getParameter('validator.mapping.loader.'.$mapping.'_files_loader.mapping_files');
  43. $validationPath = '/config/validation.'.$this->managerType.'.'.$extension;
  44. foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
  45. if ($container->fileExists($file = $bundle['path'].'/Resources'.$validationPath) || $container->fileExists($file = $bundle['path'].$validationPath)) {
  46. $files[] = $file;
  47. }
  48. }
  49. $container->setParameter('validator.mapping.loader.'.$mapping.'_files_loader.mapping_files', $files);
  50. }
  51. }