You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuRoute.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Luticate\Utils;
  3. class LuRoute {
  4. const REG_UINT = "[0-9]+";
  5. const REG_INT = "\\-?" . self::REG_UINT;
  6. const REG_BOOL = "true|false";
  7. /**
  8. * @var string[]
  9. */
  10. private $middleware = array('Luticate\Utils\ParametersMiddleware');
  11. /**
  12. * @var LuRouteDbo[]
  13. */
  14. private $routes = array();
  15. /**
  16. * @var LuRoute
  17. */
  18. private static $instance = null;
  19. private function __construct()
  20. {
  21. }
  22. public static function getInstance()
  23. {
  24. if (is_null(self::$instance)) {
  25. self::$instance = new LuRoute();
  26. }
  27. return self::$instance;
  28. }
  29. public function getRoutes()
  30. {
  31. return $this->routes;
  32. }
  33. /**
  34. * @param $middleware string
  35. */
  36. public function addMiddleware($middleware)
  37. {
  38. $this->middleware[] = $middleware;
  39. }
  40. private function getClassName(\ReflectionParameter $param) {
  41. preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
  42. return isset($matches[1]) ? $matches[1] : null;
  43. }
  44. private function getParam(\ReflectionParameter $param, $value)
  45. {
  46. $typedValue = null;
  47. $className = $this->getClassName($param);
  48. if (is_null($className)) {
  49. $typedValue = $value;
  50. }
  51. else {
  52. $class = $param->getClass();
  53. try
  54. {
  55. $json = json_decode($value, true);
  56. $typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
  57. }
  58. catch (\Exception $e)
  59. {
  60. LuLog::log($e);
  61. LuBusiness::badInput("Unable to parse JSON value for " . $param->getName());
  62. }
  63. }
  64. return $typedValue;
  65. }
  66. private function getOptions($httpMethod, $url, $controller, $method, $permissions, $middleware)
  67. {
  68. if (!is_array($permissions)) {
  69. $permissions = array($permissions);
  70. }
  71. if (!is_array($middleware)) {
  72. $middleware = array($middleware);
  73. }
  74. $permissions_string = implode(",", $permissions);
  75. $middleware_string = [];
  76. foreach (array_merge($this->middleware, $middleware) as $mid) {
  77. $middleware_string[] = $mid . (strpos($mid, ":") !== false ? "," : ":") . $permissions_string;
  78. }
  79. if (strpos($controller, "\\") === false) {
  80. $controller = "App\\Http\\Controller\\" . $controller . "Controller";
  81. }
  82. $route = new LuRouteDbo();
  83. $route->setUrl($url);
  84. $route->setBusinessClass($controller);
  85. $route->setBusinessMethod($method);
  86. $route->setMiddlware($middleware_string);
  87. $route->setPermissions($permissions);
  88. $route->setMethod($httpMethod);
  89. $this->routes[] = $route;
  90. $ctrl = $this;
  91. return [function() use($controller, $method, $ctrl)
  92. {
  93. $reflect = new \ReflectionMethod($controller, $method);
  94. $params = $reflect->getParameters();
  95. $args = array();
  96. foreach ($params as $param) {
  97. if ($param->isOptional()) {
  98. $value = null;
  99. if (LuBusiness::hasParam([$param->getName()])) {
  100. $value = $ctrl->getParam($param, LuBusiness::getParam($param->getName()));
  101. }
  102. else {
  103. $value = $param->getDefaultValue();
  104. }
  105. $args[$param->getName()] = $value;
  106. }
  107. else {
  108. $args[$param->getName()] = $ctrl->getParam($param, LuBusiness::checkParam($param->getName()));
  109. }
  110. }
  111. $controllerInstance = new $controller();
  112. $result = call_user_func_array(array($controllerInstance, $method), $args);
  113. return LuOutputFormatter::formatSuccess($result);
  114. }, 'middleware' => $middleware_string];
  115. }
  116. /**
  117. * @param $url string
  118. * @param $controller string
  119. * @param $method string
  120. * @param array $permissions string|string[]
  121. * @param array $middleware string|string[]
  122. * @return mixed
  123. */
  124. public function get($url, $controller, $method, $permissions = array(), $middleware = array())
  125. {
  126. global $app;
  127. return $app->get($url, $this->getOptions("GET", $url, $controller, $method, $permissions, $middleware));
  128. }
  129. /**
  130. * @param $url string
  131. * @param $controller string
  132. * @param $method string
  133. * @param array $permissions string|string[]
  134. * @param array $middleware string|string[]
  135. * @return mixed
  136. */
  137. public function post($url, $controller, $method, $permissions = array(), $middleware = array())
  138. {
  139. global $app;
  140. return $app->post($url, $this->getOptions("POST", $url, $controller, $method, $permissions, $middleware));
  141. }
  142. /**
  143. * @param $url string
  144. * @param $controller string
  145. * @param $method string
  146. * @param array $permissions string|string[]
  147. * @param array $middleware string|string[]
  148. * @return mixed
  149. */
  150. public function put($url, $controller, $method, $permissions = array(), $middleware = array())
  151. {
  152. global $app;
  153. return $app->put($url, $this->getOptions("PUT", $url, $controller, $method, $permissions, $middleware));
  154. }
  155. /**
  156. * @param $url string
  157. * @param $controller string
  158. * @param $method string
  159. * @param array $permissions string|string[]
  160. * @param array $middleware string|string[]
  161. * @return mixed
  162. */
  163. public function delete($url, $controller, $method, $permissions = array(), $middleware = array())
  164. {
  165. global $app;
  166. return $app->delete($url, $this->getOptions("DELETE", $url, $controller, $method, $permissions, $middleware));
  167. }
  168. }