custom/plugins/zenitPlatformFontChange/src/Subscriber/ThemeVariablesSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformFontChange\Subscriber;
  3. use Shopware\Core\System\SystemConfig\SystemConfigService;
  4. use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class ThemeVariablesSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var string
  10.      */
  11.     private $configPath 'zenitPlatformFontChange.config.';
  12.     /**
  13.      * @var SystemConfigService
  14.      */
  15.     protected $systemConfig;
  16.     // add the `SystemConfigService` to your constructor
  17.     public function __construct(SystemConfigService $systemConfig)
  18.     {
  19.         $this->systemConfig $systemConfig;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
  25.         ];
  26.     }
  27.     public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
  28.     {
  29.         $shopId $event->getSalesChannelId();
  30.         // is local
  31.         if ($this->systemConfig->get($this->configPath 'activeLocal'$shopId)) {
  32.             /**
  33.              * @var string $fontBaseLocal
  34.              * @var string $fontHeadlineLocal
  35.              */
  36.             $fontBaseLocal $this->systemConfig->get($this->configPath 'fontBaseLocal'$shopId);
  37.             $fontHeadlineLocal $this->systemConfig->get($this->configPath 'fontHeadlineLocal'$shopId);
  38.             if (!empty($fontBaseLocal) && $fontBaseLocal !== 'default') {
  39.                 // get styles, font family, name from string
  40.                 $fontStyleBaseLocal $this->getStringBetween($fontBaseLocal"[""]");
  41.                 list($fontFamilyBaseLocal) = explode(' ['$fontBaseLocal);
  42.                 if (strpos($fontBaseLocal"'") === false) {
  43.                     $fontNameBaseLocal substr($fontBaseLocal0strpos($fontBaseLocal","));
  44.                 } else {
  45.                     $fontNameBaseLocal $this->getStringBetween($fontBaseLocal"'""'");
  46.                 }
  47.                 $event->addVariable('zen-font-change-font-family-base-local'$fontFamilyBaseLocal); // 'Rubik', sans-serif
  48.                 $event->addVariable('zen-font-change-font-name-base-local'$fontNameBaseLocal); // Rubik
  49.                 $event->addVariable('zen-font-change-font-file-base-local'strtolower(str_replace(' ''-'$fontNameBaseLocal)) ); // rubik
  50.                 $event->addVariable('zen-font-change-font-style-base-local'$fontStyleBaseLocal); // regular, italic, 500, 500italic, 600, 600italic, 700, 700italic
  51.                 if ($this->systemConfig->get($this->configPath 'overwriteFontFamilyBaseLocal'$shopId)) {
  52.                     // overwrite theme config variable
  53.                     $event->addVariable('sw-font-family-base'$fontFamilyBaseLocal);
  54.                     // overwrite theme config variable for our themes
  55.                     $event->addVariable('zen-font-families-base'$fontFamilyBaseLocal);
  56.                 }
  57.             }
  58.             if (!empty($fontHeadlineLocal) && $fontHeadlineLocal !== 'default') {
  59.                 // get styles, font family, name from string
  60.                 $fontStyleHeadlineLocal $this->getStringBetween($fontHeadlineLocal"[""]");
  61.                 list($fontFamilyHeadlineLocal) = explode(' ['$fontHeadlineLocal);
  62.                 if (strpos($fontHeadlineLocal"'") === false) {
  63.                     $fontNameHeadlineLocal substr($fontHeadlineLocal0strpos($fontHeadlineLocal","));
  64.                 } else {
  65.                     $fontNameHeadlineLocal $this->getStringBetween($fontHeadlineLocal"'""'");
  66.                 }
  67.                 $event->addVariable('zen-font-change-font-family-headline-local'$fontFamilyHeadlineLocal); // 'Rubik', sans-serif
  68.                 $event->addVariable('zen-font-change-font-name-headline-local'$fontNameHeadlineLocal); // Rubik
  69.                 $event->addVariable('zen-font-change-font-file-headline-local'strtolower(str_replace(' ''-'$fontNameHeadlineLocal))); // rubik
  70.                 $event->addVariable('zen-font-change-font-style-headline-local'$fontStyleHeadlineLocal); // regular, italic, 500, 500italic, 600, 600italic, 700, 700italic
  71.                 // overwrite theme config variable
  72.                 if ($this->systemConfig->get($this->configPath 'overwriteFontFamilyHeadlineLocal'$shopId)) {
  73.                     $event->addVariable('sw-font-family-headline'$fontFamilyHeadlineLocal);
  74.                     // overwrite theme config variable for our themes
  75.                     $event->addVariable('zen-font-families-headline'$fontFamilyHeadlineLocal);
  76.                 }
  77.             }
  78.         }
  79.         // is extern
  80.         if ($this->systemConfig->get($this->configPath 'activeExtern'$shopId)) {
  81.             /**
  82.              * @var string $fontBaseExtern
  83.              * @var string $fontHeadlineExtern
  84.              */
  85.             $fontBaseExtern $this->systemConfig->get($this->configPath 'fontBaseExtern'$shopId);
  86.             $fontHeadlineExtern $this->systemConfig->get($this->configPath 'fontHeadlineExtern'$shopId);
  87.             if (!empty($fontBaseExtern)) {
  88.                 // get font family and correct possible syntax errors
  89.                 $fontFamilyBaseExtern str_replace(';'''$fontBaseExtern);
  90.                 if ($this->systemConfig->get($this->configPath 'overwriteFontFamilyBaseExtern'$shopId)) {
  91.                     // overwrite theme config variable
  92.                     $event->addVariable('sw-font-family-base'$fontFamilyBaseExtern);
  93.                     // overwrite theme config variable for our themes
  94.                     $event->addVariable('zen-font-families-base'$fontFamilyBaseExtern);
  95.                 }
  96.             }
  97.             if (!empty($fontHeadlineExtern)) {
  98.                 // get font family and correct possible syntax errors
  99.                 $fontFamilyHeadlineExtern str_replace(';'''$fontHeadlineExtern);
  100.                 if ($this->systemConfig->get($this->configPath 'overwriteFontFamilyHeadlineExtern'$shopId)) {
  101.                     // overwrite theme config variable
  102.                     $event->addVariable('sw-font-family-headline'$fontFamilyHeadlineExtern);
  103.                     // overwrite theme config variable for our themes
  104.                     $event->addVariable('zen-font-families-headline'$fontFamilyHeadlineExtern);
  105.                 }
  106.             }
  107.         }
  108.         // is custom
  109.         if ($this->systemConfig->get($this->configPath 'activeCustom'$shopId)) {
  110.             /**
  111.              * @var string $fontBaseCustom
  112.              * @var string $formatBaseCustomArray
  113.              * @var string $fileBaseCustom
  114.              * @var string $fontHeadlineCustom
  115.              * @var string $formatHeadlineCustomArray
  116.              * @var string $fileHeadlineCustom
  117.              */
  118.             $fontBaseCustom $this->systemConfig->get($this->configPath 'fontBaseCustom'$shopId);
  119.             $formatBaseCustomArray $this->systemConfig->get($this->configPath 'formatBaseCustom'$shopId);
  120.             $fileBaseCustom $this->systemConfig->get($this->configPath 'fileBaseCustom'$shopId);
  121.             $fontHeadlineCustom $this->systemConfig->get($this->configPath 'fontHeadlineCustom'$shopId);
  122.             $formatHeadlineCustomArray $this->systemConfig->get($this->configPath 'formatHeadlineCustom'$shopId);
  123.             $fileHeadlineCustom $this->systemConfig->get($this->configPath 'fileHeadlineCustom'$shopId);
  124.             if (!empty($fontBaseCustom) && !empty($formatBaseCustomArray) && !empty($fileBaseCustom)) {
  125.                 // get font name
  126.                 if (strpos($fontBaseCustom"'") === false) {
  127.                     if(strpos($fontBaseCustom",") !== false) {
  128.                         $fontNameBaseCustom substr($fontBaseCustom0strpos($fontBaseCustom","));
  129.                     } else {
  130.                         $fontNameBaseCustom $fontBaseCustom;
  131.                     }
  132.                 } else {
  133.                     $fontNameBaseCustom $this->getStringBetween($fontBaseCustom"'""'");
  134.                 }
  135.                 // get font formats
  136.                 $formatBaseCustom implode(','$formatBaseCustomArray);
  137.                 // get font style
  138.                 // get font files
  139.                 $fileBaseCustomArray explode("\n"$fileBaseCustom);
  140.                 $fileBaseCustomMap '(' implode(','$fileBaseCustomArray) . ')';
  141.                 $event->addVariable('zen-font-change-font-family-base-custom'$fontBaseCustom); // 'Rubik', sans-serif
  142.                 $event->addVariable('zen-font-change-font-name-base-custom'$fontNameBaseCustom); // Rubik
  143.                 $event->addVariable('zen-font-change-font-map-base-custom'$fileBaseCustomMap); // woff, woff2, svg, ttf, otf
  144.                 $event->addVariable('zen-font-change-font-format-base-custom'$formatBaseCustom); // woff, woff2, svg, ttf, otf
  145.             }
  146.             if (!empty($fontBaseCustom) && $this->systemConfig->get($this->configPath 'overwriteFontFamilyBaseCustom'$shopId)) {
  147.                 // get font family and correct possible syntax errors
  148.                 $fontFamilyBaseCustom str_replace(';'''$fontBaseCustom);
  149.                 // overwrite theme config variable
  150.                 $event->addVariable('sw-font-family-base'$fontFamilyBaseCustom);
  151.                 // overwrite theme config variable for our themes
  152.                 $event->addVariable('zen-font-families-base'$fontFamilyBaseCustom);
  153.             }
  154.             if (!empty($fontHeadlineCustom) && !empty($formatHeadlineCustomArray) && !empty($fileHeadlineCustom)) {
  155.                 // get font name
  156.                 if (strpos($fontHeadlineCustom"'") === false) {
  157.                     if(strpos($fontHeadlineCustom",") !== false) {
  158.                         $fontNameHeadlineCustom substr($fontHeadlineCustom0strpos($fontHeadlineCustom","));
  159.                     } else {
  160.                         $fontNameHeadlineCustom $fontHeadlineCustom;
  161.                     }
  162.                 } else {
  163.                     $fontNameHeadlineCustom $this->getStringBetween($fontHeadlineCustom"'""'");
  164.                 }
  165.                 // get font formats
  166.                 $formatHeadlineCustom implode(','$formatHeadlineCustomArray);
  167.                 // get font style
  168.                 // get font files
  169.                 $fileHeadlineCustomArray explode("\n"$fileHeadlineCustom);
  170.                 $fileHeadlineCustomMap '(' implode(','$fileHeadlineCustomArray) . ')';
  171.                 $event->addVariable('zen-font-change-font-family-headline-custom'$fontHeadlineCustom); // 'Rubik', sans-serif
  172.                 $event->addVariable('zen-font-change-font-name-headline-custom'$fontNameHeadlineCustom); // Rubik
  173.                 $event->addVariable('zen-font-change-font-map-headline-custom'$fileHeadlineCustomMap); // woff, woff2, svg, ttf, otf
  174.                 $event->addVariable('zen-font-change-font-format-headline-custom'$formatHeadlineCustom); // woff, woff2, svg, ttf, otf
  175.             }
  176.             if (!empty($fontHeadlineCustom) && $this->systemConfig->get($this->configPath 'overwriteFontFamilyHeadlineCustom'$shopId)) {
  177.                 // get font family and correct possible syntax errors
  178.                 $fontFamilyHeadlineCustom str_replace(';'''$fontHeadlineCustom);
  179.                 // overwrite theme config variable
  180.                 $event->addVariable('sw-font-family-headline'$fontFamilyHeadlineCustom);
  181.                 // overwrite theme config variable for our themes
  182.                 $event->addVariable('zen-font-families-headline'$fontFamilyHeadlineCustom);
  183.             }
  184.         }
  185.     }
  186.     /**
  187.      * @param $string
  188.      * @param $start
  189.      * @param $end
  190.      * @return false|string
  191.      */
  192.     private function getStringBetween($string$start$end){
  193.         $string ' ' $string;
  194.         $ini strpos($string$start);
  195.         if ($ini == 0) return '';
  196.         $ini += strlen($start);
  197.         $len strpos($string$end$ini) - $ini;
  198.         return substr($string$ini$len);
  199.     }
  200. }