vendor/dachcom-digital/toolbox/src/ToolboxBundle/EventListener/Frontend/FrontendJsListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace ToolboxBundle\EventListener\Frontend;
  3. use Pimcore\Tool;
  4. use Pimcore\Bundle\CoreBundle\EventListener\Traits\EnabledTrait;
  5. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  6. use Pimcore\Bundle\CoreBundle\EventListener\Traits\ResponseInjectionTrait;
  7. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. use Symfony\Component\Templating\EngineInterface;
  10. class FrontendJsListener
  11. {
  12.     use EnabledTrait;
  13.     use ResponseInjectionTrait;
  14.     use PimcoreContextAwareTrait;
  15.     private EngineInterface $templatingEngine;
  16.     public function __construct(EngineInterface $templatingEngine)
  17.     {
  18.         $this->templatingEngine $templatingEngine;
  19.     }
  20.     public function onKernelResponse(ResponseEvent $event): void
  21.     {
  22.         if (!$this->isEnabled()) {
  23.             return;
  24.         }
  25.         $request $event->getRequest();
  26.         if (!$event->isMainRequest()) {
  27.             return;
  28.         }
  29.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  30.             return;
  31.         }
  32.         if (!Tool::useFrontendOutputFilters()) {
  33.             return;
  34.         }
  35.         $serverVars $event->getRequest()->server;
  36.         if ($serverVars->get('HTTP_X_PURPOSE') === 'preview') {
  37.             return;
  38.         }
  39.         $response $event->getResponse();
  40.         if (!$this->isHtmlResponse($response)) {
  41.             return;
  42.         }
  43.         $optOutCookie $request->cookies->get('tb-google-opt-out-link');
  44.         $codeHead $this->renderTemplate(
  45.             '@Toolbox/admin/javascript/frontend.html.twig',
  46.             [
  47.                 'translations'       => [
  48.                     'toolbox.goptout_already_opt_out',
  49.                     'toolbox.goptout_successfully_opt_out'
  50.                 ],
  51.                 'trackingIsDisabled' => !empty($optOutCookie),
  52.                 'code'               => $optOutCookie
  53.             ]
  54.         );
  55.         $content $response->getContent();
  56.         if (!empty($codeHead)) {
  57.             $headEndPosition stripos($content'</head>');
  58.             if ($headEndPosition !== false) {
  59.                 $content substr_replace($content$codeHead '</head>'$headEndPosition7);
  60.             }
  61.         }
  62.         $response->setContent($content);
  63.     }
  64.     private function renderTemplate(string $template, array $data): string
  65.     {
  66.         $code $this->templatingEngine->render(
  67.             $template,
  68.             $data
  69.         );
  70.         $code trim($code);
  71.         if (!empty($code)) {
  72.             $code "\n" $code "\n";
  73.         }
  74.         return $code;
  75.     }
  76. }