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 6.9KB

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