Skip to content

Commit b9bb103

Browse files
committed
Fix memory leak
1 parent 8992f22 commit b9bb103

File tree

5 files changed

+91
-67
lines changed

5 files changed

+91
-67
lines changed

App/Request/PathInfoProcessor.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
*/
5+
declare(strict_types=1);
6+
7+
namespace Opengento\StorePathUrl\App\Request;
8+
9+
use Magento\Framework\App\RequestInterface;
10+
use Magento\Framework\App\Router\Base;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Store\Api\StoreRepositoryInterface;
13+
use Magento\Store\App\Request\PathInfoProcessor as AppPathInfoProcessor;
14+
use Magento\Store\App\Request\StorePathInfoValidator;
15+
use Opengento\StorePathUrl\Service\PathResolver;
16+
17+
use function str_starts_with;
18+
use function strlen;
19+
use function substr;
20+
21+
class PathInfoProcessor extends AppPathInfoProcessor
22+
{
23+
public function __construct(
24+
private StorePathInfoValidator $storePathInfoValidator,
25+
private PathResolver $pathResolver,
26+
private StoreRepositoryInterface $storeRepository
27+
) {}
28+
29+
public function process(RequestInterface $request, $pathInfo): string
30+
{
31+
$storeCode = $this->storePathInfoValidator->getValidStoreCode($request, $pathInfo);
32+
if (!empty($storeCode)) {
33+
try {
34+
$path = $this->pathResolver->resolve($this->storeRepository->getActiveStoreByCode($storeCode));
35+
} catch (LocalizedException) {
36+
return $pathInfo;
37+
}
38+
if (!$request->isDirectAccessFrontendName($path)) {
39+
$pathInfo = substr($pathInfo, strlen($path) + (int)str_starts_with($pathInfo, '/')) ?: '/';
40+
} else {
41+
//no route in case we're trying to access a store that has the same code as a direct access
42+
$request->setActionName(Base::NO_ROUTE);
43+
}
44+
}
45+
46+
return $pathInfo;
47+
}
48+
}

Plugin/App/Request/PathInfo.php

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
*/
5+
declare(strict_types=1);
6+
7+
namespace Opengento\StorePathUrl\Plugin\App\Request;
8+
9+
use Magento\Framework\App\Request\Http;
10+
use Magento\Store\Api\StoreRepositoryInterface;
11+
use Magento\Store\App\Request\StorePathInfoValidator as Subject;
12+
use Magento\Store\Model\Store;
13+
use Opengento\StorePathUrl\Model\Config;
14+
15+
class StorePathInfoValidator
16+
{
17+
public function __construct(
18+
private Config $config,
19+
private StoreRepositoryInterface $storeRepository
20+
) {}
21+
22+
public function beforeGetValidStoreCode(Subject $subject, Http $request, string $pathInfo = ''): array
23+
{
24+
if ($this->config->isEnabled()) {
25+
$uri = $request->getUriString();
26+
/** @var Store $store */
27+
foreach ($this->storeRepository->getList() as $store) {
28+
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) {
29+
$pathInfo = $store->getCode();
30+
}
31+
}
32+
}
33+
34+
return [$request, $pathInfo];
35+
}
36+
}

Service/StorePathFixer.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

etc/di.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
</argument>
1616
</arguments>
1717
</type>
18+
<type name="Magento\Backend\App\Request\PathInfoProcessor">
19+
<arguments>
20+
<argument name="subject" xsi:type="object">Opengento\StorePathUrl\App\Request\PathInfoProcessor</argument>
21+
</arguments>
22+
</type>
1823
<type name="Magento\Framework\Url\ScopeInterface">
1924
<plugin name="Opengento_StorePathUrl::prepend_custom_path" type="Opengento\StorePathUrl\Plugin\Url\Scope"/>
2025
</type>
21-
<type name="Magento\Framework\App\Request\PathInfo">
22-
<plugin name="Opengento_StorePathUrl::replace_country_store_code" type="Opengento\StorePathUrl\Plugin\App\Request\PathInfo"/>
26+
<type name="Magento\Store\App\Request\StorePathInfoValidator">
27+
<plugin name="Opengento_StorePathUrl::resolve_store_code" type="Opengento\StorePathUrl\Plugin\App\Request\StorePathInfoValidator"/>
2328
</type>
2429
</config>

0 commit comments

Comments
 (0)