vendor/shopware/core/System/SalesChannel/SalesChannel/StoreApiInfoController.php line 70

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel\SalesChannel;
  3. use Shopware\Core\Framework\Api\ApiDefinition\DefinitionService;
  4. use Shopware\Core\Framework\Api\ApiDefinition\Generator\OpenApi3Generator;
  5. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  6. use Shopware\Core\Framework\Routing\Annotation\Since;
  7. use Shopware\Core\PlatformRequest;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Twig\Environment;
  13. /**
  14.  * @Route(defaults={"_routeScope"={"store-api"}})
  15.  */
  16. class StoreApiInfoController
  17. {
  18.     /**
  19.      * @var DefinitionService
  20.      */
  21.     protected $definitionService;
  22.     /**
  23.      * @var Environment
  24.      */
  25.     private $twig;
  26.     /**
  27.      * @var array
  28.      */
  29.     private $cspTemplates;
  30.     public function __construct(DefinitionService $definitionServiceEnvironment $twig, array $cspTemplates)
  31.     {
  32.         $this->definitionService $definitionService;
  33.         $this->twig $twig;
  34.         $this->cspTemplates $cspTemplates;
  35.     }
  36.     /**
  37.      * @Since("6.2.0.0")
  38.      * @Route("/store-api/_info/openapi3.json", defaults={"auth_required"="%shopware.api.api_browser.auth_required_str%"}, name="store-api.info.openapi3", methods={"GET"})
  39.      */
  40.     public function info(Request $request): JsonResponse
  41.     {
  42.         $apiType $request->query->getAlpha('type'DefinitionService::TypeJsonApi);
  43.         $data $this->definitionService->generate(OpenApi3Generator::FORMATDefinitionService::STORE_API$apiType);
  44.         return new JsonResponse($data);
  45.     }
  46.     /**
  47.      * @Since("6.2.0.0")
  48.      * @Route("/store-api/_info/open-api-schema.json", defaults={"auth_required"="%shopware.api.api_browser.auth_required_str%"}, name="store-api.info.open-api-schema", methods={"GET"})
  49.      */
  50.     public function openApiSchema(): JsonResponse
  51.     {
  52.         $data $this->definitionService->getSchema(OpenApi3Generator::FORMATDefinitionService::STORE_API);
  53.         return new JsonResponse($data);
  54.     }
  55.     /**
  56.      * @Since("6.2.0.0")
  57.      * @Route("/store-api/_info/swagger.html", defaults={"auth_required"="%shopware.api.api_browser.auth_required_str%"}, name="store-api.info.swagger", methods={"GET"})
  58.      */
  59.     public function infoHtml(Request $request): Response
  60.     {
  61.         $nonce $request->attributes->get(PlatformRequest::ATTRIBUTE_CSP_NONCE);
  62.         $apiType $request->query->getAlpha('type'DefinitionService::TypeJsonApi);
  63.         $response = new Response($this->twig->render(
  64.             '@Framework/swagger.html.twig',
  65.             [
  66.                 'schemaUrl' => 'store-api.info.openapi3',
  67.                 'cspNonce' => $nonce,
  68.                 'apiType' => $apiType,
  69.             ]
  70.         ));
  71.         $cspTemplate $this->cspTemplates['administration'] ?? '';
  72.         $cspTemplate trim($cspTemplate);
  73.         if ($cspTemplate !== '') {
  74.             $csp str_replace('%nonce%'$nonce$cspTemplate);
  75.             $csp str_replace(["\n""\r"], ' '$csp);
  76.             $response->headers->set('Content-Security-Policy'$csp);
  77.         }
  78.         return $response;
  79.     }
  80. }