@@ -1089,4 +1089,62 @@ public function amLoggedInAs(UserInterface $user, $firewallName = 'main', $firew
10891089 $ cookie = new Cookie ($ session ->getName (), $ session ->getId ());
10901090 $ this ->client ->getCookieJar ()->set ($ cookie );
10911091 }
1092+
1093+ /**
1094+ * Grab a Doctrine entity repository.
1095+ * Works with objects, entities, repositories, and repository interfaces.
1096+ *
1097+ * ```php
1098+ * <?php
1099+ * $I->grabRepository($user);
1100+ * $I->grabRepository(User::class);
1101+ * $I->grabRepository(UserRepository::class);
1102+ * $I->grabRepository(UserRepositoryInterface::class);
1103+ * ```
1104+ *
1105+ * @param object|string $mixed
1106+ * @return \Doctrine\ORM\EntityRepository|null
1107+ */
1108+ public function grabRepository ($ mixed )
1109+ {
1110+ $ entityRepoClass = '\Doctrine\ORM\EntityRepository ' ;
1111+ $ isNotARepo = function () use ($ mixed ) {
1112+ $ this ->fail (
1113+ sprintf ("'%s' is not an entity repository " , $ mixed )
1114+ );
1115+ };
1116+ $ getRepo = function () use ($ mixed , $ entityRepoClass , $ isNotARepo ) {
1117+ if (!$ repo = $ this ->grabService ($ mixed )) return null ;
1118+ if (!$ repo instanceof $ entityRepoClass ) {
1119+ $ isNotARepo ();
1120+ return null ;
1121+ }
1122+ return $ repo ;
1123+ };
1124+
1125+ if (interface_exists ($ mixed )) {
1126+ return $ getRepo ();
1127+ }
1128+
1129+ if (is_object ($ mixed )) {
1130+ $ mixed = get_class ($ mixed );
1131+ }
1132+
1133+ if (!is_string ($ mixed ) || !class_exists ($ mixed ) ) {
1134+ $ isNotARepo ();
1135+ return null ;
1136+ }
1137+
1138+ if (is_subclass_of ($ mixed , $ entityRepoClass )){
1139+ return $ getRepo ();
1140+ }
1141+
1142+ $ em = $ this ->_getEntityManager ();
1143+ if ($ em ->getMetadataFactory ()->isTransient ($ mixed )) {
1144+ $ isNotARepo ();
1145+ return null ;
1146+ }
1147+
1148+ return $ em ->getRepository ($ mixed );
1149+ }
10921150}
0 commit comments