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

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