浏览代码

injection de EntityManagerInterface

François Drouhard 5 年之前
父节点
当前提交
65c148c8c5

+ 5 - 8
src/Controller/GenreController.php

@@ -4,6 +4,7 @@ namespace App\Controller;
 
 use App\Entity\Genre;
 use App\Form\GenreType;
+use Doctrine\ORM\EntityManagerInterface;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\Routing\Annotation\Route;
@@ -14,8 +15,7 @@ class GenreController extends AbstractController
     /**
      * @Route("/genre", name="genre_liste")
      */
-    public function indexAction() {
-        $em = $this->getDoctrine()->getManager();
+    public function indexAction(EntityManagerInterface $em) {
         $repo = $em->getRepository('App:Genre');
         $genres = $repo->findAll();
 
@@ -28,7 +28,7 @@ class GenreController extends AbstractController
 	/**
 	 * @Route("/genre/ajouter", name="genre_ajouter")
 	 */
-	public function ajouterAction(Request $request)
+	public function ajouterAction(Request $request, EntityManagerInterface $em)
 	{
 		$genre = new Genre();
 		$form = $this->createForm(GenreType::class, $genre);
@@ -36,7 +36,6 @@ class GenreController extends AbstractController
 		$form->handleRequest($request);
 		if ($form->isSubmitted() && $form->isValid())
 		{
-			$em = $this->getDoctrine()->getManager();
 			$em->persist($genre);
 			$em->flush();
 			$this->addFlash('success', 'Le genre a été ajouté');
@@ -51,11 +50,10 @@ class GenreController extends AbstractController
 	/**
 	 * @Route("/genre/modifier/{id}", name="genre_modifier")
 	 */
-	public function modifierAction(Request $request, Genre $genre)
+	public function modifierAction(Request $request, Genre $genre, EntityManagerInterface $em)
 	{
 		$form = $this->createForm(GenreType::class, $genre);
 		$form->handleRequest($request);
-        $em = $this->getDoctrine()->getManager();
         $films = $em->getRepository('App:Film')->findFilmWithGenre(array($genre->getName()));
 
 		if ($form->isSubmitted() && $form->isValid())
@@ -75,13 +73,12 @@ class GenreController extends AbstractController
 	/**
 	 * @Route("/genre/supprimer/{id}", name="genre_supprimer")
 	 */
-	public function supprimerAction(Request $request, Genre $genre)
+	public function supprimerAction(Request $request, Genre $genre, EntityManagerInterface $em)
     {
         $form = $this->get('form.factory')->create();
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $em = $this->getDoctrine()->getManager();
             $films = $em->getRepository('App:Film')->findFilmWithGenre(array($genre->getName()));
             foreach ($films as $film)
             {

+ 2 - 3
src/Controller/ProfilController.php

@@ -2,7 +2,7 @@
 
 namespace App\Controller;
 
-
+use Doctrine\ORM\EntityManagerInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Routing\Annotation\Route;
@@ -12,14 +12,13 @@ class ProfilController extends AbstractController
     /**
      * @Route("/profil/", name="user_profil")
      */
-    public function monProfilAction(Request $request)
+    public function monProfilAction(Request $request, EntityManagerInterface $em)
     {
         $user = $this->getUser();
         $form = $this->createForm('App\Form\UserEditProfilType', $user);
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $em = $this->getDoctrine()->getManager();
             $user = $form->getData();
             $em->flush();
             $this->addFlash('success', "Votre profil a été modifié");

+ 5 - 8
src/Controller/RealisateurController.php

@@ -4,6 +4,7 @@ namespace App\Controller;
 
 use App\Entity\Realisateur;
 use App\Form\RealisateurType;
+use Doctrine\ORM\EntityManagerInterface;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\Routing\Annotation\Route;
@@ -14,8 +15,7 @@ class RealisateurController extends AbstractController
     /**
      * @Route("/real/", name="realisateur_liste")
      */
-    public function indexAction() {
-        $em = $this->getDoctrine()->getManager();
+    public function indexAction(EntityManagerInterface $em) {
         $repo = $em->getRepository('App:Realisateur');
         $realisateurs = $repo->findAll();
 
@@ -27,7 +27,7 @@ class RealisateurController extends AbstractController
 	/**
 	 * @Route("/real/ajouter", name="realisateur_ajouter")
 	 */
-	public function ajouterAction(Request $request)
+	public function ajouterAction(Request $request, EntityManagerInterface $em)
 	{
 		$realisateur = new Realisateur();
 		$form = $this->createForm(RealisateurType::class, $realisateur);
@@ -35,7 +35,6 @@ class RealisateurController extends AbstractController
 		$form->handleRequest($request);
 		if ($form->isSubmitted() && $form->isValid())
 		{
-			$em = $this->getDoctrine()->getManager();
 			$em->persist($realisateur);
 			$em->flush();
 			$this->addFlash('success', 'Le réalisateur a été ajouté');
@@ -51,14 +50,13 @@ class RealisateurController extends AbstractController
 	/**
 	 * @Route("/real/modifier/{id}", name="realisateur_modifier")
 	 */
-	public function modifierAction(Request $request, Realisateur $realisateur)
+	public function modifierAction(Request $request, Realisateur $realisateur, EntityManagerInterface $em)
 	{
 		$form = $this->createForm(RealisateurType::class, $realisateur);
 		$form->handleRequest($request);
 
 		if ($form->isSubmitted() && $form->isValid())
 		{
-            $em = $this->getDoctrine()->getManager();
 			$em->flush();
 			$this->addFlash('success', 'Le réalisateur a été modifié');
 			return $this->redirectToRoute('realisateur_liste');
@@ -75,13 +73,12 @@ class RealisateurController extends AbstractController
 	 * @Route("/real/supprimer/{id}", name="realisateur_supprimer")
      * @IsGranted("ROLE_ADMIN")
 	 */
-	public function supprimerAction(Request $request, Realisateur $realisateur)
+	public function supprimerAction(Request $request, Realisateur $realisateur, EntityManagerInterface $em)
     {
         $form = $this->get('form.factory')->create();
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $em = $this->getDoctrine()->getManager();
             foreach ($realisateur->getFilms() as $film)
             {
                 $film->removeRealisateur($realisateur);

+ 7 - 12
src/Controller/SecurityController.php

@@ -14,6 +14,7 @@ use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Routing\Annotation\Route;
 use App\Form\UserType;
 use App\Entity\User;
+use Doctrine\ORM\EntityManagerInterface;
 use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
@@ -104,12 +105,11 @@ class SecurityController extends AbstractController
     /**
      * @Route("/admin/deluser/{id}", name="admin_deluser")
      */
-    public function delUserAction (Request $request, User $user)
+    public function delUserAction (Request $request, User $user, EntityManagerInterface $em)
     {
         $form = $this->get('form.factory')->create();
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid()) {
-            $em = $this->getDoctrine()->getManager();
             $em->remove($user);
             $em->flush();
             $this->addFlash("success", "L'utilisateur a bien été supprimé");
@@ -124,7 +124,7 @@ class SecurityController extends AbstractController
     /**
      * @Route("/register", name="security_register")
      */
-    public function registerAction(Request $request, UserPasswordEncoderInterface $encoder, UserManager $userManager, Mail $mail)
+    public function registerAction(Request $request, UserPasswordEncoderInterface $encoder, UserManager $userManager, Mail $mail, EntityManagerInterface $em)
     {
         $user = new User();
         $user->setActivated(false);
@@ -136,7 +136,6 @@ class SecurityController extends AbstractController
             $encoded = $encoder->encodePassword($user, $user->getPassword());
             $user->setPassword($encoded);
             $userManager->generateToken($user);
-            $em = $this->getDoctrine()->getManager();
             $em->persist($user);
             $em->flush();
 
@@ -156,7 +155,7 @@ class SecurityController extends AbstractController
     /**
      * @Route("/motdepasseoublie", name="security_envoyertoken")
      */
-    public function recupMdpAction(Request $request, UserManager $userManager, Mail $mail)
+    public function recupMdpAction(Request $request, UserManager $userManager, Mail $mail, EntityManagerInterface $em)
     {
         $form = $this->createFormBuilder()
             ->add('email', EmailType::class)
@@ -165,7 +164,6 @@ class SecurityController extends AbstractController
 
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid()) {
-            $em = $this->getDoctrine()->getManager();
             $data = $form->getData();
             $user = $em->getRepository('App:User')->findOneBy(array('mail'=>$data['email']));
             if ($user != null)
@@ -190,7 +188,7 @@ class SecurityController extends AbstractController
     /**
      * @Route("/resetpassword/token={token}", name="security_resetpassword")
      */
-    public function resetPasswordAction(Request $request, UserPasswordEncoderInterface $encoder, UserManager $userManager, User $user = null)
+    public function resetPasswordAction(Request $request, UserPasswordEncoderInterface $encoder, UserManager $userManager, EntityManagerInterface $em, User $user = null)
     {
         if ($user != null) {
             if ($user->isValidToken()) {
@@ -200,7 +198,6 @@ class SecurityController extends AbstractController
                     $encoded = $encoder->encodePassword($user, $user->getPassword());
                     $user->setPassword($encoded);
                     $userManager->generateToken($user);
-                    $em = $this->getDoctrine()->getManager();
                     $em->flush();
                     $this->addFlash('success', 'Mot de passe changé, veuillez vous connecter');
                     return $this->redirectToRoute('videotheque_liste');
@@ -219,9 +216,8 @@ class SecurityController extends AbstractController
      * Ne pas utiliser
      * @Route("/admin/resettokens", name="admin_resettokens")
      */
-    public function resetTokensAction(UserManager $userManager)
+    public function resetTokensAction(UserManager $userManager, EntityManagerInterface $em)
     {
-        $em = $this->getDoctrine()->getManager();
         $users = $em->getRepository('App:User')->findAll();
         foreach ($users as $user)
         {
@@ -234,14 +230,13 @@ class SecurityController extends AbstractController
     /**
      * @Route("/activate/token={token}", name="security_activate")
      */
-    public function activateAction(Request $request, User $user = null)
+    public function activateAction(Request $request, EntityManagerInterface $em, User $user = null)
     {
         if ($user != null)
         {
             if ($user->isValidToken())
             {
                 $user->setActivated(true);
-                $em = $this->getDoctrine()->getManager();
                 $em->flush();
                 $this->addFlash('success', "Votre compte est activé");
                 return $this->redirectToRoute('app_login');

+ 12 - 23
src/Controller/VideothequeController.php

@@ -13,16 +13,16 @@ use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Component\HttpFoundation\Request;
 use App\Entity\Film;
 use App\Form\FilmType;
+use Doctrine\ORM\EntityManagerInterface;
 
 class VideothequeController extends AbstractController
 {
 	/**
 	 * @Route("/", name="videotheque_liste")
 	 */
-	public function listeAction(Request $request)
+	public function listeAction(Request $request, EntityManagerInterface $em)
 	{
         $note = (int) $request->query->get('note');
-		$em = $this->getDoctrine()->getManager();
 		$repo = $em->getRepository('App:Film');
         $listeFilms = $repo->findTous($note);
 
@@ -36,9 +36,8 @@ class VideothequeController extends AbstractController
     /**
      * @Route("/liste-by/{id}", name="videotheque_listepargenre")
      */
-	public function  listeParGenreAction(\App\Entity\Genre $genre)
+	public function  listeParGenreAction(\App\Entity\Genre $genre, EntityManagerInterface $em)
     {
-        $em = $this->getDoctrine()->getManager();
         $films = $em->getRepository('App:Film')->findFilmWithGenre(array($genre->getName()));
 
         return $this->render('videotheque/liste.html.twig', array(
@@ -50,9 +49,8 @@ class VideothequeController extends AbstractController
     /**
      * @Route("/liste-by_real/{id}", name="videotheque_listeparreal")
      */
-    public function  listeParRealisateurAction(Realisateur $realisateur)
+    public function  listeParRealisateurAction(Realisateur $realisateur, EntityManagerInterface $em)
     {
-        $em = $this->getDoctrine()->getManager();
         $films = $em->getRepository('App:Film')->findFilmWithReal(array($realisateur->getNomComplet()));
 
         return $this->render('videotheque/liste.html.twig', array(
@@ -64,12 +62,11 @@ class VideothequeController extends AbstractController
 	/**
 	 * @Route("/ajouter", name="videotheque_ajouter")
 	 */
-	public function ajouterAction(Request $request, UniciteCollections $uniciteCollections)
+	public function ajouterAction(Request $request, UniciteCollections $uniciteCollections, EntityManagerInterface $em)
 	{
 		$film = new Film;
 		$film->setAuthered($this->getUser());
 		$form = $this->createForm(FilmType::class, $film);
-        $em = $this->getDoctrine()->getManager();
 
 		$form->handleRequest($request);
 		if ($form->isSubmitted() && $form->isValid())
@@ -89,11 +86,10 @@ class VideothequeController extends AbstractController
 	/**
 	 * @Route("/modifier/{id}", name="videotheque_modifier")
 	 */
-	public function modifierAction(Request $request, Film $film, UniciteCollections $uniciteCollections)
+	public function modifierAction(Request $request, Film $film, UniciteCollections $uniciteCollections, EntityManagerInterface $em)
 	{
 		$form = $this->createForm(FilmType::class, $film);
 		$form->handleRequest($request);
-        $em = $this->getDoctrine()->getManager();
 		if ($form->isSubmitted() && $form->isValid())
 		{
             $film = $uniciteCollections->assureUniciteCollections($film);
@@ -102,7 +98,6 @@ class VideothequeController extends AbstractController
 			return $this->redirectToRoute('videotheque_voirfilm',array('id'=>$film->getId()));
 		}
 
-
 		return $this->render('videotheque/modifier.html.twig', array(
 			'form'	=> $form->createView(),
 		));
@@ -112,13 +107,12 @@ class VideothequeController extends AbstractController
 	 * @Route("/supprimer/{id}", name="videotheque_supprimer")
      * @IsGranted("ROLE_ADMIN")
 	 */
-	public function supprimerAction(Request $request, Film $film)
+	public function supprimerAction(Request $request, Film $film, EntityManagerInterface $em)
 	{
         $form = $this->get('form.factory')->create();
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $em = $this->getDoctrine()->getManager();
             $commentaire = $em->getRepository('App:Commentaire')->findOneBy(array('film'=>$film));
             if ($commentaire != null)
             {
@@ -139,9 +133,8 @@ class VideothequeController extends AbstractController
 	/**
      * @Route("/fichefilm/{id}", name="videotheque_voirfilm")
      */
-	public function voirFilmAction(Request $request, \App\Entity\Film $film, Security $security)
+	public function voirFilmAction(Request $request, \App\Entity\Film $film, Security $security, EntityManagerInterface $em)
     {
-        $em = $this->getDoctrine()->getManager();
         $repoComment = $em->getRepository('App:Commentaire');
         if ($security->isGranted('IS_AUTHENTICATED_REMEMBERED'))
         {
@@ -183,11 +176,9 @@ class VideothequeController extends AbstractController
     /**
      * @Route("/ajax_req_realisateurs", name="videotheque_ajax_realisateurs")
      */
-    public function ajaxRealisateurs(Request $request)
+    public function ajaxRealisateurs(Request $request, EntityManagerInterface $em)
     {
-        $realisateurs = $this
-            ->getDoctrine()
-            ->getManager()
+        $realisateurs = $em
             ->getRepository('App:Realisateur')
             ->findNomsComplets();
         $liste = array();
@@ -202,11 +193,9 @@ class VideothequeController extends AbstractController
     /**
      * @Route("/ajax_req_genres", name="videotheque_ajax_genres")
      */
-    public function ajaxGenres(Request $request)
+    public function ajaxGenres(Request $request, EntityManagerInterface $em)
     {
-        $genres = $this
-            ->getDoctrine()
-            ->getManager()
+        $genres = $em
             ->getRepository('App:Genre')
             ->findGenres();
         $liste = array();

+ 5 - 8
src/Controller/VideothequePersonnelleController.php

@@ -2,6 +2,7 @@
 
 namespace App\Controller;
 
+use Doctrine\ORM\EntityManagerInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\Routing\Annotation\Route;
@@ -12,11 +13,9 @@ class VideothequePersonnelleController extends AbstractController
 	/**
      * @Route("/maliste/", name="videothequepersonnelle_maliste")
      */
-	public function maListeAction (Request $request)
+	public function maListeAction (Request $request, EntityManagerInterface $em)
     {
-        $films = $this
-            ->getDoctrine()
-            ->getManager()
+        $films = $em
             ->getRepository('App:Film')
             ->findTousFavoritesByUser($this->getUser());
 
@@ -42,9 +41,8 @@ class VideothequePersonnelleController extends AbstractController
      * @param Request $request
      * @return JsonResponse
      */
-    public function modifierFilmDansListeAction(Request $request)
+    public function modifierFilmDansListeAction(Request $request, EntityManagerInterface $em)
     {
-        $em = $this->getDoctrine()->getManager();
         $repo = $em->getRepository('App:Film');
         $film = $repo->find($request->request->get('id_film'));
         if ($request->isXmlHttpRequest())
@@ -61,9 +59,8 @@ class VideothequePersonnelleController extends AbstractController
      * @param Request $request
      * @return JsonResponse
      */
-    public function modifierFilmVusAction(Request $request)
+    public function modifierFilmVusAction(Request $request, EntityManagerInterface $em)
     {
-        $em = $this->getDoctrine()->getManager();
         $repo = $em->getRepository('App:Film');
         $film = $repo->find($request->request->get('id_film'));
         if ($request->isXmlHttpRequest())