PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-webapi/Controller/Rest/Router/Route.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 244 lines | 111 code | 24 blank | 109 comment | 11 complexity | 69278080d6d3927d6d03feef7507e8d5 MD5 | raw file
  1. <?php
  2. /**
  3. * Route to services available via REST API.
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Webapi\Controller\Rest\Router;
  9. use Magento\Framework\App\RequestInterface as Request;
  10. use Magento\Framework\App\RouterInterface;
  11. class Route implements RouterInterface
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $serviceClass;
  17. /**
  18. * @var string
  19. */
  20. protected $serviceMethod;
  21. /**
  22. * @var boolean
  23. */
  24. protected $secure;
  25. /**
  26. * @var array
  27. */
  28. protected $aclResources = [];
  29. /**
  30. * @var array
  31. */
  32. protected $parameters = [];
  33. /**
  34. * @var array
  35. */
  36. protected $variables = [];
  37. /**
  38. * @var string
  39. */
  40. protected $route;
  41. /**
  42. * @param string $route
  43. */
  44. public function __construct($route = '')
  45. {
  46. $this->route = trim($route, '/');
  47. }
  48. /**
  49. * Split route by parts and variables
  50. *
  51. * @return array
  52. */
  53. protected function getRouteParts()
  54. {
  55. $result = [];
  56. $routeParts = explode('/', $this->route);
  57. foreach ($routeParts as $key => $value) {
  58. if ($this->isVariable($value)) {
  59. $this->variables[$key] = substr($value, 1);
  60. $value = null;
  61. }
  62. $result[$key] = $value;
  63. }
  64. return $result;
  65. }
  66. /**
  67. * Check if current route part is a name of variable
  68. *
  69. * @param string $value
  70. * @return bool
  71. */
  72. protected function isVariable($value)
  73. {
  74. if (substr($value, 0, 1) == ':'
  75. && substr($value, 1, 1) != ':') {
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * Retrieve unified requested path
  82. *
  83. * @param string $path
  84. * @return array
  85. */
  86. protected function getPathParts($path)
  87. {
  88. return explode('/', trim($path, '/'));
  89. }
  90. /**
  91. * Check if current route matches the requested path
  92. *
  93. * @param Request $request
  94. * @return array|bool
  95. */
  96. public function match(Request $request)
  97. {
  98. /** @var \Magento\Framework\Webapi\Rest\Request $request */
  99. $pathParts = $this->getPathParts($request->getPathInfo());
  100. $routeParts = $this->getRouteParts();
  101. if (count($pathParts) <> count($routeParts)) {
  102. return false;
  103. }
  104. $result = [];
  105. foreach ($pathParts as $key => $value) {
  106. if (!array_key_exists($key, $routeParts)) {
  107. return false;
  108. }
  109. $variable = isset($this->variables[$key]) ? $this->variables[$key] : null;
  110. if ($variable) {
  111. $result[$variable] = urldecode($pathParts[$key]);
  112. } else {
  113. if ($value != $routeParts[$key]) {
  114. return false;
  115. }
  116. }
  117. }
  118. return $result;
  119. }
  120. /**
  121. * Set service class.
  122. *
  123. * @param string $serviceClass
  124. * @return $this
  125. */
  126. public function setServiceClass($serviceClass)
  127. {
  128. $this->serviceClass = $serviceClass;
  129. return $this;
  130. }
  131. /**
  132. * Get service class.
  133. *
  134. * @return string
  135. */
  136. public function getServiceClass()
  137. {
  138. return $this->serviceClass;
  139. }
  140. /**
  141. * Set service method name.
  142. *
  143. * @param string $serviceMethod
  144. * @return $this
  145. */
  146. public function setServiceMethod($serviceMethod)
  147. {
  148. $this->serviceMethod = $serviceMethod;
  149. return $this;
  150. }
  151. /**
  152. * Get service method name.
  153. *
  154. * @return string
  155. */
  156. public function getServiceMethod()
  157. {
  158. return $this->serviceMethod;
  159. }
  160. /**
  161. * Set if the route is secure
  162. *
  163. * @param boolean $secure
  164. * @return $this
  165. */
  166. public function setSecure($secure)
  167. {
  168. $this->secure = $secure;
  169. return $this;
  170. }
  171. /**
  172. * Returns true if the route is secure
  173. *
  174. * @return boolean
  175. */
  176. public function isSecure()
  177. {
  178. return $this->secure;
  179. }
  180. /**
  181. * Set ACL resources list.
  182. *
  183. * @param array $aclResources
  184. * @return $this
  185. */
  186. public function setAclResources($aclResources)
  187. {
  188. $this->aclResources = $aclResources;
  189. return $this;
  190. }
  191. /**
  192. * Get ACL resources list.
  193. *
  194. * @return array
  195. */
  196. public function getAclResources()
  197. {
  198. return $this->aclResources;
  199. }
  200. /**
  201. * Set parameters list.
  202. *
  203. * @param array $parameters
  204. * @return $this
  205. */
  206. public function setParameters($parameters)
  207. {
  208. $this->parameters = $parameters;
  209. return $this;
  210. }
  211. /**
  212. * Get parameters list.
  213. *
  214. * @return array
  215. */
  216. public function getParameters()
  217. {
  218. return $this->parameters;
  219. }
  220. }