Router.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Controller;
  7. use Magento\Framework\App\RequestInterface;
  8. use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
  9. use Magento\UrlRewrite\Model\UrlFinderInterface;
  10. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  11. use Magento\Framework\App\Request\Http as HttpRequest;
  12. use Magento\Framework\App\Response\Http as HttpResponse;
  13. use Magento\Framework\UrlInterface;
  14. use Magento\Framework\App\Action\Redirect;
  15. use Magento\Framework\App\ActionInterface;
  16. /**
  17. * UrlRewrite Controller Router
  18. *
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class Router implements \Magento\Framework\App\RouterInterface
  22. {
  23. /**
  24. * @var \Magento\Framework\App\ActionFactory
  25. */
  26. protected $actionFactory;
  27. /**
  28. * @var UrlInterface
  29. */
  30. protected $url;
  31. /**
  32. * @var \Magento\Store\Model\StoreManagerInterface
  33. */
  34. protected $storeManager;
  35. /**
  36. * @var HttpResponse
  37. */
  38. protected $response;
  39. /**
  40. * @var \Magento\UrlRewrite\Model\UrlFinderInterface
  41. */
  42. protected $urlFinder;
  43. /**
  44. * @param \Magento\Framework\App\ActionFactory $actionFactory
  45. * @param UrlInterface $url
  46. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  47. * @param \Magento\Framework\App\ResponseInterface $response
  48. * @param UrlFinderInterface $urlFinder
  49. */
  50. public function __construct(
  51. \Magento\Framework\App\ActionFactory $actionFactory,
  52. UrlInterface $url,
  53. \Magento\Store\Model\StoreManagerInterface $storeManager,
  54. \Magento\Framework\App\ResponseInterface $response,
  55. UrlFinderInterface $urlFinder
  56. ) {
  57. $this->actionFactory = $actionFactory;
  58. $this->url = $url;
  59. $this->storeManager = $storeManager;
  60. $this->response = $response;
  61. $this->urlFinder = $urlFinder;
  62. }
  63. /**
  64. * Match corresponding URL Rewrite and modify request.
  65. *
  66. * @param RequestInterface|HttpRequest $request
  67. *
  68. * @return ActionInterface|null
  69. */
  70. public function match(RequestInterface $request)
  71. {
  72. $rewrite = $this->getRewrite(
  73. $request->getPathInfo(),
  74. $this->storeManager->getStore()->getId()
  75. );
  76. if ($rewrite === null) {
  77. //No rewrite rule matching current URl found, continuing with
  78. //processing of this URL.
  79. return null;
  80. }
  81. if ($rewrite->getRedirectType()) {
  82. //Rule requires the request to be redirected to another URL
  83. //and cannot be processed further.
  84. return $this->processRedirect($request, $rewrite);
  85. }
  86. //Rule provides actual URL that can be processed by a controller.
  87. $request->setAlias(
  88. UrlInterface::REWRITE_REQUEST_PATH_ALIAS,
  89. $rewrite->getRequestPath()
  90. );
  91. $request->setPathInfo('/' . $rewrite->getTargetPath());
  92. return $this->actionFactory->create(
  93. \Magento\Framework\App\Action\Forward::class
  94. );
  95. }
  96. /**
  97. * @param RequestInterface $request
  98. * @param UrlRewrite $rewrite
  99. *
  100. * @return ActionInterface|null
  101. */
  102. protected function processRedirect($request, $rewrite)
  103. {
  104. $target = $rewrite->getTargetPath();
  105. if ($rewrite->getEntityType() !== Rewrite::ENTITY_TYPE_CUSTOM
  106. || ($prefix = substr($target, 0, 6)) !== 'http:/' && $prefix !== 'https:'
  107. ) {
  108. $target = $this->url->getUrl('', ['_direct' => $target]);
  109. }
  110. return $this->redirect($request, $target, $rewrite->getRedirectType());
  111. }
  112. /**
  113. * @param RequestInterface|HttpRequest $request
  114. * @param string $url
  115. * @param int $code
  116. * @return ActionInterface
  117. */
  118. protected function redirect($request, $url, $code)
  119. {
  120. $this->response->setRedirect($url, $code);
  121. $request->setDispatched(true);
  122. return $this->actionFactory->create(Redirect::class);
  123. }
  124. /**
  125. * @param string $requestPath
  126. * @param int $storeId
  127. * @return UrlRewrite|null
  128. */
  129. protected function getRewrite($requestPath, $storeId)
  130. {
  131. return $this->urlFinder->findOneByData([
  132. UrlRewrite::REQUEST_PATH => ltrim($requestPath, '/'),
  133. UrlRewrite::STORE_ID => $storeId,
  134. ]);
  135. }
  136. }