Router.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Router for Magento web API.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Webapi\Controller\Rest;
  9. use \Magento\Framework\Webapi\Rest\Request;
  10. class Router
  11. {
  12. /**
  13. * @var array
  14. */
  15. protected $_routes = [];
  16. /**
  17. * @var \Magento\Webapi\Model\Rest\Config
  18. */
  19. protected $_apiConfig;
  20. /**
  21. * Initialize dependencies.
  22. *
  23. * @param \Magento\Webapi\Model\Rest\Config $apiConfig
  24. */
  25. public function __construct(\Magento\Webapi\Model\Rest\Config $apiConfig)
  26. {
  27. $this->_apiConfig = $apiConfig;
  28. }
  29. /**
  30. * Route the Request, the only responsibility of the class.
  31. * Find route that matches current URL, set parameters of the route to Request object.
  32. *
  33. * @param Request $request
  34. * @return \Magento\Webapi\Controller\Rest\Router\Route
  35. * @throws \Magento\Framework\Webapi\Exception
  36. */
  37. public function match(Request $request)
  38. {
  39. /** @var \Magento\Webapi\Controller\Rest\Router\Route[] $routes */
  40. $routes = $this->_apiConfig->getRestRoutes($request);
  41. $matched = [];
  42. foreach ($routes as $route) {
  43. $params = $route->match($request);
  44. if ($params !== false) {
  45. $request->setParams($params);
  46. $matched[] = $route;
  47. }
  48. }
  49. if (!empty($matched)) {
  50. return array_pop($matched);
  51. }
  52. throw new \Magento\Framework\Webapi\Exception(
  53. __('Request does not match any route.'),
  54. 0,
  55. \Magento\Framework\Webapi\Exception::HTTP_NOT_FOUND
  56. );
  57. }
  58. }