PathProcessor.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Controller;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. /**
  9. * Class PathProcessor
  10. */
  11. class PathProcessor
  12. {
  13. /** Store code alias to indicate that all stores should be affected by action */
  14. const ALL_STORE_CODE = 'all';
  15. /**
  16. * @var \Magento\Store\Model\StoreManagerInterface
  17. */
  18. private $storeManager;
  19. /**
  20. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  21. */
  22. public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
  23. {
  24. $this->storeManager = $storeManager;
  25. }
  26. /**
  27. * Process path
  28. *
  29. * @param string $pathInfo
  30. * @return array
  31. */
  32. private function stripPathBeforeStorecode($pathInfo)
  33. {
  34. $pathParts = explode('/', trim($pathInfo, '/'));
  35. array_shift($pathParts);
  36. $path = '/' . implode('/', $pathParts);
  37. return explode('/', ltrim($path, '/'), 2);
  38. }
  39. /**
  40. * Process path info
  41. *
  42. * @param string $pathInfo
  43. * @return string
  44. * @throws NoSuchEntityException
  45. */
  46. public function process($pathInfo)
  47. {
  48. $pathParts = $this->stripPathBeforeStorecode($pathInfo);
  49. $storeCode = current($pathParts);
  50. $stores = $this->storeManager->getStores(false, true);
  51. if (isset($stores[$storeCode])) {
  52. $this->storeManager->setCurrentStore($storeCode);
  53. $path = '/' . (isset($pathParts[1]) ? $pathParts[1] : '');
  54. } elseif ($storeCode === self::ALL_STORE_CODE) {
  55. $this->storeManager->setCurrentStore(\Magento\Store\Model\Store::ADMIN_CODE);
  56. $path = '/' . (isset($pathParts[1]) ? $pathParts[1] : '');
  57. } else {
  58. $path = '/' . implode('/', $pathParts);
  59. }
  60. return $path;
  61. }
  62. }