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.                             $htmlWithBase64 $jsonWithBase64[0]["html"];
  55.                             $cssWithBase64 $jsonWithBase64[0]["css"];
  56.                             
  57.                            
  58.                             
  59.                 /*
  60.                             $resultMatchHtml = preg_replace_callback(
  61.                                 '#(<img\s(?>(?!src=)[^>])*?src=")data:image/(gif|png|jpeg);base64,([\w=+/]++)("[^>]*>)#',
  62.                                     function ($match) {
  63.                                         [, $img, $type, $base64, $end] = $match;
  64.                                         $md5 = md5($base64);   // generate a new temporary filename
  65.                                             $fn = "uploads/pages/$md5.$type";
  66.                                             return $this->base64ToImage($base64,$fn);
  67.                                         return "$img$fn$end";  // new <img> tag
  68.                                     },
  69.                                 (string) $htmlWithBase64);
  70. */
  71.                                 $resultMatchHtml preg_replace_callback(
  72.                                     '/(data:image\/(gif|png|jpeg|jpg);base64,[^"]+)/',
  73.                                         function ($match) {
  74.                                             $settings $this->doctrine->getManager()->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
  75.                                             
  76.                                             [, $base64$type] = $match;
  77.                                             $md5 md5($base64);   // generate a new temporary filename
  78.                                             $fn "uploads/".strtolower($settings->getAssetFolderName())."/pages/$md5.$type";
  79.                                             return $this->base64ToImage($base64,$fn);
  80.                                             
  81.                                         },
  82.                                     (string) $htmlWithBase64);
  83.                                     
  84.                                 
  85.                                 // /src="(data:image\/[^;]+;base64[^"]+)"/
  86.                                 ///(data:image\/[^;]+;base64[^"]+)/
  87.                                 $resultMatchCss preg_replace_callback(
  88.                                     '/\'(data:image\/(gif|png|jpeg|jpg);base64,[^"]+)\'/',
  89.                                         function ($match) {
  90.                                             $settings $this->doctrine->getManager()->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
  91.                                             [, $base64$type] = $match;
  92.                                             $md5 md5($base64);   // generate a new temporary filename
  93.                                             $fn "uploads/".strtolower($settings->getAssetFolderName())."/pages/$md5.$type";
  94.                                             return $this->base64ToImage($base64,$fn);
  95.                                             
  96.                                         },
  97.                                     (string) $cssWithBase64);
  98.                          
  99.                                 
  100.                                
  101.                                 //background-image:url('
  102.                             
  103.                                 $jsonWithBase64[0]["html"]=$resultMatchHtml;
  104.                                 $jsonWithBase64[0]["css"]=$resultMatchCss;
  105.                                 //dd($jsonWithBase64);
  106.                                 $entityInstance->setJsonSimplifiedContent($jsonWithBase64);
  107.                                 
  108.         
  109.                             $this->doctrine->getManager()->persist($entityInstance);
  110.                             $this->doctrine->getManager()->flush();
  111.                             
  112.                             
  113.                             
  114.                         }
  115.                     
  116.                 
  117.                 
  118.             }
  119.             public function base64ToImage($base64_string$output_file) {
  120.                 $file fopen($output_file"wb");
  121.             
  122.                 $data explode(',', (string) $base64_string);
  123.                 //dd($data);
  124.             
  125.                 fwrite($filebase64_decode($data[1]));
  126.                 fclose($file);
  127.             
  128.                 return "/".$output_file;
  129.             }
  130.             
  131.             
  132.         }
  133.