1111
1212namespace Symfony \Component \Form \Extension \DependencyInjection ;
1313
14+ use Psr \Container \ContainerInterface ;
1415use Symfony \Component \Form \FormExtensionInterface ;
1516use Symfony \Component \Form \FormTypeGuesserChain ;
1617use Symfony \Component \Form \Exception \InvalidArgumentException ;
17- use Symfony \Component \DependencyInjection \ContainerInterface ;
1818
1919class DependencyInjectionExtension implements FormExtensionInterface
2020{
21- private $ container ;
22- private $ typeServiceIds ;
23- private $ typeExtensionServiceIds ;
24- private $ guesserServiceIds ;
2521 private $ guesser ;
2622 private $ guesserLoaded = false ;
23+ private $ typeContainer ;
24+ private $ typeExtensionServices ;
25+ private $ guesserServices ;
26+
27+ // @deprecated to be removed in Symfony 4.0
28+ private $ typeServiceIds ;
29+ private $ guesserServiceIds ;
2730
28- public function __construct (ContainerInterface $ container , array $ typeServiceIds , array $ typeExtensionServiceIds , array $ guesserServiceIds )
31+ /**
32+ * Constructor.
33+ *
34+ * @param ContainerInterface $typeContainer
35+ * @param iterable[] $typeExtensionServices
36+ * @param iterable $guesserServices
37+ */
38+ public function __construct (ContainerInterface $ typeContainer , array $ typeExtensionServices , $ guesserServices , array $ guesserServiceIds = null )
2939 {
30- $ this ->container = $ container ;
31- $ this ->typeServiceIds = $ typeServiceIds ;
32- $ this ->typeExtensionServiceIds = $ typeExtensionServiceIds ;
33- $ this ->guesserServiceIds = $ guesserServiceIds ;
40+ if (null !== $ guesserServiceIds ) {
41+ @trigger_error (sprintf ('Passing four arguments to the %s::__construct() method is deprecated since Symfony 3.3 and will be disallowed in Symfony 4.0. The new constructor only accepts three arguments. ' , __CLASS__ ), E_USER_DEPRECATED );
42+ $ this ->guesserServiceIds = $ guesserServiceIds ;
43+ $ this ->typeServiceIds = $ typeExtensionServices ;
44+ }
45+
46+ $ this ->typeContainer = $ typeContainer ;
47+ $ this ->typeExtensionServices = $ typeExtensionServices ;
48+ $ this ->guesserServices = $ guesserServices ;
3449 }
3550
3651 public function getType ($ name )
3752 {
38- if (!isset ($ this ->typeServiceIds [$ name ])) {
39- throw new InvalidArgumentException (sprintf ('The field type "%s" is not registered with the service container. ' , $ name ));
53+ if (null !== $ this ->guesserServiceIds ) {
54+ if (!isset ($ this ->typeServiceIds [$ name ])) {
55+ throw new InvalidArgumentException (sprintf ('The field type "%s" is not registered in the service container. ' , $ name ));
56+ }
57+
58+ return $ this ->typeContainer ->get ($ this ->typeServiceIds [$ name ]);
4059 }
4160
42- return $ this ->container ->get ($ this ->typeServiceIds [$ name ]);
61+ if (!$ this ->typeContainer ->has ($ name )) {
62+ throw new InvalidArgumentException (sprintf ('The field type "%s" is not registered in the service container. ' , $ name ));
63+ }
64+
65+ return $ this ->typeContainer ->get ($ name );
4366 }
4467
4568 public function hasType ($ name )
4669 {
47- return isset ($ this ->typeServiceIds [$ name ]);
70+ if (null !== $ this ->guesserServiceIds ) {
71+ return isset ($ this ->typeServiceIds [$ name ]);
72+ }
73+
74+ return $ this ->typeContainer ->has ($ name );
4875 }
4976
5077 public function getTypeExtensions ($ name )
5178 {
5279 $ extensions = array ();
5380
54- if (isset ($ this ->typeExtensionServiceIds [$ name ])) {
55- foreach ($ this ->typeExtensionServiceIds [$ name ] as $ serviceId ) {
56- $ extensions [] = $ extension = $ this ->container ->get ($ serviceId );
81+ if (isset ($ this ->typeExtensionServices [$ name ])) {
82+ foreach ($ this ->typeExtensionServices [$ name ] as $ serviceId => $ extension ) {
83+ if (null !== $ this ->guesserServiceIds ) {
84+ $ extension = $ this ->typeContainer ->get ($ serviceId = $ extension );
85+ }
86+
87+ $ extensions [] = $ extension ;
5788
5889 // validate result of getExtendedType() to ensure it is consistent with the service definition
5990 if ($ extension ->getExtendedType () !== $ name ) {
@@ -73,7 +104,7 @@ public function getTypeExtensions($name)
73104
74105 public function hasTypeExtensions ($ name )
75106 {
76- return isset ($ this ->typeExtensionServiceIds [$ name ]);
107+ return isset ($ this ->typeExtensionServices [$ name ]);
77108 }
78109
79110 public function getTypeGuesser ()
@@ -82,11 +113,15 @@ public function getTypeGuesser()
82113 $ this ->guesserLoaded = true ;
83114 $ guessers = array ();
84115
85- foreach ($ this ->guesserServiceIds as $ serviceId ) {
86- $ guessers [] = $ this ->container ->get ($ serviceId );
116+ foreach ($ this ->guesserServices as $ serviceId => $ service ) {
117+ if (null !== $ this ->guesserServiceIds ) {
118+ $ service = $ this ->typeContainer ->get ($ serviceId = $ service );
119+ }
120+
121+ $ guessers [] = $ service ;
87122 }
88123
89- if (count ( $ guessers) > 0 ) {
124+ if ($ guessers ) {
90125 $ this ->guesser = new FormTypeGuesserChain ($ guessers );
91126 }
92127 }
0 commit comments