1313
1414use PhpParser \Node ;
1515use Symfony \Bundle \MakerBundle \Util \ClassSourceManipulator ;
16+ use Symfony \Component \HttpKernel \Kernel ;
17+ use Symfony \Component \Security \Core \User \PasswordAuthenticatedUserInterface ;
1618use Symfony \Component \Security \Core \User \UserInterface ;
1719
1820/**
2224 */
2325final class UserClassBuilder
2426{
25- public function addUserInterfaceImplementation (ClassSourceManipulator $ manipulator , UserClassConfiguration $ userClassConfig )
27+ public function addUserInterfaceImplementation (ClassSourceManipulator $ manipulator , UserClassConfiguration $ userClassConfig ): void
2628 {
2729 $ manipulator ->addInterface (UserInterface::class);
2830
2931 $ this ->addGetUsername ($ manipulator , $ userClassConfig );
3032
3133 $ this ->addGetRoles ($ manipulator , $ userClassConfig );
3234
33- $ this ->addGetPassword ($ manipulator , $ userClassConfig );
34-
35- $ this ->addGetSalt ($ manipulator , $ userClassConfig );
35+ $ this ->addPasswordImplementation ($ manipulator , $ userClassConfig );
3636
3737 $ this ->addEraseCredentials ($ manipulator , $ userClassConfig );
3838 }
3939
40+ private function addPasswordImplementation (ClassSourceManipulator $ manipulator , UserClassConfiguration $ userClassConfig ): void
41+ {
42+ if (60000 > Kernel::VERSION_ID ) {
43+ // Add methods required to fulfill the UserInterface contract
44+ $ this ->addGetPassword ($ manipulator , $ userClassConfig );
45+ $ this ->addGetSalt ($ manipulator , $ userClassConfig );
46+
47+ // Symfony >=5.3 uses "@see PasswordAuthenticatedInterface" for getPassword()
48+ if (interface_exists (PasswordAuthenticatedUserInterface::class)) {
49+ $ manipulator ->addUseStatementIfNecessary (PasswordAuthenticatedUserInterface::class);
50+ }
51+
52+ // Future proof the entity for >= Symfony 6 && the entity will check passwords
53+ if ($ userClassConfig ->hasPassword () && interface_exists (PasswordAuthenticatedUserInterface::class)) {
54+ $ manipulator ->addInterface (PasswordAuthenticatedUserInterface::class);
55+ }
56+
57+ return ;
58+ }
59+
60+ // Future proof >= Symfony 6.0
61+ if (!$ userClassConfig ->hasPassword ()) {
62+ return ;
63+ }
64+
65+ $ manipulator ->addInterface (PasswordAuthenticatedUserInterface::class);
66+
67+ $ this ->addGetPassword ($ manipulator , $ userClassConfig );
68+ }
69+
4070 private function addGetUsername (ClassSourceManipulator $ manipulator , UserClassConfiguration $ userClassConfig )
4171 {
4272 if ($ userClassConfig ->isEntity ()) {
@@ -161,16 +191,18 @@ private function addGetRoles(ClassSourceManipulator $manipulator, UserClassConfi
161191
162192 private function addGetPassword (ClassSourceManipulator $ manipulator , UserClassConfiguration $ userClassConfig )
163193 {
194+ $ seeInterface = interface_exists (PasswordAuthenticatedUserInterface::class) ? '@see PasswordAuthenticatedUserInterface ' : '@see UserInterface ' ;
195+
164196 if (!$ userClassConfig ->hasPassword ()) {
165197 // add an empty method only
166198 $ builder = $ manipulator ->createMethodBuilder (
167199 'getPassword ' ,
168200 'string ' ,
169201 true ,
170202 [
171- 'This method is not needed for apps that do not check user passwords. ' ,
203+ 'This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. ' ,
172204 '' ,
173- ' @see UserInterface ' ,
205+ $ seeInterface ,
174206 ]
175207 );
176208
@@ -221,9 +253,8 @@ private function addGetPassword(ClassSourceManipulator $manipulator, UserClassCo
221253 'string ' ,
222254 false ,
223255 [
224- '@see UserInterface ' ,
225- ],
226- true
256+ $ seeInterface ,
257+ ]
227258 );
228259 }
229260
@@ -236,7 +267,7 @@ private function addGetSalt(ClassSourceManipulator $manipulator, UserClassConfig
236267 ];
237268 } else {
238269 $ methodDescription = [
239- 'This method is not needed for apps that do not check user passwords. ' ,
270+ 'This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords. ' ,
240271 ];
241272 }
242273
0 commit comments