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

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