Files
MageSail/Magesail/Model/AdminPathDetector.php
T
2026-03-20 22:51:40 -05:00

56 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace MageSail\Magesail\Model;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Request\Http;
/**
* Detects Magento admin URL path so tunnel store scope is not applied to backend requests.
*/
class AdminPathDetector
{
public function __construct(
private readonly DeploymentConfig $deploymentConfig
) {
}
public function getConfiguredAdminFrontName(): string
{
return (string) $this->deploymentConfig->get('backend/frontName', 'admin');
}
/**
* True when the request path contains the configured admin front name as a segment (any position),
* after skipping empty, index.php, and pub segments.
*/
public function isAdminPath(Http $request): bool
{
return $this->getAdminFrontNameIfPresentInPath($request) !== null;
}
public function getAdminFrontNameIfPresentInPath(Http $request): ?string
{
$path = trim((string) $request->getPathInfo(), '/');
if ($path === '') {
$uriPath = parse_url((string) $request->getRequestUri(), PHP_URL_PATH);
$path = trim((string) $uriPath, '/');
}
if ($path === '') {
return null;
}
$adminFront = $this->getConfiguredAdminFrontName();
foreach (explode('/', $path) as $segment) {
$lower = strtolower($segment);
if ($lower === '' || $lower === 'index.php' || $lower === 'pub') {
continue;
}
if (strcasecmp($segment, $adminFront) === 0) {
return $adminFront;
}
}
return null;
}
}