<?php
namespace App\EventSubscriber;
use App\Entity\Rubrique;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RubriqueSubscriber implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['generateFilePersist'],
BeforeEntityUpdatedEvent::class => ['generateFileUpdate'],
AfterEntityDeletedEvent::class =>['generateFileDelete']
];
}
public function generateFilePersist(AfterEntityPersistedEvent $event)
{
try {
// $entityManager = $this->getDoctrine()->getManager();
$rubriques=$this->entityManager->getRepository(Rubrique::class)->findAll();
$requestString='var rubriques= {';
$numItems = count($rubriques);
$i = 0;
foreach ($rubriques as $rb){
$requestString = $requestString.'"'. $rb->getSlugNom() . '":"' .$rb->getRubrique().'",';
if(++$i === $numItems) {
$requestString = $requestString .'}';
}
}
$routPath=dirname(__DIR__, 2) ;
// $routPath1=$routPath . '/public\autocomplete\rubriques.js';
// $routPath2=$routPath . '\public\autocomplete\autocomplete_rubriques.js';
$routPath1=$routPath . '/public/autocomplete/rubriques.js';
$routPath2=$routPath . '/public/autocomplete/autocomplete_rubriques.js';
// dd(dirname(__FILE__),dirname(__DIR__),realpath(__DIR__ . '/../../../public/autocomplete/autocomplete_rubriques.js'),dirname(__DIR__, 3),$routPath1);
//pour eviter les conflict
file_put_contents($routPath1 , $requestString);
if(file_exists( $routPath2)){
unlink($routPath2 ); rename($routPath1 ,$routPath2);
}else{
rename($routPath1 ,$routPath2);
}
}catch (\Doctrine\ORM\ORMException $e){
return $e;
}
}
public function generateFileUpdate(BeforeEntityUpdatedEvent $event)
{
try {
$rubriques=$this->entityManager->getRepository(Rubrique::class)->findAll();
$requestString='var rubriques= {';
$numItems = count($rubriques);
$i = 0;
foreach ($rubriques as $rb){
$requestString = $requestString.'"'. $rb->getSlugNom() . '":"' .$rb->getRubrique().'",';
if(++$i === $numItems) {
$requestString = $requestString .'}';
}
}
$routPath=dirname(__DIR__, 2) ;
$routPath1=$routPath . '/public/autocomplete/rubriques.js';
$routPath2=$routPath . '/public/autocomplete/autocomplete_rubriques.js';
// dd(dirname(__FILE__),dirname(__DIR__),realpath(__DIR__ . '/../../../public/autocomplete/autocomplete_rubriques.js'),dirname(__DIR__, 3),$routPath1);
//pour eviter les conflict
file_put_contents($routPath1 , $requestString);
if(file_exists( $routPath2)){
unlink($routPath2 ); rename($routPath1 ,$routPath2);
}else{
rename($routPath1 ,$routPath2);
}
}catch (\Doctrine\ORM\ORMException $e){
return $e;
}
}
public function generateFileDelete(AfterEntityDeletedEvent $event)
{
try {
$rubriques=$this->entityManager->getRepository(Rubrique::class)->findAll();
$requestString='var rubriques= {';
$numItems = count($rubriques);
$i = 0;
foreach ($rubriques as $rb){
$requestString = $requestString.'"'. $rb->getSlugNom() . '":"' .$rb->getRubrique().'",';
if(++$i === $numItems) {
$requestString = $requestString .'}';
}
}
$routPath=dirname(__DIR__, 2) ;
$routPath1=$routPath . '/public/autocomplete/rubriques.js';
$routPath2=$routPath . '/public/autocomplete/autocomplete_rubriques.js';
// dd(dirname(__FILE__),dirname(__DIR__),realpath(__DIR__ . '/../../../public/autocomplete/autocomplete_rubriques.js'),dirname(__DIR__, 3),$routPath1);
//pour eviter les conflict
file_put_contents($routPath1 , $requestString);
if(file_exists( $routPath2)){
unlink($routPath2 ); rename($routPath1 ,$routPath2);
}else{
rename($routPath1 ,$routPath2);
}
}catch (\Doctrine\ORM\ORMException $e){
return $e;
}
}
}