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

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