Files
MageSail/Magesail/Plugin/App/Request/StoreFromMageSailHeaderPlugin.php
T
2026-03-20 22:51:40 -05:00

61 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace MageSail\Magesail\Plugin\App\Request;
use Magento\Framework\App\Request\Http;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManager;
use MageSail\Magesail\Model\AdminPathDetector;
use MageSail\Magesail\Model\TunnelRequestDetector;
/**
* When request has X-MageSail-Store-Code header or magesail_store query param (from tunnel proxy),
* make getServerValue return it for MAGE_RUN_CODE and 'store' for MAGE_RUN_TYPE so Magento serves that store.
*
* Admin URLs must not use nginx tunnel map values: StoreResolver is built from MAGE_RUN_* init params; if they
* stay as the tunnel store, admin can 404. For admin paths we force website scope and empty run code so the
* default website is used (same as a normal request without per-host map).
*/
class StoreFromMageSailHeaderPlugin
{
public function __construct(
private readonly AdminPathDetector $adminPathDetector,
private readonly TunnelRequestDetector $tunnelRequestDetector
) {
}
/**
* Around getServerValue: if MageSail store header is set, return it for MAGE_RUN_CODE / MAGE_RUN_TYPE.
*
* @param Http $subject
* @param callable $proceed
* @param string|null $name
* @param mixed $default
* @return mixed
*/
public function aroundGetServerValue(Http $subject, callable $proceed, $name = null, $default = null)
{
if ($this->adminPathDetector->isAdminPath($subject)) {
if ($name === StoreManager::PARAM_RUN_TYPE) {
return ScopeInterface::SCOPE_WEBSITE;
}
if ($name === StoreManager::PARAM_RUN_CODE) {
return '';
}
return $proceed($name, $default);
}
$storeCode = $this->tunnelRequestDetector->getTunnelStoreCode($subject);
if ($storeCode === null) {
return $proceed($name, $default);
}
if ($name === StoreManager::PARAM_RUN_CODE) {
return $storeCode;
}
if ($name === StoreManager::PARAM_RUN_TYPE) {
return 'store';
}
return $proceed($name, $default);
}
}