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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. $typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
  60. }
  61. catch (LuDboDeserializeException $e)
  62. {
  63. throw $e;
  64. }
  65. catch (\Exception $e)
  66. {
  67. LuLog::log($e);
  68. LuBusiness::badInput("Unable to parse JSON value for '" . $param->getName() . "'");
  69. }
  70. }
  71. return $typedValue;
  72. }
  73. private function getOptions($httpMethod, $url, $controller, $method, $permissions, $middleware)
  74. {
  75. if (!is_array($permissions)) {
  76. $permissions = array($permissions);
  77. }
  78. if (!is_array($middleware)) {
  79. $middleware = array($middleware);
  80. }
  81. $permissions_string = implode(",", $permissions);
  82. $middleware_string = [];
  83. foreach (array_merge($this->middleware, $middleware) as $mid) {
  84. $middleware_string[] = $mid . (strpos($mid, ":") !== false ? "," : ":") . $permissions_string;
  85. }
  86. if (strpos($controller, "\\") === false) {
  87. $controller = "App\\Http\\Controller\\" . $controller . "Controller";
  88. }
  89. $route = new LuRouteDbo();
  90. $route->setUrl($url);
  91. $route->setBusinessClass($controller);
  92. $route->setBusinessMethod($method);
  93. $route->setMiddlware($middleware_string);
  94. $route->setPermissions($permissions);
  95. $route->setMethod($httpMethod);
  96. $this->routes[] = $route;
  97. $ctrl = $this;
  98. return [function() use($controller, $method, $ctrl)
  99. {
  100. $reflect = new \ReflectionMethod($controller, $method);
  101. $params = $reflect->getParameters();
  102. $doc = new LuDocParser($reflect->getDocComment());
  103. $doc->parse();
  104. $args = array();
  105. foreach ($params as $param) {
  106. try {
  107. if ($param->isOptional()) {
  108. $value = null;
  109. if (LuBusiness::hasParam([$param->getName()])) {
  110. $value = $ctrl->getParam($param, LuBusiness::getParam($param->getName()));
  111. }
  112. else {
  113. $value = $param->getDefaultValue();
  114. }
  115. $args[$param->getName()] = $value;
  116. }
  117. else {
  118. $args[$param->getName()] = $ctrl->getParam($param, LuBusiness::checkParam($param->getName()));
  119. }
  120. if (array_key_exists($param->getName(), $doc->getParams())) {
  121. foreach ($doc->getParams()[$param->getName()]->getConstraints() as $constraint) {
  122. call_user_func_array([$args[$param->getName()], $constraint->getMethod()],
  123. $constraint->getArguments());
  124. }
  125. }
  126. }
  127. catch (LuDboConstraintException $e)
  128. {
  129. $paramName = $param->getName();
  130. LuBusiness::badInput("Invalid value for '${paramName}': " . $e->getMessage());
  131. }
  132. }
  133. $controllerInstance = new $controller();
  134. $result = call_user_func_array(array($controllerInstance, $method), $args);
  135. return LuOutputFormatter::formatSuccess($result);
  136. }, 'middleware' => $middleware_string];
  137. }
  138. /**
  139. * @param $url string
  140. * @param $controller string
  141. * @param $method string
  142. * @param array $permissions string|string[]
  143. * @param array $middleware string|string[]
  144. * @return mixed
  145. */
  146. public function get($url, $controller, $method, $permissions = array(), $middleware = array())
  147. {
  148. global $app;
  149. return $app->get($url, $this->getOptions("GET", $url, $controller, $method, $permissions, $middleware));
  150. }
  151. /**
  152. * @param $url string
  153. * @param $controller string
  154. * @param $method string
  155. * @param array $permissions string|string[]
  156. * @param array $middleware string|string[]
  157. * @return mixed
  158. */
  159. public function post($url, $controller, $method, $permissions = array(), $middleware = array())
  160. {
  161. global $app;
  162. return $app->post($url, $this->getOptions("POST", $url, $controller, $method, $permissions, $middleware));
  163. }
  164. /**
  165. * @param $url string
  166. * @param $controller string
  167. * @param $method string
  168. * @param array $permissions string|string[]
  169. * @param array $middleware string|string[]
  170. * @return mixed
  171. */
  172. public function put($url, $controller, $method, $permissions = array(), $middleware = array())
  173. {
  174. global $app;
  175. return $app->put($url, $this->getOptions("PUT", $url, $controller, $method, $permissions, $middleware));
  176. }
  177. /**
  178. * @param $url string
  179. * @param $controller string
  180. * @param $method string
  181. * @param array $permissions string|string[]
  182. * @param array $middleware string|string[]
  183. * @return mixed
  184. */
  185. public function delete($url, $controller, $method, $permissions = array(), $middleware = array())
  186. {
  187. global $app;
  188. return $app->delete($url, $this->getOptions("DELETE", $url, $controller, $method, $permissions, $middleware));
  189. }
  190. }