<?php declare(strict_types=1);
namespace zenit\PlatformFontChange\Subscriber;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ThemeVariablesSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $configPath = 'zenitPlatformFontChange.config.';
/**
* @var SystemConfigService
*/
protected $systemConfig;
// add the `SystemConfigService` to your constructor
public function __construct(SystemConfigService $systemConfig)
{
$this->systemConfig = $systemConfig;
}
public static function getSubscribedEvents(): array
{
return [
ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
];
}
public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
{
$shopId = $event->getSalesChannelId();
// is local
if ($this->systemConfig->get($this->configPath . 'activeLocal', $shopId)) {
/**
* @var string $fontBaseLocal
* @var string $fontHeadlineLocal
*/
$fontBaseLocal = $this->systemConfig->get($this->configPath . 'fontBaseLocal', $shopId);
$fontHeadlineLocal = $this->systemConfig->get($this->configPath . 'fontHeadlineLocal', $shopId);
if (!empty($fontBaseLocal) && $fontBaseLocal !== 'default') {
// get styles, font family, name from string
$fontStyleBaseLocal = $this->getStringBetween($fontBaseLocal, "[", "]");
list($fontFamilyBaseLocal) = explode(' [', $fontBaseLocal);
if (strpos($fontBaseLocal, "'") === false) {
$fontNameBaseLocal = substr($fontBaseLocal, 0, strpos($fontBaseLocal, ","));
} else {
$fontNameBaseLocal = $this->getStringBetween($fontBaseLocal, "'", "'");
}
$event->addVariable('zen-font-change-font-family-base-local', $fontFamilyBaseLocal); // 'Rubik', sans-serif
$event->addVariable('zen-font-change-font-name-base-local', $fontNameBaseLocal); // Rubik
$event->addVariable('zen-font-change-font-file-base-local', strtolower(str_replace(' ', '-', $fontNameBaseLocal)) ); // rubik
$event->addVariable('zen-font-change-font-style-base-local', $fontStyleBaseLocal); // regular, italic, 500, 500italic, 600, 600italic, 700, 700italic
if ($this->systemConfig->get($this->configPath . 'overwriteFontFamilyBaseLocal', $shopId)) {
// overwrite theme config variable
$event->addVariable('sw-font-family-base', $fontFamilyBaseLocal);
// overwrite theme config variable for our themes
$event->addVariable('zen-font-families-base', $fontFamilyBaseLocal);
}
}
if (!empty($fontHeadlineLocal) && $fontHeadlineLocal !== 'default') {
// get styles, font family, name from string
$fontStyleHeadlineLocal = $this->getStringBetween($fontHeadlineLocal, "[", "]");
list($fontFamilyHeadlineLocal) = explode(' [', $fontHeadlineLocal);
if (strpos($fontHeadlineLocal, "'") === false) {
$fontNameHeadlineLocal = substr($fontHeadlineLocal, 0, strpos($fontHeadlineLocal, ","));
} else {
$fontNameHeadlineLocal = $this->getStringBetween($fontHeadlineLocal, "'", "'");
}
$event->addVariable('zen-font-change-font-family-headline-local', $fontFamilyHeadlineLocal); // 'Rubik', sans-serif
$event->addVariable('zen-font-change-font-name-headline-local', $fontNameHeadlineLocal); // Rubik
$event->addVariable('zen-font-change-font-file-headline-local', strtolower(str_replace(' ', '-', $fontNameHeadlineLocal))); // rubik
$event->addVariable('zen-font-change-font-style-headline-local', $fontStyleHeadlineLocal); // regular, italic, 500, 500italic, 600, 600italic, 700, 700italic
// overwrite theme config variable
if ($this->systemConfig->get($this->configPath . 'overwriteFontFamilyHeadlineLocal', $shopId)) {
$event->addVariable('sw-font-family-headline', $fontFamilyHeadlineLocal);
// overwrite theme config variable for our themes
$event->addVariable('zen-font-families-headline', $fontFamilyHeadlineLocal);
}
}
}
// is extern
if ($this->systemConfig->get($this->configPath . 'activeExtern', $shopId)) {
/**
* @var string $fontBaseExtern
* @var string $fontHeadlineExtern
*/
$fontBaseExtern = $this->systemConfig->get($this->configPath . 'fontBaseExtern', $shopId);
$fontHeadlineExtern = $this->systemConfig->get($this->configPath . 'fontHeadlineExtern', $shopId);
if (!empty($fontBaseExtern)) {
// get font family and correct possible syntax errors
$fontFamilyBaseExtern = str_replace(';', '', $fontBaseExtern);
if ($this->systemConfig->get($this->configPath . 'overwriteFontFamilyBaseExtern', $shopId)) {
// overwrite theme config variable
$event->addVariable('sw-font-family-base', $fontFamilyBaseExtern);
// overwrite theme config variable for our themes
$event->addVariable('zen-font-families-base', $fontFamilyBaseExtern);
}
}
if (!empty($fontHeadlineExtern)) {
// get font family and correct possible syntax errors
$fontFamilyHeadlineExtern = str_replace(';', '', $fontHeadlineExtern);
if ($this->systemConfig->get($this->configPath . 'overwriteFontFamilyHeadlineExtern', $shopId)) {
// overwrite theme config variable
$event->addVariable('sw-font-family-headline', $fontFamilyHeadlineExtern);
// overwrite theme config variable for our themes
$event->addVariable('zen-font-families-headline', $fontFamilyHeadlineExtern);
}
}
}
// is custom
if ($this->systemConfig->get($this->configPath . 'activeCustom', $shopId)) {
/**
* @var string $fontBaseCustom
* @var string $formatBaseCustomArray
* @var string $fileBaseCustom
* @var string $fontHeadlineCustom
* @var string $formatHeadlineCustomArray
* @var string $fileHeadlineCustom
*/
$fontBaseCustom = $this->systemConfig->get($this->configPath . 'fontBaseCustom', $shopId);
$formatBaseCustomArray = $this->systemConfig->get($this->configPath . 'formatBaseCustom', $shopId);
$fileBaseCustom = $this->systemConfig->get($this->configPath . 'fileBaseCustom', $shopId);
$fontHeadlineCustom = $this->systemConfig->get($this->configPath . 'fontHeadlineCustom', $shopId);
$formatHeadlineCustomArray = $this->systemConfig->get($this->configPath . 'formatHeadlineCustom', $shopId);
$fileHeadlineCustom = $this->systemConfig->get($this->configPath . 'fileHeadlineCustom', $shopId);
if (!empty($fontBaseCustom) && !empty($formatBaseCustomArray) && !empty($fileBaseCustom)) {
// get font name
if (strpos($fontBaseCustom, "'") === false) {
if(strpos($fontBaseCustom, ",") !== false) {
$fontNameBaseCustom = substr($fontBaseCustom, 0, strpos($fontBaseCustom, ","));
} else {
$fontNameBaseCustom = $fontBaseCustom;
}
} else {
$fontNameBaseCustom = $this->getStringBetween($fontBaseCustom, "'", "'");
}
// get font formats
$formatBaseCustom = implode(',', $formatBaseCustomArray);
// get font style
// get font files
$fileBaseCustomArray = explode("\n", $fileBaseCustom);
$fileBaseCustomMap = '(' . implode(',', $fileBaseCustomArray) . ')';
$event->addVariable('zen-font-change-font-family-base-custom', $fontBaseCustom); // 'Rubik', sans-serif
$event->addVariable('zen-font-change-font-name-base-custom', $fontNameBaseCustom); // Rubik
$event->addVariable('zen-font-change-font-map-base-custom', $fileBaseCustomMap); // woff, woff2, svg, ttf, otf
$event->addVariable('zen-font-change-font-format-base-custom', $formatBaseCustom); // woff, woff2, svg, ttf, otf
}
if (!empty($fontBaseCustom) && $this->systemConfig->get($this->configPath . 'overwriteFontFamilyBaseCustom', $shopId)) {
// get font family and correct possible syntax errors
$fontFamilyBaseCustom = str_replace(';', '', $fontBaseCustom);
// overwrite theme config variable
$event->addVariable('sw-font-family-base', $fontFamilyBaseCustom);
// overwrite theme config variable for our themes
$event->addVariable('zen-font-families-base', $fontFamilyBaseCustom);
}
if (!empty($fontHeadlineCustom) && !empty($formatHeadlineCustomArray) && !empty($fileHeadlineCustom)) {
// get font name
if (strpos($fontHeadlineCustom, "'") === false) {
if(strpos($fontHeadlineCustom, ",") !== false) {
$fontNameHeadlineCustom = substr($fontHeadlineCustom, 0, strpos($fontHeadlineCustom, ","));
} else {
$fontNameHeadlineCustom = $fontHeadlineCustom;
}
} else {
$fontNameHeadlineCustom = $this->getStringBetween($fontHeadlineCustom, "'", "'");
}
// get font formats
$formatHeadlineCustom = implode(',', $formatHeadlineCustomArray);
// get font style
// get font files
$fileHeadlineCustomArray = explode("\n", $fileHeadlineCustom);
$fileHeadlineCustomMap = '(' . implode(',', $fileHeadlineCustomArray) . ')';
$event->addVariable('zen-font-change-font-family-headline-custom', $fontHeadlineCustom); // 'Rubik', sans-serif
$event->addVariable('zen-font-change-font-name-headline-custom', $fontNameHeadlineCustom); // Rubik
$event->addVariable('zen-font-change-font-map-headline-custom', $fileHeadlineCustomMap); // woff, woff2, svg, ttf, otf
$event->addVariable('zen-font-change-font-format-headline-custom', $formatHeadlineCustom); // woff, woff2, svg, ttf, otf
}
if (!empty($fontHeadlineCustom) && $this->systemConfig->get($this->configPath . 'overwriteFontFamilyHeadlineCustom', $shopId)) {
// get font family and correct possible syntax errors
$fontFamilyHeadlineCustom = str_replace(';', '', $fontHeadlineCustom);
// overwrite theme config variable
$event->addVariable('sw-font-family-headline', $fontFamilyHeadlineCustom);
// overwrite theme config variable for our themes
$event->addVariable('zen-font-families-headline', $fontFamilyHeadlineCustom);
}
}
}
/**
* @param $string
* @param $start
* @param $end
* @return false|string
*/
private function getStringBetween($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
}