@@ -947,13 +947,16 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
947
947
948
948
$ container ->setParameter ('validator.translation_domain ' , $ config ['translation_domain ' ]);
949
949
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 ' ]));
953
956
}
954
957
955
- if (count ( $ yamlMappings ) > 0 ) {
956
- $ validatorBuilder ->addMethodCall ('addYamlMappings ' , array ($ yamlMappings ));
958
+ if (! empty ( $ files [ ' yml ' ]) ) {
959
+ $ validatorBuilder ->addMethodCall ('addYamlMappings ' , array ($ files [ ' yml ' ] ));
957
960
}
958
961
959
962
$ definition = $ container ->findDefinition ('validator.email ' );
@@ -987,41 +990,58 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
987
990
}
988
991
}
989
992
990
- private function getValidatorMappingFiles (ContainerBuilder $ container )
993
+ private function getValidatorMappingFiles (ContainerBuilder $ container, array & $ files )
991
994
{
992
- $ files = array (array (), array ());
993
-
994
995
if (interface_exists ('Symfony\Component\Form\FormInterface ' )) {
995
996
$ 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 ));
998
999
}
999
1000
1000
1001
foreach ($ container ->getParameter ('kernel.bundles_metadata ' ) as $ bundle ) {
1001
1002
$ 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 ;
1004
1006
$ container ->addResource (new FileResource ($ file ));
1005
1007
}
1006
1008
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 ;
1009
1011
$ container ->addResource (new FileResource ($ file ));
1010
1012
}
1011
1013
1012
1014
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 );
1020
1016
$ container ->addResource (new DirectoryResource ($ dir ));
1021
1017
}
1022
1018
}
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
+ }
1023
1028
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
+ }
1025
1045
}
1026
1046
1027
1047
private function registerAnnotationsConfiguration (array $ config , ContainerBuilder $ container , $ loader )
0 commit comments