<?php declare(strict_types=1);
namespace Avency\Shopware\Cms\Core\Subscriber;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
class ScssSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
protected $systemConfigService;
public function __construct(SystemConfigService $systemConfigService)
{
$this->systemConfigService = $systemConfigService;
}
/**
* Returns subscribed events
*
* @return array|string[]
*/
public static function getSubscribedEvents(): array
{
return [
ThemeCompilerEnrichScssVariablesEvent::class => 'addScssVariables'
];
}
/**
* Add scss variables
*
* @param ThemeCompilerEnrichScssVariablesEvent $event
* @return void
*/
public function addScssVariables(ThemeCompilerEnrichScssVariablesEvent $event): void
{
$config = $this->systemConfigService->get('AvencyShopwareCms.config', $event->getSalesChannelId());
if ($config['colorsActive']) {
foreach ($config as $key => $value) {
$variable = str_replace('_', '-', (new CamelCaseToSnakeCaseNameConverter())->normalize($key));
if (strpos($variable, 'avency') !== false && strpos($value, '#') !== false) {
$event->addVariable($variable, $value);
}
}
}
}
}