11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2011 Adobe
4+ * All Rights Reserved .
55 */
6+
67declare (strict_types=1 );
78
89namespace Magento \Store \App \Request ;
1112use Magento \Framework \App \Request \Http ;
1213use Magento \Framework \App \Request \PathInfo ;
1314use Magento \Framework \Exception \NoSuchEntityException ;
15+ use Magento \Framework \ObjectManager \ResetAfterRequestInterface ;
1416use Magento \Store \Api \StoreRepositoryInterface ;
1517use Magento \Store \Model \Store ;
1618use Magento \Store \Model \StoreIsInactiveException ;
1921/**
2022 * Gets the store from the path if valid
2123 */
22- class StorePathInfoValidator
24+ class StorePathInfoValidator implements ResetAfterRequestInterface
2325{
2426 /**
2527 * Store Config
@@ -43,6 +45,11 @@ class StorePathInfoValidator
4345 */
4446 private $ storeCodeValidator ;
4547
48+ /**
49+ * @var array
50+ */
51+ private array $ validatedStoreCodes = [];
52+
4653 /**
4754 * @param ScopeConfigInterface $config
4855 * @param StoreRepositoryInterface $storeRepository
@@ -79,17 +86,25 @@ public function getValidStoreCode(Http $request, string $pathInfo = '') : ?strin
7986 $ pathInfo = $ this ->pathInfo ->getPathInfo ($ request ->getRequestUri (), $ request ->getBaseUrl ());
8087 }
8188 $ storeCode = $ this ->getStoreCode ($ pathInfo );
89+
8290 if (empty ($ storeCode ) || $ storeCode === Store::ADMIN_CODE || !$ this ->storeCodeValidator ->isValid ($ storeCode )) {
8391 return null ;
8492 }
8593
94+ if (array_key_exists ($ storeCode , $ this ->validatedStoreCodes )) {
95+ return $ this ->validatedStoreCodes [$ storeCode ];
96+ }
97+
8698 try {
8799 $ this ->storeRepository ->getActiveStoreByCode ($ storeCode );
88100
101+ $ this ->validatedStoreCodes [$ storeCode ] = $ storeCode ;
89102 return $ storeCode ;
90103 } catch (NoSuchEntityException $ e ) {
104+ $ this ->validatedStoreCodes [$ storeCode ] = null ;
91105 return null ;
92106 } catch (StoreIsInactiveException $ e ) {
107+ $ this ->validatedStoreCodes [$ storeCode ] = null ;
93108 return null ;
94109 }
95110 }
@@ -105,4 +120,12 @@ private function getStoreCode(string $pathInfo) : string
105120 $ pathParts = explode ('/ ' , ltrim ($ pathInfo , '/ ' ), 2 );
106121 return current ($ pathParts );
107122 }
123+
124+ /**
125+ * @inheritDoc
126+ */
127+ public function _resetState (): void
128+ {
129+ $ this ->validatedStoreCodes = [];
130+ }
108131}
0 commit comments