Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Mar 18, 2024
1 parent 8992f22 commit b9bb103
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 67 deletions.
48 changes: 48 additions & 0 deletions App/Request/PathInfoProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
*/
declare(strict_types=1);

namespace Opengento\StorePathUrl\App\Request;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Router\Base;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\App\Request\PathInfoProcessor as AppPathInfoProcessor;
use Magento\Store\App\Request\StorePathInfoValidator;
use Opengento\StorePathUrl\Service\PathResolver;

use function str_starts_with;
use function strlen;
use function substr;

class PathInfoProcessor extends AppPathInfoProcessor
{
public function __construct(
private StorePathInfoValidator $storePathInfoValidator,
private PathResolver $pathResolver,
private StoreRepositoryInterface $storeRepository
) {}

public function process(RequestInterface $request, $pathInfo): string
{
$storeCode = $this->storePathInfoValidator->getValidStoreCode($request, $pathInfo);
if (!empty($storeCode)) {
try {
$path = $this->pathResolver->resolve($this->storeRepository->getActiveStoreByCode($storeCode));
} catch (LocalizedException) {
return $pathInfo;
}
if (!$request->isDirectAccessFrontendName($path)) {
$pathInfo = substr($pathInfo, strlen($path) + (int)str_starts_with($pathInfo, '/')) ?: '/';
} else {
//no route in case we're trying to access a store that has the same code as a direct access
$request->setActionName(Base::NO_ROUTE);
}
}

return $pathInfo;
}
}
30 changes: 0 additions & 30 deletions Plugin/App/Request/PathInfo.php

This file was deleted.

36 changes: 36 additions & 0 deletions Plugin/App/Request/StorePathInfoValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
*/
declare(strict_types=1);

namespace Opengento\StorePathUrl\Plugin\App\Request;

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

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

public function beforeGetValidStoreCode(Subject $subject, Http $request, string $pathInfo = ''): array
{
if ($this->config->isEnabled()) {
$uri = $request->getUriString();
/** @var Store $store */
foreach ($this->storeRepository->getList() as $store) {
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) {
$pathInfo = $store->getCode();
}
}
}

return [$request, $pathInfo];
}
}
35 changes: 0 additions & 35 deletions Service/StorePathFixer.php

This file was deleted.

9 changes: 7 additions & 2 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
</argument>
</arguments>
</type>
<type name="Magento\Backend\App\Request\PathInfoProcessor">
<arguments>
<argument name="subject" xsi:type="object">Opengento\StorePathUrl\App\Request\PathInfoProcessor</argument>
</arguments>
</type>
<type name="Magento\Framework\Url\ScopeInterface">
<plugin name="Opengento_StorePathUrl::prepend_custom_path" type="Opengento\StorePathUrl\Plugin\Url\Scope"/>
</type>
<type name="Magento\Framework\App\Request\PathInfo">
<plugin name="Opengento_StorePathUrl::replace_country_store_code" type="Opengento\StorePathUrl\Plugin\App\Request\PathInfo"/>
<type name="Magento\Store\App\Request\StorePathInfoValidator">
<plugin name="Opengento_StorePathUrl::resolve_store_code" type="Opengento\StorePathUrl\Plugin\App\Request\StorePathInfoValidator"/>
</type>
</config>

0 comments on commit b9bb103

Please sign in to comment.