custom/plugins/zenitPlatformFontChange/src/Subscriber/StorefrontRenderSubscriber.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformFontChange\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  4. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  5. use Shopware\Core\System\SystemConfig\Exception\InvalidDomainException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Event\StorefrontRenderEvent;
  9. use zenit\PlatformFontChange\Struct\SystemConfigData;
  10. class StorefrontRenderSubscriber  implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var string
  14.      */
  15.     private $pluginName 'zenitPlatformFontChange';
  16.     /**
  17.      * @var string
  18.      */
  19.     private $configPath 'zenitPlatformFontChange.config.';
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private $systemConfig;
  24.     /**
  25.      * HeaderPageletLoadedSubscriber constructor.
  26.      * @param SystemConfigService $systemConfig
  27.      */
  28.     public function __construct(SystemConfigService $systemConfig)
  29.     {
  30.         $this->systemConfig $systemConfig;
  31.     }
  32.     /**
  33.      * @return array
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return[
  38.             StorefrontRenderEvent::class => 'onStorefrontRender'
  39.         ];
  40.     }
  41.     /**
  42.      * @param StorefrontRenderEvent $event
  43.      * @throws InvalidDomainException
  44.      * @throws InvalidUuidException
  45.      * @throws InconsistentCriteriaIdsException
  46.      */
  47.     public function onStorefrontRender(StorefrontRenderEvent $event)
  48.     {
  49.         $shopId $event->getSalesChannelContext()->getSalesChannel()->getId();
  50.         // get config
  51.         $systemConfig $this->systemConfig->getDomain($this->configPath$shopIdtrue);
  52.         // replace domainstrings in keys
  53.         $config = [];
  54.         foreach($systemConfig as $key => $value) {
  55.             $config[str_replace($this->configPath'',$key)] = $value;
  56.         }
  57.         if ($this->systemConfig->get($this->configPath 'activeExtern'$shopId)) {
  58.             /**
  59.              * @var string $fontBaseExtern
  60.              * @var string $styleBaseExtern
  61.              * @var string $fontHeadlineExtern
  62.              * @var string $styleHeadlineExtern
  63.              */
  64.             $fontBaseExtern $this->systemConfig->get($this->configPath 'fontBaseExtern'$shopId);
  65.             $styleBaseExtern $this->systemConfig->get($this->configPath 'styleBaseExtern'$shopId);
  66.             $fontHeadlineExtern $this->systemConfig->get($this->configPath 'fontHeadlineExtern'$shopId);
  67.             $styleHeadlineExtern $this->systemConfig->get($this->configPath 'styleHeadlineExtern'$shopId);
  68.             // get font family
  69.             if (!empty($fontBaseExtern)) {
  70.                 if (strpos($fontBaseExtern"'") === false) {
  71.                     if(strpos($fontBaseExtern",") !== false) {
  72.                         $fontFamilyBaseExtern substr($fontBaseExtern0strpos($fontBaseExtern","));
  73.                     } else {
  74.                         $fontFamilyBaseExtern $fontBaseExtern;
  75.                     }
  76.                 } else {
  77.                     $fontFamilyBaseExtern $this->getStringBetween($fontBaseExtern"'""'");
  78.                 }
  79.             }
  80.             if (!empty($fontHeadlineExtern)) {
  81.                 if (strpos($fontHeadlineExtern"'") === false) {
  82.                     if(strpos($fontHeadlineExtern",") !== false) {
  83.                         $fontFamilyHeadlineExtern substr($fontHeadlineExtern0strpos($fontHeadlineExtern","));
  84.                     } else {
  85.                         $fontFamilyHeadlineExtern $fontHeadlineExtern;
  86.                     }
  87.                 } else {
  88.                     $fontFamilyHeadlineExtern $this->getStringBetween($fontHeadlineExtern"'""'");
  89.                 }
  90.             }
  91.             // avoid double implementation when base and headline fonts are the same
  92.             if (!empty($fontFamilyBaseExtern) && !empty($fontFamilyHeadlineExtern)) {
  93.                 if ($fontFamilyBaseExtern === $fontFamilyHeadlineExtern) {
  94.                     $config['fontFamilyCumulativeExtern'] = str_replace(' ''+'$fontFamilyBaseExtern);
  95.                     if (!empty($styleBaseExtern) && !empty($styleHeadlineExtern)) {
  96.                         $config['fontStyleCumulativeExtern'] = array_merge($styleBaseExtern$styleHeadlineExtern);
  97.                     }
  98.                 } else {
  99.                     $config['fontFamilyBaseExtern'] = str_replace(' ''+'$fontFamilyBaseExtern);
  100.                     $config['fontStyleBaseExtern'] = $styleBaseExtern;
  101.                     $config['fontFamilyHeadlineExtern'] = str_replace(' ''+'$fontFamilyHeadlineExtern);
  102.                     $config['fontStyleHeadlineExtern'] = $styleHeadlineExtern;
  103.                 }
  104.             }
  105.         }
  106.         // set config
  107.         $configValues = new SystemConfigData($config);
  108.         // add config
  109.         $event->getContext()->addExtension($this->pluginName$configValues);
  110.     }
  111.     /**
  112.      * @param $string
  113.      * @param $start
  114.      * @param $end
  115.      * @return false|string
  116.      */
  117.     private function getStringBetween($string$start$end){
  118.         $string ' ' $string;
  119.         $ini strpos($string$start);
  120.         if ($ini == 0) return '';
  121.         $ini += strlen($start);
  122.         $len strpos($string$end$ini) - $ini;
  123.         return substr($string$ini$len);
  124.     }
  125. }