src/Flexy/FrontBundle/EventSubscriber/EventsApiSubscriber.php line 50

  1. <?php
  2. namespace App\Flexy\FrontBundle\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Book;
  5. use App\Entity\Settings;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\ViewEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Mime\Email;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use App\Entity\User;
  13. use App\Flexy\FrontBundle\Entity\Page;
  14. use App\Flexy\FrontBundle\Entity\Section;
  15. use App\Flexy\ShopBundle\Entity\Customer\Customer;
  16. use App\Flexy\ShopBundle\Entity\Order\Order;
  17. use App\Flexy\ShopBundle\Entity\Shipping\ShippingMethod;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Doctrine\ORM\Events;
  20. use Doctrine\Persistence\Event\LifecycleEventArgs;
  21. use Exception;
  22. use Doctrine\Persistence\ManagerRegistry;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. use Symfony\Component\Mercure\HubInterface;
  25. use Symfony\Component\Mercure\Update;
  26. use Symfony\Component\Security\Core\Security;
  27. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  28. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  29. use App\Security\AppAuthenticator;
  30. final class EventsApiSubscriber implements EventSubscriberInterface
  31. {
  32.     
  33.     public function __construct(private readonly Security $security, protected RequestStack $requestStack, private readonly HubInterface $hub, private readonly UserPasswordHasherInterface $passwordEncoder, private readonly ManagerRegistry $doctrine, private readonly UserAuthenticatorInterface $userAuthenticator, private readonly AppAuthenticator $appAuthenticator)
  34.     {
  35.         
  36.     }
  37.     
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             KernelEvents::VIEW => ['postWrite'EventPriorities::POST_WRITE],
  42.         ];
  43.     }
  44.     public function postWrite(ViewEvent $event): void
  45.     {
  46.         $entityInstance $event->getControllerResult();
  47.         $method $event->getRequest()->getMethod();
  48.         
  49.         if (($entityInstance instanceof Section or $entityInstance instanceof Page) and Request::METHOD_PUT === $method) {
  50.                             
  51.             
  52.                             
  53.                             $jsonWithBase64 $entityInstance->getJsonSimplifiedContent();
  54.                             
  55.                             $htmlWithBase64 $jsonWithBase64[0]["html"];
  56.                             $cssWithBase64 $jsonWithBase64[0]["css"];
  57.                             $settings $this->doctrine->getManager()->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
  58.                             
  59.                            
  60.                             
  61.                 /*
  62.                             $resultMatchHtml = preg_replace_callback(
  63.                                 '#(<img\s(?>(?!src=)[^>])*?src=")data:image/(gif|png|jpeg);base64,([\w=+/]++)("[^>]*>)#',
  64.                                     function ($match) {
  65.                                         [, $img, $type, $base64, $end] = $match;
  66.                                         $md5 = md5($base64);   // generate a new temporary filename
  67.                                             $fn = "uploads/pages/$md5.$type";
  68.                                             return $this->base64ToImage($base64,$fn);
  69.                                         return "$img$fn$end";  // new <img> tag
  70.                                     },
  71.                                 (string) $htmlWithBase64);
  72. */
  73.                                 $resultMatchHtml preg_replace_callback(
  74.                                     '/(data:image\/(gif|png|jpeg|jpg);base64,[^"]+)/',
  75.                                         function ($match) use ($settings) {
  76.                                            
  77.                                             
  78.                                             [, $base64$type] = $match;
  79.                                             $md5 md5($base64);   // generate a new temporary filename
  80.                                             $fn "uploads/".strtolower($settings->getAssetFolderName())."/pages/$md5.$type";
  81.                                             return $settings->getRootUrl().$this->base64ToImage($base64,$fn);
  82.                                             
  83.                                         },
  84.                                         (string) $htmlWithBase64);
  85.                                     
  86.                                 
  87.                                 // /src="(data:image\/[^;]+;base64[^"]+)"/
  88.                                 ///(data:image\/[^;]+;base64[^"]+)/
  89.                                 $resultMatchCss preg_replace_callback(
  90.                                     '/\'(data:image\/(gif|png|jpeg|jpg);base64,[^"]+)\'/',
  91.                                         function ($match)  use ($settings) {
  92.                                             
  93.                                             [, $base64$type] = $match;
  94.                                             $md5 md5($base64);   // generate a new temporary filename
  95.                                             $fn "uploads/".strtolower($settings->getAssetFolderName())."/pages/$md5.$type";
  96.                                             return $settings->getRootUrl().$this->base64ToImage($base64,$fn);
  97.                                             
  98.                                         },
  99.                                         (string) $cssWithBase64);
  100.                          
  101.                                 
  102.                                
  103.                                 //background-image:url('
  104.                             
  105.                                 $jsonWithBase64[0]["html"]=$resultMatchHtml;
  106.                                 $jsonWithBase64[0]["css"]=$resultMatchCss;
  107.                                 //dd($jsonWithBase64);
  108.                                 $entityInstance->setJsonSimplifiedContent($jsonWithBase64);
  109.                                 
  110.         
  111.                             $this->doctrine->getManager()->persist($entityInstance);
  112.                             $this->doctrine->getManager()->flush();
  113.                             
  114.                             
  115.                             
  116.                         }
  117.                     
  118.                 
  119.                 
  120.             }
  121.             public function base64ToImage($base64_string$output_file) {
  122.                 $file fopen($output_file"wb");
  123.             
  124.                 $data explode(',', (string) $base64_string);
  125.                 //dd($data);
  126.             
  127.                 fwrite($filebase64_decode($data[1]));
  128.                 fclose($file);
  129.             
  130.                 return "/".$output_file;
  131.             }
  132.             
  133.             
  134.         }
  135.