vendor/shopware/core/System/SystemConfig/Api/SystemConfigController.php line 57

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SystemConfig\Api;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\Feature;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  7. use Shopware\Core\System\SystemConfig\Service\ConfigurationService;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Core\System\SystemConfig\Validation\SystemConfigValidator;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. #[Route(defaults: ['_routeScope' => ['api']])]
  16. #[Package('system-settings')]
  17. class SystemConfigController extends AbstractController
  18. {
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         private readonly ConfigurationService $configurationService,
  24.         private readonly SystemConfigService $systemConfig,
  25.         private readonly SystemConfigValidator $systemConfigValidator
  26.     ) {
  27.     }
  28.     #[Route(path'/api/_action/system-config/check'name'api.action.core.system-config.check'methods: ['GET'], defaults: ['_acl' => ['system_config:read']])]
  29.     public function checkConfiguration(Request $requestContext $context): JsonResponse
  30.     {
  31.         $domain = (string) $request->query->get('domain');
  32.         if ($domain === '') {
  33.             return new JsonResponse(false);
  34.         }
  35.         return new JsonResponse($this->configurationService->checkConfiguration($domain$context));
  36.     }
  37.     #[Route(path'/api/_action/system-config/schema'name'api.action.core.system-config'methods: ['GET'])]
  38.     public function getConfiguration(Request $requestContext $context): JsonResponse
  39.     {
  40.         $domain = (string) $request->query->get('domain');
  41.         if ($domain === '') {
  42.             throw new MissingRequestParameterException('domain');
  43.         }
  44.         return new JsonResponse($this->configurationService->getConfiguration($domain$context));
  45.     }
  46.     #[Route(path'/api/_action/system-config'name'api.action.core.system-config.value'methods: ['GET'], defaults: ['_acl' => ['system_config:read']])]
  47.     public function getConfigurationValues(Request $request): JsonResponse
  48.     {
  49.         $domain = (string) $request->query->get('domain');
  50.         if ($domain === '') {
  51.             throw new MissingRequestParameterException('domain');
  52.         }
  53.         $salesChannelId $request->query->get('salesChannelId');
  54.         if (!\is_string($salesChannelId)) {
  55.             $salesChannelId null;
  56.         }
  57.         $values $this->systemConfig->getDomain($domain$salesChannelId);
  58.         if (empty($values)) {
  59.             $json '{}';
  60.         } else {
  61.             $json json_encode($values\JSON_PRESERVE_ZERO_FRACTION);
  62.         }
  63.         return new JsonResponse($json200, [], true);
  64.     }
  65.     #[Route(path'/api/_action/system-config'name'api.action.core.save.system-config'methods: ['POST'], defaults: ['_acl' => ['system_config:update''system_config:create''system_config:delete']])]
  66.     public function saveConfiguration(Request $request): JsonResponse
  67.     {
  68.         $salesChannelId $request->query->get('salesChannelId');
  69.         if (!\is_string($salesChannelId)) {
  70.             $salesChannelId null;
  71.         }
  72.         $kvs $request->request->all();
  73.         $this->saveKeyValues($salesChannelId$kvs);
  74.         return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  75.     }
  76.     /**
  77.      * @deprecated tag:v6.6.0 $context param will be required
  78.      */
  79.     #[Route(path'/api/_action/system-config/batch'name'api.action.core.save.system-config.batch'methods: ['POST'], defaults: ['_acl' => ['system_config:update''system_config:create''system_config:delete']])]
  80.     public function batchSaveConfiguration(Request $request, ?Context $context null): JsonResponse
  81.     {
  82.         if (!$context) {
  83.             Feature::triggerDeprecationOrThrow(
  84.                 'v6.6.0.0',
  85.                 'Second parameter `$context` will be required in method `batchSaveConfiguration()` in `SystemConfigController` in v6.6.0.0'
  86.             );
  87.             $context Context::createDefaultContext();
  88.         }
  89.         $this->systemConfigValidator->validate($request->request->all(), $context);
  90.         /**
  91.          * @var string $salesChannelId
  92.          * @var array<string, mixed> $kvs
  93.          */
  94.         foreach ($request->request->all() as $salesChannelId => $kvs) {
  95.             if ($salesChannelId === 'null') {
  96.                 $salesChannelId null;
  97.             }
  98.             $this->saveKeyValues($salesChannelId$kvs);
  99.         }
  100.         return new JsonResponse(nullResponse::HTTP_NO_CONTENT);
  101.     }
  102.     /**
  103.      * @param array<string, mixed> $kvs
  104.      */
  105.     private function saveKeyValues(?string $salesChannelId, array $kvs): void
  106.     {
  107.         foreach ($kvs as $key => $value) {
  108.             $this->systemConfig->set($key$value$salesChannelId);
  109.         }
  110.     }
  111. }