vendor/shopware/core/System/SystemConfig/SystemConfigLoader.php line 48

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SystemConfig;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Field\ConfigJsonField;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Shopware\Core\Kernel;
  10. use function array_shift;
  11. use function explode;
  12. use function json_decode;
  13. #[Package('system-settings')]
  14. class SystemConfigLoader extends AbstractSystemConfigLoader
  15. {
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(protected Connection $connection, protected Kernel $kernel)
  20.     {
  21.     }
  22.     public function getDecorated(): AbstractSystemConfigLoader
  23.     {
  24.         throw new DecorationPatternException(self::class);
  25.     }
  26.     public function load(?string $salesChannelId): array
  27.     {
  28.         $query $this->connection->createQueryBuilder();
  29.         $query->from('system_config');
  30.         $query->select(['configuration_key''configuration_value']);
  31.         if ($salesChannelId === null) {
  32.             $query
  33.                 ->andWhere('sales_channel_id IS NULL');
  34.         } else {
  35.             $query->andWhere('sales_channel_id = :salesChannelId OR system_config.sales_channel_id IS NULL');
  36.             $query->setParameter('salesChannelId'Uuid::fromHexToBytes($salesChannelId));
  37.         }
  38.         $query->addOrderBy('sales_channel_id''ASC');
  39.         $result $query->executeQuery();
  40.         return $this->buildSystemConfigArray($result->fetchAllKeyValue());
  41.     }
  42.     private function buildSystemConfigArray(array $systemConfigs): array
  43.     {
  44.         $configValues = [];
  45.         foreach ($systemConfigs as $key => $value) {
  46.             $keys explode('.', (string) $key);
  47.             if ($value !== null) {
  48.                 $value json_decode((string) $valuetrue512\JSON_THROW_ON_ERROR);
  49.                 if ($value === false || !isset($value[ConfigJsonField::STORAGE_KEY])) {
  50.                     $value null;
  51.                 } else {
  52.                     $value $value[ConfigJsonField::STORAGE_KEY];
  53.                 }
  54.             }
  55.             $configValues $this->getSubArray($configValues$keys$value);
  56.         }
  57.         return $this->filterNotActivatedPlugins($configValues);
  58.     }
  59.     /**
  60.      * @param array|bool|float|int|string|null $value
  61.      */
  62.     private function getSubArray(array $configValues, array $keys$value): array
  63.     {
  64.         $key array_shift($keys);
  65.         if (empty($keys)) {
  66.             // Configs can be overwritten with sales_channel_id
  67.             $inheritedValuePresent \array_key_exists($key$configValues);
  68.             $valueConsideredEmpty = !\is_bool($value) && empty($value);
  69.             if ($inheritedValuePresent && $valueConsideredEmpty) {
  70.                 return $configValues;
  71.             }
  72.             $configValues[$key] = $value;
  73.         } else {
  74.             if (!\array_key_exists($key$configValues)) {
  75.                 $configValues[$key] = [];
  76.             }
  77.             $configValues[$key] = $this->getSubArray($configValues[$key], $keys$value);
  78.         }
  79.         return $configValues;
  80.     }
  81.     private function filterNotActivatedPlugins(array $configValues): array
  82.     {
  83.         $notActivatedPlugins $this->kernel->getPluginLoader()->getPluginInstances()->filter(fn (Plugin $plugin) => !$plugin->isActive())->all();
  84.         foreach ($notActivatedPlugins as $plugin) {
  85.             if (isset($configValues[$plugin->getName()])) {
  86.                 unset($configValues[$plugin->getName()]);
  87.             }
  88.         }
  89.         return $configValues;
  90.     }
  91. }