@@ -947,13 +947,16 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
947947
948948 $ container ->setParameter ('validator.translation_domain ' , $ config ['translation_domain ' ]);
949949
950- list ($ xmlMappings , $ yamlMappings ) = $ this ->getValidatorMappingFiles ($ container );
951- if (count ($ xmlMappings ) > 0 ) {
952- $ validatorBuilder ->addMethodCall ('addXmlMappings ' , array ($ xmlMappings ));
950+ $ files = array ('xml ' => array (), 'yml ' => array ());
951+ $ this ->getValidatorMappingFiles ($ container , $ files );
952+ $ this ->getValidatorMappingFilesFromConfig ($ config , $ files );
953+
954+ if (!empty ($ files ['xml ' ])) {
955+ $ validatorBuilder ->addMethodCall ('addXmlMappings ' , array ($ files ['xml ' ]));
953956 }
954957
955- if (count ( $ yamlMappings ) > 0 ) {
956- $ validatorBuilder ->addMethodCall ('addYamlMappings ' , array ($ yamlMappings ));
958+ if (! empty ( $ files [ ' yml ' ]) ) {
959+ $ validatorBuilder ->addMethodCall ('addYamlMappings ' , array ($ files [ ' yml ' ] ));
957960 }
958961
959962 $ definition = $ container ->findDefinition ('validator.email ' );
@@ -987,41 +990,58 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
987990 }
988991 }
989992
990- private function getValidatorMappingFiles (ContainerBuilder $ container )
993+ private function getValidatorMappingFiles (ContainerBuilder $ container, array & $ files )
991994 {
992- $ files = array (array (), array ());
993-
994995 if (interface_exists ('Symfony\Component\Form\FormInterface ' )) {
995996 $ reflClass = new \ReflectionClass ('Symfony\Component\Form\FormInterface ' );
996- $ files [0 ][] = dirname ($ reflClass ->getFileName ()).'/Resources/config/validation.xml ' ;
997- $ container ->addResource (new FileResource ($ files [ 0 ][ 0 ] ));
997+ $ files [' xml ' ][] = $ file = dirname ($ reflClass ->getFileName ()).'/Resources/config/validation.xml ' ;
998+ $ container ->addResource (new FileResource ($ file ));
998999 }
9991000
10001001 foreach ($ container ->getParameter ('kernel.bundles_metadata ' ) as $ bundle ) {
10011002 $ dirname = $ bundle ['path ' ];
1002- if (is_file ($ file = $ dirname .'/Resources/config/validation.xml ' )) {
1003- $ files [0 ][] = $ file ;
1003+
1004+ if (is_file ($ file = $ dirname .'/Resources/config/validation.yml ' )) {
1005+ $ files ['yml ' ][] = $ file ;
10041006 $ container ->addResource (new FileResource ($ file ));
10051007 }
10061008
1007- if (is_file ($ file = $ dirname .'/Resources/config/validation.yml ' )) {
1008- $ files [1 ][] = $ file ;
1009+ if (is_file ($ file = $ dirname .'/Resources/config/validation.xml ' )) {
1010+ $ files [' xml ' ][] = $ file ;
10091011 $ container ->addResource (new FileResource ($ file ));
10101012 }
10111013
10121014 if (is_dir ($ dir = $ dirname .'/Resources/config/validation ' )) {
1013- foreach (Finder::create ()->followLinks ()->files ()->in ($ dir )->name ('*.xml ' ) as $ file ) {
1014- $ files [0 ][] = $ file ->getPathname ();
1015- }
1016- foreach (Finder::create ()->followLinks ()->files ()->in ($ dir )->name ('*.yml ' ) as $ file ) {
1017- $ files [1 ][] = $ file ->getPathname ();
1018- }
1019-
1015+ $ this ->getValidatorMappingFilesFromDir ($ dir , $ files );
10201016 $ container ->addResource (new DirectoryResource ($ dir ));
10211017 }
10221018 }
1019+ }
1020+
1021+ private function getValidatorMappingFilesFromDir ($ dir , array &$ files )
1022+ {
1023+ foreach (Finder::create ()->followLinks ()->files ()->in ($ dir )->name ('/\.(xml|ya?ml)$/ ' ) as $ file ) {
1024+ $ extension = $ file ->getExtension ();
1025+ $ files ['yaml ' === $ extension ? 'yml ' : $ extension ][] = $ file ->getRealpath ();
1026+ }
1027+ }
10231028
1024- return $ files ;
1029+ private function getValidatorMappingFilesFromConfig (array $ config , array &$ files )
1030+ {
1031+ foreach ($ config ['mapping ' ]['paths ' ] as $ path ) {
1032+ if (is_dir ($ path )) {
1033+ $ this ->getValidatorMappingFilesFromDir ($ path , $ files );
1034+ } elseif (is_file ($ path )) {
1035+ if (preg_match ('/\.(xml|ya?ml)$/ ' , $ path , $ matches )) {
1036+ $ extension = $ matches [1 ];
1037+ $ files ['yaml ' === $ extension ? 'yml ' : $ extension ][] = $ path ;
1038+ } else {
1039+ throw new \RuntimeException (sprintf ('Unsupported mapping type in "%s", supported types are XML & Yaml. ' , $ path ));
1040+ }
1041+ } else {
1042+ throw new \RuntimeException (sprintf ('Could not open file or directory "%s". ' , $ path ));
1043+ }
1044+ }
10251045 }
10261046
10271047 private function registerAnnotationsConfiguration (array $ config , ContainerBuilder $ container , $ loader )
0 commit comments