Skip to content

Commit

Permalink
Prevent recursion over plugin and improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 authored May 22, 2024
1 parent 994cb7f commit 67312b1
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions Plugin/App/Request/StorePathInfoValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,39 @@

class StorePathInfoValidator
{
private int $stack = 0;

public function __construct(
private Config $config,
private StoreRepositoryInterface $storeRepository
) {}

public function beforeGetValidStoreCode(Subject $subject, Http $request, string $pathInfo = ''): array
{
if ($this->config->isBaseUrlResolverEnabled()) {
$originalPathInfo = $pathInfo;
$uri = strtok($request->getUriString(), '?') . '/';
if ($uri !== false) {
if ($pathInfo === '') {
$pathInfo = parse_url($uri, PHP_URL_PATH);
if ($pathInfo === false) {
return [$request, $originalPathInfo];
}
$pathInfo = strtok($pathInfo, '/');
if (++$this->stack === 1 && $this->config->isBaseUrlResolverEnabled()) {
$storeCode = $this->resolveStoreCode($pathInfo);
$pathInfo = $storeCode === '' ? $pathInfo : $storeCode;
}
$this->stack--;

return [$request, $pathInfo];
}

private function resolveStoreCode(string $pathInfo): string
{
$uri = strtok($request->getUriString(), '?') . '/';
if ($uri !== false) {
if ($pathInfo === '') {
$pathInfo = parse_url($uri, PHP_URL_PATH);
if ($pathInfo === false) {
return '';
}
$pathInfo = $pathInfo === false ? $this->resolveByWebUrl($uri) : $this->resolveByLinkUrl($uri);
$pathInfo = $pathInfo === '' ? $originalPathInfo : $pathInfo;
$pathInfo = strtok($pathInfo, '/');
}
$pathInfo = $pathInfo === false ? $this->resolveByWebUrl($uri) : $this->resolveByLinkUrl($uri);
}

return [$request, $pathInfo];
return $pathInfo;
}

private function resolveByLinkUrl(string $uri): string
Expand Down

0 comments on commit 67312b1

Please sign in to comment.