RewriteUrl.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\UrlRewrite\Model\StoreSwitcher;
  8. use Magento\Store\Api\Data\StoreInterface;
  9. use Magento\Store\Model\StoreSwitcherInterface;
  10. use Magento\UrlRewrite\Model\UrlFinderInterface;
  11. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  12. /**
  13. * Handle url rewrites for redirect url
  14. */
  15. class RewriteUrl implements StoreSwitcherInterface
  16. {
  17. /**
  18. * @var UrlFinderInterface
  19. */
  20. private $urlFinder;
  21. /**
  22. * @var \Magento\Framework\HTTP\PhpEnvironment\RequestFactory
  23. */
  24. private $requestFactory;
  25. /**
  26. * @param UrlFinderInterface $urlFinder
  27. * @param \Magento\Framework\HTTP\PhpEnvironment\RequestFactory $requestFactory
  28. */
  29. public function __construct(
  30. UrlFinderInterface $urlFinder,
  31. \Magento\Framework\HTTP\PhpEnvironment\RequestFactory $requestFactory
  32. ) {
  33. $this->urlFinder = $urlFinder;
  34. $this->requestFactory = $requestFactory;
  35. }
  36. /**
  37. * Switch to another store.
  38. *
  39. * @param StoreInterface $fromStore
  40. * @param StoreInterface $targetStore
  41. * @param string $redirectUrl
  42. * @return string
  43. */
  44. public function switch(StoreInterface $fromStore, StoreInterface $targetStore, string $redirectUrl): string
  45. {
  46. $targetUrl = $redirectUrl;
  47. /** @var \Magento\Framework\HTTP\PhpEnvironment\Request $request */
  48. $request = $this->requestFactory->create(['uri' => $targetUrl]);
  49. $urlPath = ltrim($request->getPathInfo(), '/');
  50. if ($targetStore->isUseStoreInUrl()) {
  51. // Remove store code in redirect url for correct rewrite search
  52. $storeCode = preg_quote($targetStore->getCode() . '/', '/');
  53. $pattern = "@^($storeCode)@";
  54. $urlPath = preg_replace($pattern, '', $urlPath);
  55. }
  56. $oldStoreId = $fromStore->getId();
  57. $oldRewrite = $this->urlFinder->findOneByData([
  58. UrlRewrite::REQUEST_PATH => $urlPath,
  59. UrlRewrite::STORE_ID => $oldStoreId,
  60. ]);
  61. if ($oldRewrite) {
  62. $targetUrl = $targetStore->getBaseUrl();
  63. // look for url rewrite match on the target store
  64. $currentRewrite = $this->urlFinder->findOneByData([
  65. UrlRewrite::TARGET_PATH => $oldRewrite->getTargetPath(),
  66. UrlRewrite::STORE_ID => $targetStore->getId(),
  67. ]);
  68. if ($currentRewrite) {
  69. $targetUrl .= $currentRewrite->getRequestPath();
  70. }
  71. } else {
  72. $existingRewrite = $this->urlFinder->findOneByData([
  73. UrlRewrite::REQUEST_PATH => $urlPath
  74. ]);
  75. if ($existingRewrite) {
  76. /** @var \Magento\Framework\App\Response\Http $response */
  77. $targetUrl = $targetStore->getBaseUrl();
  78. }
  79. }
  80. return $targetUrl;
  81. }
  82. }