5
5
namespace Yokai \EnumBundle \DependencyInjection ;
6
6
7
7
use Symfony \Bundle \TwigBundle \TwigBundle ;
8
- use Symfony \Component \Config \FileLocator ;
9
8
use Symfony \Component \DependencyInjection \ContainerBuilder ;
10
- use Symfony \Component \DependencyInjection \Loader \ XmlFileLoader ;
9
+ use Symfony \Component \DependencyInjection \Reference ;
11
10
use Symfony \Component \Form \FormInterface ;
12
11
use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
13
12
use Symfony \Component \Validator \Validator \ValidatorInterface ;
14
13
use Yokai \EnumBundle \EnumInterface ;
14
+ use Yokai \EnumBundle \EnumRegistry ;
15
+ use Yokai \EnumBundle \Form \Extension \EnumTypeGuesser ;
16
+ use Yokai \EnumBundle \Form \Type \EnumType ;
17
+ use Yokai \EnumBundle \Twig \Extension \EnumExtension as EnumTwigExtension ;
18
+ use Yokai \EnumBundle \Validator \Constraints \EnumValidator ;
15
19
16
20
/**
17
21
* @author Yann Eugoné <[email protected] >
@@ -23,24 +27,34 @@ class EnumExtension extends Extension
23
27
*/
24
28
public function load (array $ configs , ContainerBuilder $ container ): void
25
29
{
26
- $ xmlLoader = new XmlFileLoader ($ container , new FileLocator (__DIR__ . '/../Resources/config ' ));
27
- $ xmlLoader ->load ('enum.xml ' );
30
+ $ container ->register ('yokai_enum.enum_registry ' , EnumRegistry::class);
31
+ $ container ->setAlias (EnumRegistry::class, 'yokai_enum.enum_registry ' );
32
+
33
+ $ registry = new Reference (EnumRegistry::class);
28
34
29
35
$ requiresForm = interface_exists (FormInterface::class);
30
36
$ requiresValidator = interface_exists (ValidatorInterface::class);
31
37
$ requiresTwig = class_exists (TwigBundle::class);
32
38
33
39
if ($ requiresForm ) {
34
- $ xmlLoader ->load ('form.xml ' );
35
- if (!$ requiresValidator ) {
36
- $ container ->removeDefinition ('form_extention.type_guesser.enum ' );
40
+ $ container ->register ('yokai_enum.form_type.enum_type ' , EnumType::class)
41
+ ->setArgument ('$enumRegistry ' , $ registry )
42
+ ->addTag ('form.type ' );
43
+ if ($ requiresValidator ) {
44
+ $ container ->register ('yokai_enum.form_extension.enum_type_guesser ' , EnumTypeGuesser::class)
45
+ ->setArgument ('$metadataFactory ' , new Reference ('validator.mapping.class_metadata_factory ' ))
46
+ ->addTag ('form.type_guesser ' );
37
47
}
38
48
}
39
49
if ($ requiresValidator ) {
40
- $ xmlLoader ->load ('validator.xml ' );
50
+ $ container ->register ('yokai_enum.validator_constraints.enum_validator ' , EnumValidator::class)
51
+ ->setArgument ('$enumRegistry ' , $ registry )
52
+ ->addTag ('validator.constraint_validator ' );
41
53
}
42
54
if ($ requiresTwig ) {
43
- $ xmlLoader ->load ('twig.xml ' );
55
+ $ container ->register ('yokai_enum.twig_extension.enum_extension ' , EnumTwigExtension::class)
56
+ ->setArgument ('$enumRegistry ' , $ registry )
57
+ ->addTag ('twig.extension ' );
44
58
}
45
59
46
60
$ container ->registerForAutoconfiguration (EnumInterface::class)
0 commit comments