src/EventSubscriber/RubriqueSubscriber.php line 109

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Rubrique;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class RubriqueSubscriber implements EventSubscriberInterface
  11. {
  12.     private $entityManager;
  13.     public function __construct(EntityManagerInterface $entityManager)
  14.     {
  15.         $this->entityManager $entityManager;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             AfterEntityPersistedEvent::class => ['generateFilePersist'],
  21.             BeforeEntityUpdatedEvent::class => ['generateFileUpdate'],
  22.             AfterEntityDeletedEvent::class =>['generateFileDelete']
  23.         ];
  24.     }
  25.     public function generateFilePersist(AfterEntityPersistedEvent $event)
  26.     {
  27.         try {
  28.            // $entityManager = $this->getDoctrine()->getManager();
  29.             $rubriques=$this->entityManager->getRepository(Rubrique::class)->findAll();
  30.             $requestString='var rubriques= {';
  31.             $numItems count($rubriques);
  32.             $i 0;
  33.             foreach ($rubriques as $rb){
  34.                 $requestString $requestString.'"'$rb->getSlugNom() . '":"' .$rb->getRubrique().'",';
  35.                 if(++$i === $numItems) {
  36.                     $requestString =  $requestString .'}';
  37.                 }
  38.             }
  39.             $routPath=dirname(__DIR__2) ;
  40. //            $routPath1=$routPath . '/public\autocomplete\rubriques.js';
  41. //            $routPath2=$routPath . '\public\autocomplete\autocomplete_rubriques.js';
  42.             $routPath1=$routPath '/public/autocomplete/rubriques.js';
  43.             $routPath2=$routPath '/public/autocomplete/autocomplete_rubriques.js';
  44.             // dd(dirname(__FILE__),dirname(__DIR__),realpath(__DIR__ . '/../../../public/autocomplete/autocomplete_rubriques.js'),dirname(__DIR__, 3),$routPath1);
  45.             //pour eviter les conflict
  46.             file_put_contents($routPath1 $requestString);
  47.             if(file_exists$routPath2)){
  48.                 unlink($routPath2 ); rename($routPath1 ,$routPath2);
  49.             }else{
  50.                 rename($routPath1 ,$routPath2);
  51.             }
  52.         }catch (\Doctrine\ORM\ORMException $e){
  53.             return $e;
  54.         }
  55.      }
  56.     public function generateFileUpdate(BeforeEntityUpdatedEvent $event)
  57.     {
  58.         try {
  59.             $rubriques=$this->entityManager->getRepository(Rubrique::class)->findAll();
  60.             $requestString='var rubriques= {';
  61.             $numItems count($rubriques);
  62.             $i 0;
  63.             foreach ($rubriques as $rb){
  64.                 $requestString $requestString.'"'$rb->getSlugNom() . '":"' .$rb->getRubrique().'",';
  65.                 if(++$i === $numItems) {
  66.                     $requestString =  $requestString .'}';
  67.                 }
  68.             }
  69.             $routPath=dirname(__DIR__2) ;
  70.             $routPath1=$routPath '/public/autocomplete/rubriques.js';
  71.             $routPath2=$routPath '/public/autocomplete/autocomplete_rubriques.js';
  72.             // dd(dirname(__FILE__),dirname(__DIR__),realpath(__DIR__ . '/../../../public/autocomplete/autocomplete_rubriques.js'),dirname(__DIR__, 3),$routPath1);
  73.             //pour eviter les conflict
  74.             file_put_contents($routPath1 $requestString);
  75.             if(file_exists$routPath2)){
  76.                 unlink($routPath2 ); rename($routPath1 ,$routPath2);
  77.             }else{
  78.                 rename($routPath1 ,$routPath2);
  79.             }
  80.         }catch (\Doctrine\ORM\ORMException $e){
  81.             return $e;
  82.         }
  83.     }
  84.     public function generateFileDelete(AfterEntityDeletedEvent $event)
  85.     {
  86.         try {
  87.             $rubriques=$this->entityManager->getRepository(Rubrique::class)->findAll();
  88.             $requestString='var rubriques= {';
  89.             $numItems count($rubriques);
  90.             $i 0;
  91.             foreach ($rubriques as $rb){
  92.                 $requestString $requestString.'"'$rb->getSlugNom() . '":"' .$rb->getRubrique().'",';
  93.                 if(++$i === $numItems) {
  94.                     $requestString =  $requestString .'}';
  95.                 }
  96.             }
  97.             $routPath=dirname(__DIR__2) ;
  98.             $routPath1=$routPath '/public/autocomplete/rubriques.js';
  99.             $routPath2=$routPath '/public/autocomplete/autocomplete_rubriques.js';
  100.             // dd(dirname(__FILE__),dirname(__DIR__),realpath(__DIR__ . '/../../../public/autocomplete/autocomplete_rubriques.js'),dirname(__DIR__, 3),$routPath1);
  101.             //pour eviter les conflict
  102.             file_put_contents($routPath1 $requestString);
  103.             if(file_exists$routPath2)){
  104.                 unlink($routPath2 ); rename($routPath1 ,$routPath2);
  105.             }else{
  106.                 rename($routPath1 ,$routPath2);
  107.             }
  108.         }catch (\Doctrine\ORM\ORMException $e){
  109.             return $e;
  110.         }
  111.     }
  112. }