-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FRW-7936 Allow to provide store name as part of URL path. (#2529)
FRW-7936 Allow to provide store name as part of URL path.
- Loading branch information
1 parent
f8afe35
commit b1e57a2
Showing
12 changed files
with
386 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/SprykerShop/Shared/StorageRouter/StorageRouterConstants.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerShop\Shared\StorageRouter; | ||
|
||
/** | ||
* Declares global environment configuration keys. Do not use it for other class constants. | ||
*/ | ||
interface StorageRouterConstants | ||
{ | ||
/** | ||
* Specification: | ||
* - Returns true if the store routing is enabled. | ||
* | ||
* @api | ||
* | ||
* @var string | ||
*/ | ||
public const IS_STORE_ROUTING_ENABLED = 'STORAGE_ROUTER:IS_STORE_ROUTING_ENABLED'; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/SprykerShop/Shared/StorageRouter/Transfer/storage_router.transfer.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0"?> | ||
<transfers xmlns="spryker:transfer-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="spryker:transfer-01 http://static.spryker.com/transfer-01.xsd"> | ||
|
||
<transfer name="Store"> | ||
<property name="name" type="string"/> | ||
</transfer> | ||
|
||
</transfers> |
18 changes: 18 additions & 0 deletions
18
src/SprykerShop/Yves/StorageRouter/Dependency/Client/StorageRouterToStoreClientInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerShop\Yves\StorageRouter\Dependency\Client; | ||
|
||
use Generated\Shared\Transfer\StoreTransfer; | ||
|
||
interface StorageRouterToStoreClientInterface | ||
{ | ||
/** | ||
* @return \Generated\Shared\Transfer\StoreTransfer | ||
*/ | ||
public function getCurrentStore(): StoreTransfer; | ||
} |
34 changes: 34 additions & 0 deletions
34
...ykerShop/Yves/StorageRouter/Dependency/Client/StorageStorageRouterToStoreClientBridge.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerShop\Yves\StorageRouter\Dependency\Client; | ||
|
||
use Generated\Shared\Transfer\StoreTransfer; | ||
|
||
class StorageStorageRouterToStoreClientBridge implements StorageRouterToStoreClientInterface | ||
{ | ||
/** | ||
* @var \Spryker\Client\Store\StoreClientInterface | ||
*/ | ||
protected $storeClient; | ||
|
||
/** | ||
* @param \Spryker\Client\Store\StoreClientInterface $storeClient | ||
*/ | ||
public function __construct($storeClient) | ||
{ | ||
$this->storeClient = $storeClient; | ||
} | ||
|
||
/** | ||
* @return \Generated\Shared\Transfer\StoreTransfer | ||
*/ | ||
public function getCurrentStore(): StoreTransfer | ||
{ | ||
return $this->storeClient->getCurrentStore(); | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
...rShop/Yves/StorageRouter/Plugin/RouterEnhancer/StorePrefixStorageRouterEnhancerPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerShop\Yves\StorageRouter\Plugin\RouterEnhancer; | ||
|
||
use Spryker\Service\UtilText\Model\Url\Url; | ||
use Spryker\Yves\Kernel\AbstractPlugin; | ||
use SprykerShop\Yves\StorageRouter\Router\DynamicRouter; | ||
use SprykerShop\Yves\StorageRouterExtension\Dependency\Plugin\StorageRouterEnhancerPluginInterface; | ||
use Symfony\Component\Routing\RequestContext; | ||
|
||
/** | ||
* @method \SprykerShop\Yves\StorageRouter\StorageRouterConfig getConfig() | ||
* @method \SprykerShop\Yves\StorageRouter\StorageRouterFactory getFactory() | ||
*/ | ||
class StorePrefixStorageRouterEnhancerPlugin extends AbstractPlugin implements StorageRouterEnhancerPluginInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected const PARAMETER_STORE = 'store'; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
protected $currentStore; | ||
|
||
/** | ||
* @param string $pathinfo | ||
* @param \Symfony\Component\Routing\RequestContext $requestContext | ||
* | ||
* @return string | ||
*/ | ||
public function beforeMatch(string $pathinfo, RequestContext $requestContext): string | ||
{ | ||
if ($pathinfo === '/') { | ||
return $pathinfo; | ||
} | ||
|
||
$pathinfoFragments = explode('/', trim($pathinfo, '/')); | ||
if (in_array($pathinfoFragments[0], $this->getConfig()->getAllowedStores(), true)) { | ||
$this->currentStore = array_shift($pathinfoFragments); | ||
|
||
return '/' . implode('/', $pathinfoFragments); | ||
} | ||
|
||
return $pathinfo; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $parameters | ||
* @param \Symfony\Component\Routing\RequestContext $requestContext | ||
* | ||
* @return array<mixed> | ||
*/ | ||
public function afterMatch(array $parameters, RequestContext $requestContext): array | ||
{ | ||
if ($this->currentStore !== null) { | ||
$parameters[static::PARAMETER_STORE] = $this->currentStore; | ||
} | ||
|
||
return $parameters; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param \Symfony\Component\Routing\RequestContext $requestContext | ||
* @param int $referenceType | ||
* | ||
* @return string | ||
*/ | ||
public function afterGenerate(string $url, RequestContext $requestContext, int $referenceType): string | ||
{ | ||
$store = $this->findStore($requestContext); | ||
|
||
if ($store !== null) { | ||
return $this->buildUrlWithStore($url, $store, $referenceType); | ||
} | ||
|
||
return $url; | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\Routing\RequestContext $requestContext | ||
* | ||
* @return string|null | ||
*/ | ||
protected function findStore(RequestContext $requestContext): ?string | ||
{ | ||
return $requestContext->hasParameter(static::PARAMETER_STORE) && $requestContext->getParameter(static::PARAMETER_STORE) !== null | ||
? $requestContext->getParameter(static::PARAMETER_STORE) | ||
: ($this->getConfig()->isStoreRoutingEnabled() | ||
? $this->getFactory()->getStoreClient()->getCurrentStore()->getNameOrFail() | ||
: null); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param string $store | ||
* @param int $referenceType | ||
* | ||
* @return string | ||
*/ | ||
protected function buildUrlWithStore(string $url, string $store, int $referenceType): string | ||
{ | ||
if ($url === '/') { | ||
$url = ''; | ||
} | ||
|
||
if ($referenceType === DynamicRouter::ABSOLUTE_PATH) { | ||
return sprintf('/%s%s', $store, $url); | ||
} | ||
|
||
if ($referenceType === DynamicRouter::ABSOLUTE_URL) { | ||
$parsedUrl = Url::parse($url); | ||
$pathWithStore = sprintf('/%s%s', $store, $parsedUrl->getPath()); | ||
$parsedUrl->setPath($pathWithStore); | ||
|
||
return (string)$parsedUrl; | ||
} | ||
|
||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.