vendor/aufwind/cookiebot-bundle/src/EventSubscriber/CookiebotInsertSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace Aufwind\CookiebotBundle\EventSubscriber;
  4. use Aufwind\CookiebotBundle\CookiebotService;
  5. use Aufwind\CookiebotBundle\Service\ConfigManager;
  6. use Pimcore\Analytics\SiteId\SiteIdProvider;
  7. use Pimcore\Bundle\CoreBundle\EventListener\Traits\EnabledTrait;
  8. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  9. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PreviewRequestTrait;
  10. use Pimcore\Bundle\CoreBundle\EventListener\Traits\ResponseInjectionTrait;
  11. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  12. use Pimcore\Tool;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Templating\EngineInterface;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. class CookiebotInsertSubscriber implements EventSubscriberInterface
  19. {
  20.     use EnabledTrait;
  21.     use ResponseInjectionTrait;
  22.     use PimcoreContextAwareTrait;
  23.     use PreviewRequestTrait;
  24.     public function __construct(
  25.         // protected SiteIdProvider $siteIdProvider,
  26.         // protected EventDispatcherInterface $eventDispatcher,
  27.         // protected EngineInterface $templatingEngine,
  28.         protected ConfigManager $configManager,
  29.         protected CookiebotService $cookiebotService,
  30.     ) {
  31.     }
  32.     public function onKernelResponse(ResponseEvent $event): void
  33.     {
  34.         if (!$this->configManager->isAutoInsert()) {
  35.             return;
  36.         }
  37.         if (!$this->isEnabled()) {
  38.             return;
  39.         }
  40.         $request $event->getRequest();
  41.         if (!$event->isMainRequest()) {
  42.             return;
  43.         }
  44.         // only inject tag manager code on non-admin requests
  45.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  46.             return;
  47.         }
  48.         if (!Tool::useFrontendOutputFilters()) {
  49.             return;
  50.         }
  51.         if ($this->isPreviewRequest($request)) {
  52.             return;
  53.         }
  54.         // $siteId = $this->siteIdProvider->getForRequest($event->getRequest());
  55.         // $siteKey = $siteId->getConfigKey();
  56.         $response $event->getResponse();
  57.         if (!$this->isHtmlResponse($response)) {
  58.             return;
  59.         }
  60.         if (!$this->cookiebotService->isCookieBotEnabled()) {
  61.             return;
  62.         }
  63.         $content $response->getContent();
  64.         $scriptTag $this->cookiebotService->getScriptCode();
  65.         $searchString '<script';
  66.         $replace '';
  67.         if (!empty($scriptTag)) {
  68.             // search for the end <head> tag, and insert the google tag manager code before
  69.             // this method is much faster than using simple_html_dom and uses less memory
  70.             $position stripos($content$searchString);
  71.             if (false === $position) {
  72.                 $searchString '<meta charset="utf-8">';
  73.                 // $append = $searchString;
  74.                 // $prepend = '';
  75.                 $position stripos($content$searchString);
  76.                 $replace $searchString $scriptTag;
  77.             } else {
  78.                 $replace $scriptTag $searchString;
  79.             }
  80.             if (false !== $position) {
  81.                 $content substr_replace($content$replace$positionstrlen($searchString));
  82.             }
  83.         }
  84.         $response->setContent($content);
  85.     }
  86.     public static function getSubscribedEvents()
  87.     {
  88.         return [
  89.             // KernelEvents::CONTROLLER => 'onKernelController',
  90.             KernelEvents::RESPONSE => [
  91.                 ['onKernelResponse', -108],
  92.             ],
  93.         ];
  94.     }
  95. }