ControllerRest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\WebapiAsync\Plugin;
  8. use Magento\WebapiAsync\Model\ServiceConfig;
  9. use Magento\Webapi\Controller\PathProcessor;
  10. use Magento\Framework\App\RequestInterface;
  11. use Magento\WebapiAsync\Model\ServiceConfig\Converter;
  12. class ControllerRest
  13. {
  14. /**
  15. * @var ServiceConfig
  16. */
  17. private $serviceConfig;
  18. /**
  19. * @var PathProcessor
  20. */
  21. private $pathProcessor;
  22. /**
  23. * ControllerRest constructor.
  24. *
  25. * @param ServiceConfig $serviceConfig
  26. * @param PathProcessor $pathProcessor
  27. */
  28. public function __construct(
  29. ServiceConfig $serviceConfig,
  30. PathProcessor $pathProcessor
  31. ) {
  32. $this->serviceConfig = $serviceConfig;
  33. $this->pathProcessor = $pathProcessor;
  34. }
  35. /**
  36. * Apply route customization.
  37. * @param \Magento\Webapi\Controller\Rest $subject
  38. * @param RequestInterface $request
  39. * @return array
  40. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  41. */
  42. public function beforeDispatch(\Magento\Webapi\Controller\Rest $subject, RequestInterface $request)
  43. {
  44. $routeCustomizations = $this->serviceConfig->getServices()[Converter::KEY_ROUTES] ?? [];
  45. if ($routeCustomizations) {
  46. $originPath = $request->getPathInfo();
  47. $requestMethod = $request->getMethod();
  48. $routePath = ltrim($this->pathProcessor->process($originPath), '/');
  49. if (array_key_exists($routePath, $routeCustomizations)) {
  50. if (isset($routeCustomizations[$routePath][$requestMethod])) {
  51. $path = ltrim($routeCustomizations[$routePath][$requestMethod], '/');
  52. $request->setPathInfo(str_replace($routePath, $path, $originPath));
  53. }
  54. }
  55. }
  56. return [$request];
  57. }
  58. }