custom/plugins/MaxiaVariantsTable6/src/Storefront/Subscriber/ThemeVariablesSubscriber.php line 71

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Maxia\MaxiaVariantsTable6\Storefront\Subscriber;
  3. use Shopware\Core\System\SystemConfig\SystemConfigService;
  4. use Shopware\Storefront\Event\ThemeCompilerEnrichScssVariablesEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use function Symfony\Component\String\u;
  7. class ThemeVariablesSubscriber implements EventSubscriberInterface
  8. {
  9.     public static function getSubscribedEvents(): array
  10.     {
  11.         return [
  12.             ThemeCompilerEnrichScssVariablesEvent::class => 'onAddVariables'
  13.         ];
  14.     }
  15.     /**
  16.      * @var SystemConfigService
  17.      */
  18.     protected $systemConfig;
  19.     /**
  20.      * @var string
  21.      */
  22.     protected $namespace;
  23.     /**
  24.      * @var string
  25.      */
  26.     protected $addPrefix;
  27.     /**
  28.      * @var string
  29.      */
  30.     protected $searchPrefix;
  31.     protected $types = [
  32.         'px' => [
  33.             'paddingY''paddingX''headingPaddingY''headingPaddingX''cellPaddingY''cellPaddingX',
  34.             'cellHeight''cellProductNumberWidth''cellManufacturerNumberWidth''cellImageWidth',
  35.             'cellDeliveryWidth''cellBuyWidth''quantityInputHeight''quantityInputBorderRadius',
  36.             'quantityInputButtonPaddingX''quantityInputButtonPaddingXMobile''cellImageHeight'
  37.         ]
  38.     ];
  39.     /**
  40.      * @param SystemConfigService $systemConfig
  41.      * @param string $namespace
  42.      * @param string $addPrefix
  43.      * @param string $searchPrefix
  44.      */
  45.     public function __construct(
  46.         SystemConfigService $systemConfig,
  47.         string $namespace,
  48.         string $addPrefix '',
  49.         string $searchPrefix 'scss'
  50.     ) {
  51.         $this->systemConfig $systemConfig;
  52.         $this->namespace $namespace;
  53.         $this->addPrefix $addPrefix '-';
  54.         $this->searchPrefix $searchPrefix;
  55.     }
  56.     /**
  57.      * Reads all config options that begin with 'scss' and adds them as SASS variables.
  58.      *
  59.      * @param ThemeCompilerEnrichScssVariablesEvent $event
  60.      */
  61.     public function onAddVariables(ThemeCompilerEnrichScssVariablesEvent $event)
  62.     {
  63.         $databaseUrl getenv('DATABASE_URL');
  64.         if (!$databaseUrl || $databaseUrl === 'mysql://_placeholder.test') {
  65.             // deployment server without database
  66.             return;
  67.         }
  68.         $config $this->systemConfig->get($this->namespace$event->getSalesChannelId());
  69.         foreach ($config as $key => $value) {
  70.             $value u((string)$value)->trim();
  71.             if ($value->isEmpty()) {
  72.                 continue;
  73.             }
  74.             $key u($key);
  75.             if (!$key->startsWith($this->searchPrefix)) {
  76.                 continue;
  77.             }
  78.             $key lcfirst($key->slice(strlen($this->searchPrefix))->toString());
  79.             $keyDashed $this->addPrefix $this->camel2dashed($key);
  80.             // auto append unit
  81.             foreach ($this->types as $type => $keys) {
  82.                 if (in_array($key$keys)) {
  83.                     if (!$value->endsWith($type)) {
  84.                         $value $value->append($type);
  85.                     }
  86.                 }
  87.             }
  88.             $event->addVariable($keyDashed$value->toString());
  89.         }
  90.     }
  91.     /**
  92.      * @param $string
  93.      * @return string
  94.      */
  95.     protected function camel2dashed($string) {
  96.         return strtolower(preg_replace('/([a-zA-Z])(?=[A-Z])/''$1-'$string));
  97.     }
  98. }