DoctrineValidationPass.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. /**
  15. * Registers additional validators
  16. *
  17. * @author Benjamin Eberlei <kontakt@beberlei.de>
  18. */
  19. class DoctrineValidationPass implements CompilerPassInterface
  20. {
  21. /**
  22. * @var string
  23. */
  24. private $managerType;
  25. public function __construct($managerType)
  26. {
  27. $this->managerType = $managerType;
  28. }
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function process(ContainerBuilder $container)
  33. {
  34. $this->updateValidatorMappingFiles($container, 'xml', 'xml');
  35. $this->updateValidatorMappingFiles($container, 'yaml', 'yml');
  36. }
  37. /**
  38. * Gets the validation mapping files for the format and extends them with
  39. * files matching a doctrine search pattern (Resources/config/validation.orm.xml)
  40. *
  41. * @param ContainerBuilder $container
  42. * @param string $mapping
  43. * @param string $extension
  44. */
  45. private function updateValidatorMappingFiles(ContainerBuilder $container, $mapping, $extension)
  46. {
  47. if (!$container->hasParameter('validator.mapping.loader.'.$mapping.'_files_loader.mapping_files')) {
  48. return;
  49. }
  50. $files = $container->getParameter('validator.mapping.loader.'.$mapping.'_files_loader.mapping_files');
  51. $validationPath = 'Resources/config/validation.'.$this->managerType.'.'.$extension;
  52. foreach ($container->getParameter('kernel.bundles') as $bundle) {
  53. $reflection = new \ReflectionClass($bundle);
  54. if (is_file($file = dirname($reflection->getFilename()).'/'.$validationPath)) {
  55. $files[] = realpath($file);
  56. $container->addResource(new FileResource($file));
  57. }
  58. }
  59. $container->setParameter('validator.mapping.loader.'.$mapping.'_files_loader.mapping_files', $files);
  60. }
  61. }