123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * Route to services available via REST API.
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Webapi\Controller\Rest\Router;
- use Magento\Framework\App\RequestInterface as Request;
- use Magento\Framework\App\RouterInterface;
- class Route implements RouterInterface
- {
- /**
- * @var string
- */
- protected $serviceClass;
- /**
- * @var string
- */
- protected $serviceMethod;
- /**
- * @var boolean
- */
- protected $secure;
- /**
- * @var array
- */
- protected $aclResources = [];
- /**
- * @var array
- */
- protected $parameters = [];
- /**
- * @var array
- */
- protected $variables = [];
- /**
- * @var string
- */
- protected $route;
- /**
- * @param string $route
- */
- public function __construct($route = '')
- {
- $this->route = trim($route, '/');
- }
- /**
- * Split route by parts and variables
- *
- * @return array
- */
- protected function getRouteParts()
- {
- $result = [];
- $routeParts = explode('/', $this->route);
- foreach ($routeParts as $key => $value) {
- if ($this->isVariable($value)) {
- $this->variables[$key] = substr($value, 1);
- $value = null;
- }
- $result[$key] = $value;
- }
- return $result;
- }
- /**
- * Check if current route part is a name of variable
- *
- * @param string $value
- * @return bool
- */
- protected function isVariable($value)
- {
- if (substr($value, 0, 1) == ':'
- && substr($value, 1, 1) != ':') {
- return true;
- }
- return false;
- }
- /**
- * Retrieve unified requested path
- *
- * @param string $path
- * @return array
- */
- protected function getPathParts($path)
- {
- return explode('/', trim($path, '/'));
- }
- /**
- * Check if current route matches the requested path
- *
- * @param Request $request
- * @return array|bool
- */
- public function match(Request $request)
- {
- /** @var \Magento\Framework\Webapi\Rest\Request $request */
- $pathParts = $this->getPathParts($request->getPathInfo());
- $routeParts = $this->getRouteParts();
- if (count($pathParts) <> count($routeParts)) {
- return false;
- }
- $result = [];
- foreach ($pathParts as $key => $value) {
- if (!array_key_exists($key, $routeParts)) {
- return false;
- }
- $variable = isset($this->variables[$key]) ? $this->variables[$key] : null;
- if ($variable) {
- $result[$variable] = urldecode($pathParts[$key]);
- } else {
- if ($value != $routeParts[$key]) {
- return false;
- }
- }
- }
- return $result;
- }
- /**
- * Set service class.
- *
- * @param string $serviceClass
- * @return $this
- */
- public function setServiceClass($serviceClass)
- {
- $this->serviceClass = $serviceClass;
- return $this;
- }
- /**
- * Get service class.
- *
- * @return string
- */
- public function getServiceClass()
- {
- return $this->serviceClass;
- }
- /**
- * Set service method name.
- *
- * @param string $serviceMethod
- * @return $this
- */
- public function setServiceMethod($serviceMethod)
- {
- $this->serviceMethod = $serviceMethod;
- return $this;
- }
- /**
- * Get service method name.
- *
- * @return string
- */
- public function getServiceMethod()
- {
- return $this->serviceMethod;
- }
- /**
- * Set if the route is secure
- *
- * @param boolean $secure
- * @return $this
- */
- public function setSecure($secure)
- {
- $this->secure = $secure;
- return $this;
- }
- /**
- * Returns true if the route is secure
- *
- * @return boolean
- */
- public function isSecure()
- {
- return $this->secure;
- }
- /**
- * Set ACL resources list.
- *
- * @param array $aclResources
- * @return $this
- */
- public function setAclResources($aclResources)
- {
- $this->aclResources = $aclResources;
- return $this;
- }
- /**
- * Get ACL resources list.
- *
- * @return array
- */
- public function getAclResources()
- {
- return $this->aclResources;
- }
- /**
- * Set parameters list.
- *
- * @param array $parameters
- * @return $this
- */
- public function setParameters($parameters)
- {
- $this->parameters = $parameters;
- return $this;
- }
- /**
- * Get parameters list.
- *
- * @return array
- */
- public function getParameters()
- {
- return $this->parameters;
- }
- /**
- * Get route path.
- *
- * @return string
- */
- public function getRoutePath()
- {
- return $this->route;
- }
- }
|