PathInfoProcessor.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Store\App\Request;
  8. /**
  9. * Processes the path and looks for the store in the url and removes it and modifies the path accordingly.
  10. */
  11. class PathInfoProcessor implements \Magento\Framework\App\Request\PathInfoProcessorInterface
  12. {
  13. /**
  14. * @var StorePathInfoValidator
  15. */
  16. private $storePathInfoValidator;
  17. /**
  18. * @var \Magento\Framework\App\Config\ReinitableConfigInterface
  19. */
  20. private $config;
  21. /**
  22. * @param \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator
  23. * @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
  24. */
  25. public function __construct(
  26. \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator,
  27. \Magento\Framework\App\Config\ReinitableConfigInterface $config
  28. ) {
  29. $this->storePathInfoValidator = $storePathInfoValidator;
  30. $this->config = $config;
  31. }
  32. /**
  33. * Process path info and remove store from pathInfo.
  34. *
  35. * This method also sets request to no route if store is not valid and store is present in url config is enabled
  36. *
  37. * @param \Magento\Framework\App\RequestInterface $request
  38. * @param string $pathInfo
  39. * @return string
  40. */
  41. public function process(\Magento\Framework\App\RequestInterface $request, $pathInfo) : string
  42. {
  43. //can store code be used in url
  44. if ((bool)$this->config->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL)) {
  45. $storeCode = $this->storePathInfoValidator->getValidStoreCode($request, $pathInfo);
  46. if (!empty($storeCode)) {
  47. if (!$request->isDirectAccessFrontendName($storeCode)) {
  48. $pathInfo = $this->trimStoreCodeFromPathInfo($pathInfo, $storeCode);
  49. } else {
  50. //no route in case we're trying to access a store that has the same code as a direct access
  51. $request->setActionName(\Magento\Framework\App\Router\Base::NO_ROUTE);
  52. }
  53. }
  54. }
  55. return $pathInfo;
  56. }
  57. /**
  58. * Trim store code from path info string if exists
  59. *
  60. * @param string $pathInfo
  61. * @param string $storeCode
  62. * @return string
  63. */
  64. private function trimStoreCodeFromPathInfo(string $pathInfo, string $storeCode) : ?string
  65. {
  66. if (substr($pathInfo, 0, strlen('/' . $storeCode)) == '/'. $storeCode) {
  67. $pathInfo = substr($pathInfo, strlen($storeCode)+1);
  68. }
  69. return empty($pathInfo) ? '/' : $pathInfo;
  70. }
  71. }