Skip to content

Commit

Permalink
Setup MAGE_RUN_CODE & MAGE_RUN_TYPE are not necessary anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Mar 27, 2024
1 parent 807354b commit 723229b
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions Plugin/App/Request/StorePathInfoValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
namespace Opengento\StorePathUrl\Plugin\App\Request;

use Magento\Framework\App\Request\Http;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\App\Request\StorePathInfoValidator as Subject;
use Magento\Store\Model\Store;
use Opengento\StorePathUrl\Model\Config;

use function explode;
use function parse_url;
use function strtok;
use function trim;

use const PHP_URL_PATH;

class StorePathInfoValidator
{
Expand All @@ -23,16 +30,52 @@ public function __construct(

public function beforeGetValidStoreCode(Subject $subject, Http $request, string $pathInfo = ''): array
{
if ($pathInfo !== '' && $this->config->isEnabled()) {
if ($this->config->isEnabled()) {
$uri = explode('?', $request->getUriString())[0] . '/';
/** @var Store $store */
foreach ($this->storeRepository->getList() as $store) {
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) {
$pathInfo = $store->getCode();
}
if ($pathInfo === '') {
$pathInfo = strtok(trim(parse_url($uri, PHP_URL_PATH), '/'), '/');
}
$pathInfo = $pathInfo === false ? $this->resolveByWebUrl($uri) : $this->resolveByLinkUrl($uri, $pathInfo);
}

return [$request, $pathInfo];
}

private function resolveByLinkUrl(string $uri, string $pathInfo): string
{
/** @var Store $store */
foreach ($this->storeRepository->getList() as $store) {
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) {
$pathInfo = $store->getCode();
}
}

return $pathInfo;
}

private function resolveByWebUrl(string $uri): string
{
$matches = [];

/** @var Store $store */
foreach ($this->storeRepository->getList() as $store) {
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl(UrlInterface::URL_TYPE_WEB))) {
try {
$website = $store->getWebsite();
if ($website->getIsDefault()) {
if ($store->isDefault()) {
return $store->getCode();
}
$matches[0] = $store->getCode();
} elseif ($store->isDefault()) {
$matches[1] = $store->getCode();
} else {
$matches[2] = $store->getCode();
}
} catch (NoSuchEntityException) {}
}
}

return $matches[0] ?? $matches[1] ?? $matches[2] ?? '';
}
}

0 comments on commit 723229b

Please sign in to comment.