src/Flexy/FrontBundle/EventSubscriber/EventsApiSubscriber.php line 50
<?php
namespace App\Flexy\FrontBundle\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Entity\Book;
use App\Entity\Settings;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use App\Entity\User;
use App\Flexy\FrontBundle\Entity\Page;
use App\Flexy\FrontBundle\Entity\Section;
use App\Flexy\ShopBundle\Entity\Customer\Customer;
use App\Flexy\ShopBundle\Entity\Order\Order;
use App\Flexy\ShopBundle\Entity\Shipping\ShippingMethod;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Exception;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use App\Security\AppAuthenticator;
final class EventsApiSubscriber implements EventSubscriberInterface
{
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)
{
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['postWrite', EventPriorities::POST_WRITE],
];
}
public function postWrite(ViewEvent $event): void
{
$entityInstance = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (($entityInstance instanceof Section or $entityInstance instanceof Page) and Request::METHOD_PUT === $method) {
$jsonWithBase64 = $entityInstance->getJsonSimplifiedContent();
$htmlWithBase64 = $jsonWithBase64[0]["html"];
$cssWithBase64 = $jsonWithBase64[0]["css"];
/*
$resultMatchHtml = preg_replace_callback(
'#(<img\s(?>(?!src=)[^>])*?src=")data:image/(gif|png|jpeg);base64,([\w=+/]++)("[^>]*>)#',
function ($match) {
[, $img, $type, $base64, $end] = $match;
$md5 = md5($base64); // generate a new temporary filename
$fn = "uploads/pages/$md5.$type";
return $this->base64ToImage($base64,$fn);
return "$img$fn$end"; // new <img> tag
},
(string) $htmlWithBase64);
*/
$resultMatchHtml = preg_replace_callback(
'/(data:image\/(gif|png|jpeg|jpg);base64,[^"]+)/',
function ($match) {
$settings = $this->doctrine->getManager()->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
[, $base64, $type] = $match;
$md5 = md5($base64); // generate a new temporary filename
$fn = "uploads/".strtolower($settings->getAssetFolderName())."/pages/$md5.$type";
return $this->base64ToImage($base64,$fn);
},
(string) $htmlWithBase64);
// /src="(data:image\/[^;]+;base64[^"]+)"/
///(data:image\/[^;]+;base64[^"]+)/
$resultMatchCss = preg_replace_callback(
'/\'(data:image\/(gif|png|jpeg|jpg);base64,[^"]+)\'/',
function ($match) {
$settings = $this->doctrine->getManager()->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
[, $base64, $type] = $match;
$md5 = md5($base64); // generate a new temporary filename
$fn = "uploads/".strtolower($settings->getAssetFolderName())."/pages/$md5.$type";
return $this->base64ToImage($base64,$fn);
},
(string) $cssWithBase64);
//background-image:url('
$jsonWithBase64[0]["html"]=$resultMatchHtml;
$jsonWithBase64[0]["css"]=$resultMatchCss;
//dd($jsonWithBase64);
$entityInstance->setJsonSimplifiedContent($jsonWithBase64);
$this->doctrine->getManager()->persist($entityInstance);
$this->doctrine->getManager()->flush();
}
}
public function base64ToImage($base64_string, $output_file) {
$file = fopen($output_file, "wb");
$data = explode(',', (string) $base64_string);
//dd($data);
fwrite($file, base64_decode($data[1]));
fclose($file);
return "/".$output_file;
}
}