<?php
namespace Cofondateur\SocleTechniqueBundle\EventSubscriber;
use App\Entity\SEOPage;
use App\Entity\SiteSettings;
use Cofondateur\SocleTechniqueBundle\Service\BreadcrumbService;
use Cofondateur\SocleTechniqueBundle\Traits\UpdatableInterface;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class TwigEventSubscriber implements EventSubscriberInterface
{
/** @var EntityManagerInterface */
private $em;
/** @var Environment */
private $twig;
public function __construct(EntityManagerInterface $em, Environment $twig, BreadcrumbService $breadcrumbService)
{
$this->em = $em;
$this->twig = $twig;
$this->breadcrumbService = $breadcrumbService;
}
public static function getSubscribedEvents()
{
return array(
RequestEvent::class => ["onRequest"]
);
}
public function onRequest(RequestEvent $event)
{
// Add current SEOPage
$repo = $this->em->getRepository(SEOPage::class);
$route = $event->getRequest()->get('_route');
$seoPage = $repo->findOneBy(array('route' => $route));
$this->twig->addGlobal('SEOPage', $seoPage);
// Add site settings
$repo = $this->em->getRepository(SiteSettings::class);
$settings = $repo->findAll();
if (count($settings) > 0) {
$this->twig->addGlobal('siteSettings', $settings[0]);
} else {
if ($route && !str_starts_with($route, 'admin') && !str_starts_with($route, 'login')) {
throw new \Exception('Les paramètres du site n\'ont pas été initialisés. Veuillez les initialiser dans le menu admin ['.$route.']');
}
}
// Services
$this->breadcrumbService->setCurrentRequest($event->getRequest());
}
}