<?php
declare(strict_types=1);
namespace She\AdminUrl\Core\Framework\Routing;
use Shopware\Administration\Framework\Routing\AdministrationRouteScope;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\RequestContextResolverInterface;
use Shopware\Core\Framework\Routing\SalesChannelRequestContextResolver as InnerService;
use Shopware\Core\PlatformRequest;
use Symfony\Component\HttpFoundation\Request;
class ApiRequestContextResolver implements RequestContextResolverInterface
{
private InnerService $innerService;
public function __construct(InnerService $innerService)
{
$this->innerService = $innerService;
}
public function resolve(Request $request): void
{
$routeScope = $request->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE);
if (!$routeScope instanceof RouteScope || !\in_array(
AdministrationRouteScope::ID,
$routeScope->getScopes(),
true
)) {
$this->innerService->resolve($request);
return;
}
$scopes = $routeScope->getScopes();
$scopes[] = 'api';
$routeScope->setScopes($scopes);
$request->attributes->set(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, $routeScope);
$this->innerService->resolve($request);
}
}