Files
MageSail/Magesail/Plugin/Store/BaseUrlCheckerTunnelPlugin.php
T
2026-03-20 23:48:02 -05:00

77 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace MageSail\Magesail\Plugin\Store;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\BaseUrlChecker;
use MageSail\Magesail\Model\TunnelRequestDetector;
/**
* When request is a MageSail tunnel (header/query or hostname vs persisted tunnel URLs), skip base-URL 301.
*/
class BaseUrlCheckerTunnelPlugin
{
private const QUERY_PARAM_STORE = 'magesail_store';
private const SERVER_HEADER = 'HTTP_X_MAGESAIL_STORE_CODE';
public function __construct(
private readonly RequestInterface $request,
private readonly TunnelRequestDetector $tunnelRequestDetector
) {
}
/**
* Disable redirect-to-base when tunnel param is present so RequestPreprocessor skips the block.
*
* @param BaseUrlChecker $subject
* @return array|null [false] to disable, null to proceed
*/
public function beforeIsEnabled(BaseUrlChecker $subject): ?array
{
if ($this->request instanceof Http && $this->hasTunnelStore($this->request)) {
return [false];
}
return null;
}
/**
* If tunnel store indicator is present, treat URL as valid (skip redirect).
*
* @param BaseUrlChecker $subject
* @param callable $proceed
* @param array $uri
* @param Http $request
* @return bool
*/
public function aroundExecute(BaseUrlChecker $subject, callable $proceed, $uri, $request): bool
{
if ($request instanceof Http && $this->hasTunnelStore($request)) {
return true;
}
return $proceed($uri, $request);
}
private function hasTunnelStore(Http $request): bool
{
if ($this->tunnelRequestDetector->getTunnelStoreCode($request) !== null) {
return true;
}
return $this->hasTunnelStoreFromSuperglobals();
}
private function hasTunnelStoreFromSuperglobals(): bool
{
if (!empty($_GET[self::QUERY_PARAM_STORE]) && trim((string) $_GET[self::QUERY_PARAM_STORE]) !== '') {
return true;
}
$uri = $_SERVER['REQUEST_URI'] ?? '';
if ($uri !== '' && str_contains($uri, self::QUERY_PARAM_STORE . '=')) {
return true;
}
$h = $_SERVER[self::SERVER_HEADER] ?? '';
return $h !== '' && trim((string) $h) !== '';
}
}