vendor/pimcore/pimcore/lib/Targeting/Code/TargetingCodeGenerator.php line 69

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Enterprise License (PEL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  14.  */
  15. namespace Pimcore\Targeting\Code;
  16. use Pimcore\Analytics\Code\CodeBlock;
  17. use Pimcore\Event\Targeting\TargetingCodeEvent;
  18. use Pimcore\Event\TargetingEvents;
  19. use Pimcore\Targeting\Model\VisitorInfo;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\Templating\EngineInterface;
  22. class TargetingCodeGenerator
  23. {
  24.     const BLOCK_BEFORE_SCRIPT_TAG 'beforeScriptTag';
  25.     const BLOCK_BEFORE_SCRIPT 'beforeScript';
  26.     const BLOCK_AFTER_SCRIPT 'afterScript';
  27.     const BLOCK_AFTER_SCRIPT_TAG 'afterScriptTag';
  28.     /**
  29.      * @var EventDispatcherInterface
  30.      */
  31.     private $eventDispatcher;
  32.     /**
  33.      * @var EngineInterface
  34.      */
  35.     private $templatingEngine;
  36.     /**
  37.      * @var array
  38.      */
  39.     private $blocks = [
  40.         self::BLOCK_BEFORE_SCRIPT_TAG,
  41.         self::BLOCK_BEFORE_SCRIPT,
  42.         self::BLOCK_AFTER_SCRIPT,
  43.         self::BLOCK_AFTER_SCRIPT_TAG,
  44.     ];
  45.     public function __construct(
  46.         EventDispatcherInterface $eventDispatcher,
  47.         EngineInterface $templatingEngine
  48.     ) {
  49.         $this->eventDispatcher $eventDispatcher;
  50.         $this->templatingEngine $templatingEngine;
  51.     }
  52.     public function generateCode(VisitorInfo $visitorInfo): string
  53.     {
  54.         $data = [
  55.             'inDebugMode' => \Pimcore::inDebugMode(),
  56.             'dataProviderKeys' => $visitorInfo->getFrontendDataProviders(),
  57.         ];
  58.         $event = new TargetingCodeEvent(
  59.             '@PimcoreCore/Targeting/targetingCode.html.twig',
  60.             $this->buildCodeBlocks(),
  61.             $data
  62.         );
  63.         $this->eventDispatcher->dispatch(TargetingEvents::TARGETING_CODE$event);
  64.         return $this->renderTemplate($event);
  65.     }
  66.     private function renderTemplate(TargetingCodeEvent $event): string
  67.     {
  68.         $data $event->getData();
  69.         $data['blocks'] = $event->getBlocks();
  70.         $code $this->templatingEngine->render(
  71.             $event->getTemplate(),
  72.             $data
  73.         );
  74.         $code trim($code);
  75.         return $code;
  76.     }
  77.     private function buildCodeBlocks(): array
  78.     {
  79.         $blocks = [];
  80.         foreach ($this->blocks as $block) {
  81.             $blocks[$block] = new CodeBlock();
  82.         }
  83.         return $blocks;
  84.     }
  85. }