| @@ -6,73 +6,73 @@ discard block | ||
| 6 | 6 | |
| 7 | 7 | class Route | 
| 8 | 8 |  { | 
| 9 | - const NEXT = '_NEXT_'; | |
| 9 | + const NEXT = '_NEXT_'; | |
| 10 | 10 | |
| 11 | - private $app; | |
| 11 | + private $app; | |
| 12 | 12 | |
| 13 | - private $routes = []; | |
| 13 | + private $routes = []; | |
| 14 | 14 | |
| 15 | - public $current = []; | |
| 15 | + public $current = []; | |
| 16 | 16 | |
| 17 | - private $prefix; | |
| 17 | + private $prefix; | |
| 18 | 18 | |
| 19 | - private $basController; | |
| 19 | + private $basController; | |
| 20 | 20 | |
| 21 | - private $middlewareFrom; | |
| 21 | + private $middlewareFrom; | |
| 22 | 22 | |
| 23 | - private $groupMiddleware = []; | |
| 23 | + private $groupMiddleware = []; | |
| 24 | 24 | |
| 25 | - public function __construct(Application $app) | |
| 26 | -  { | |
| 25 | + public function __construct(Application $app) | |
| 26 | +    { | |
| 27 | 27 | $this->app = $app; | 
| 28 | - } | |
| 28 | + } | |
| 29 | 29 | |
| 30 | - public function add($url, $action, $requestMethos = 'GET', $middleware = []) | |
| 31 | -  { | |
| 30 | + public function add($url, $action, $requestMethos = 'GET', $middleware = []) | |
| 31 | +    { | |
| 32 | 32 | |
| 33 | 33 |      if ($this->prefix) { | 
| 34 | 34 | |
| 35 | -      if ($this->prefix !== '/') { | |
| 35 | +        if ($this->prefix !== '/') { | |
| 36 | 36 | |
| 37 | 37 | $url = $this->prefix . $url; | 
| 38 | 38 | |
| 39 | 39 | $url = rtrim($url, '/'); | 
| 40 | - } | |
| 40 | + } | |
| 41 | 41 | } | 
| 42 | 42 | |
| 43 | 43 |      if ($this->basController) { | 
| 44 | - $action = $this->basController . '/' . $action; | |
| 44 | + $action = $this->basController . '/' . $action; | |
| 45 | 45 | } | 
| 46 | 46 | |
| 47 | 47 |      if ($this->groupMiddleware) { | 
| 48 | 48 | |
| 49 | -      if (is_array($middleware)) { | |
| 49 | +        if (is_array($middleware)) { | |
| 50 | 50 | |
| 51 | 51 | $middleware = array_merge($this->groupMiddleware[0], $middleware); | 
| 52 | 52 | |
| 53 | -      } else { | |
| 53 | +        } else { | |
| 54 | 54 | |
| 55 | 55 | array_push($this->groupMiddleware[0], $middleware); | 
| 56 | 56 | |
| 57 | 57 | $middleware = $this->groupMiddleware[0]; | 
| 58 | - } | |
| 58 | + } | |
| 59 | 59 | } | 
| 60 | 60 | |
| 61 | 61 | $routes = [ | 
| 62 | - 'url' => $url, | |
| 63 | - 'pattern' => $this->generatePattern($url), | |
| 64 | - 'action' => $this->getAction($action), | |
| 65 | - 'method' => $requestMethos, | |
| 66 | - 'middleware' => $middleware | |
| 62 | + 'url' => $url, | |
| 63 | + 'pattern' => $this->generatePattern($url), | |
| 64 | + 'action' => $this->getAction($action), | |
| 65 | + 'method' => $requestMethos, | |
| 66 | + 'middleware' => $middleware | |
| 67 | 67 | ]; | 
| 68 | 68 | |
| 69 | 69 | $this->routes[] = $routes; | 
| 70 | 70 | |
| 71 | 71 | return $this; | 
| 72 | - } | |
| 72 | + } | |
| 73 | 73 | |
| 74 | - public function group($groupOptions, callable $callback) | |
| 75 | -  { | |
| 74 | + public function group($groupOptions, callable $callback) | |
| 75 | +    { | |
| 76 | 76 | $prefix = $groupOptions['prefix']; | 
| 77 | 77 | $controller = $groupOptions['controller']; | 
| 78 | 78 | $middlewareFrom = array_shift($groupOptions['middleware']); | 
| @@ -82,7 +82,7 @@ discard block | ||
| 82 | 82 |      $check = '/' . explode('/', $url)[0]; | 
| 83 | 83 | |
| 84 | 84 |      if ($check !== $prefix) { | 
| 85 | - return $this; | |
| 85 | + return $this; | |
| 86 | 86 | } | 
| 87 | 87 | |
| 88 | 88 | $this->prefix = $prefix; | 
| @@ -93,10 +93,10 @@ discard block | ||
| 93 | 93 | $callback($this); | 
| 94 | 94 | |
| 95 | 95 | return $this; | 
| 96 | - } | |
| 96 | + } | |
| 97 | 97 | |
| 98 | - public function package($url, $controller) | |
| 99 | -  { | |
| 98 | + public function package($url, $controller) | |
| 99 | +    { | |
| 100 | 100 |      $this->add("$url", "$controller"); | 
| 101 | 101 |      $this->add("$url/add", "$controller@add", "POST"); | 
| 102 | 102 |      $this->add("$url/submit", "$controller@submit", "POST"); | 
| @@ -105,19 +105,19 @@ discard block | ||
| 105 | 105 |      $this->add("$url/delete/:id", "$controller@delete", "POST"); | 
| 106 | 106 | |
| 107 | 107 | return $this; | 
| 108 | - } | |
| 108 | + } | |
| 109 | 109 | |
| 110 | - public function generatePattern($url) | |
| 111 | -  { | |
| 110 | + public function generatePattern($url) | |
| 111 | +    { | |
| 112 | 112 | $pattern = '#^'; | 
| 113 | 113 | $pattern .= str_replace([':text', ':id'], ['([a-zA-Z0-9-]+)', '(\d+)'], $url); | 
| 114 | 114 | $pattern .= '$#'; | 
| 115 | 115 | |
| 116 | 116 | return $pattern; | 
| 117 | - } | |
| 117 | + } | |
| 118 | 118 | |
| 119 | - public function getAction($action) | |
| 120 | -  { | |
| 119 | + public function getAction($action) | |
| 120 | +    { | |
| 121 | 121 |      $action = str_replace('/', '\\', $action); | 
| 122 | 122 | |
| 123 | 123 | $action = (strpos($action, '@') != 0) ? $action : $action . '@index'; | 
| @@ -125,13 +125,13 @@ discard block | ||
| 125 | 125 |      $action = explode('@', $action); | 
| 126 | 126 | |
| 127 | 127 | return $action; | 
| 128 | - } | |
| 128 | + } | |
| 129 | 129 | |
| 130 | - public function getProperRoute() | |
| 131 | -  { | |
| 130 | + public function getProperRoute() | |
| 131 | +    { | |
| 132 | 132 |      foreach ($this->routes as $route) { | 
| 133 | 133 | |
| 134 | -      if ($this->isMatching($route['pattern']) && $this->isMatchingRequestMethod($route['method'])) { | |
| 134 | +        if ($this->isMatching($route['pattern']) && $this->isMatchingRequestMethod($route['method'])) { | |
| 135 | 135 | |
| 136 | 136 | $this->current = $route; | 
| 137 | 137 | |
| @@ -139,85 +139,85 @@ discard block | ||
| 139 | 139 | |
| 140 | 140 |          if ($route['middleware']) { | 
| 141 | 141 | |
| 142 | -          if (is_array($route['middleware'])) { | |
| 142 | +            if (is_array($route['middleware'])) { | |
| 143 | 143 | |
| 144 | 144 |              foreach ($route['middleware'] as $middleware) { | 
| 145 | 145 | |
| 146 | - $output = $this->middleware($middleware); | |
| 146 | + $output = $this->middleware($middleware); | |
| 147 | 147 | |
| 148 | -              if ($output != '') { | |
| 148 | +                if ($output != '') { | |
| 149 | 149 | break; | 
| 150 | - } | |
| 150 | + } | |
| 151 | 151 | } | 
| 152 | 152 | |
| 153 | -          } else { | |
| 153 | +            } else { | |
| 154 | 154 | |
| 155 | 155 | $output = $this->middleware($route['middleware']); | 
| 156 | 156 | |
| 157 | 157 | if ($output != '') break; | 
| 158 | - } | |
| 158 | + } | |
| 159 | 159 | } | 
| 160 | 160 | |
| 161 | 161 |          if ($output == '') { | 
| 162 | 162 | |
| 163 | - list($controller, $method) = $route['action']; | |
| 163 | + list($controller, $method) = $route['action']; | |
| 164 | 164 | |
| 165 | - $arguments = $this->getArgumentsFor($route['pattern']); | |
| 165 | + $arguments = $this->getArgumentsFor($route['pattern']); | |
| 166 | 166 | |
| 167 | - $output = (string) $this->app->load->action($controller, $method, $arguments); | |
| 167 | + $output = (string) $this->app->load->action($controller, $method, $arguments); | |
| 168 | 168 | |
| 169 | - return $output; | |
| 169 | + return $output; | |
| 170 | 170 | |
| 171 | 171 | } | 
| 172 | 172 | break; | 
| 173 | - } | |
| 173 | + } | |
| 174 | 174 | } | 
| 175 | 175 | |
| 176 | 176 |      $output = (string) $this->app->load->action('notFound', 'index', []); | 
| 177 | 177 | |
| 178 | 178 | return $output; | 
| 179 | - } | |
| 179 | + } | |
| 180 | 180 | |
| 181 | - public function isMatching($pattern) | |
| 182 | -  { | |
| 181 | + public function isMatching($pattern) | |
| 182 | +    { | |
| 183 | 183 | $url = $this->app->request->url(); | 
| 184 | 184 | |
| 185 | 185 | $url = strtolower($url); | 
| 186 | 186 | |
| 187 | 187 | return preg_match($pattern, $url); | 
| 188 | - } | |
| 188 | + } | |
| 189 | 189 | |
| 190 | - private function isMatchingRequestMethod($method) | |
| 191 | -  { | |
| 190 | + private function isMatchingRequestMethod($method) | |
| 191 | +    { | |
| 192 | 192 | $methods = ['GET', 'POST']; | 
| 193 | 193 | |
| 194 | 194 |      if (($method == 'both') && in_array($this->app->request->method(), $methods)) { | 
| 195 | - return true; | |
| 195 | + return true; | |
| 196 | 196 | } | 
| 197 | 197 | |
| 198 | 198 |      if (is_array($method)) { | 
| 199 | 199 | |
| 200 | -      if (count($method) == 1) { | |
| 200 | +        if (count($method) == 1) { | |
| 201 | 201 | |
| 202 | 202 | $method = $method[0]; | 
| 203 | 203 | |
| 204 | -      } else if (count($method) == 2) { | |
| 204 | +        } else if (count($method) == 2) { | |
| 205 | 205 | |
| 206 | 206 |          if (in_array($method[0], $methods) && in_array($method[1], $methods)) { | 
| 207 | - return true; | |
| 207 | + return true; | |
| 208 | 208 | } | 
| 209 | 209 | |
| 210 | -      } else { | |
| 210 | +        } else { | |
| 211 | 211 | |
| 212 | 212 | return false; | 
| 213 | - } | |
| 213 | + } | |
| 214 | 214 | } | 
| 215 | 215 | |
| 216 | 216 | return $this->app->request->method() == $method; | 
| 217 | - } | |
| 217 | + } | |
| 218 | 218 | |
| 219 | - public function getArgumentsFor($pattern) | |
| 220 | -  { | |
| 219 | + public function getArgumentsFor($pattern) | |
| 220 | +    { | |
| 221 | 221 | $url = $this->app->request->url(); | 
| 222 | 222 | |
| 223 | 223 | preg_match($pattern, $url, $matches); | 
| @@ -225,15 +225,15 @@ discard block | ||
| 225 | 225 | array_shift($matches); | 
| 226 | 226 | |
| 227 | 227 | return $matches; | 
| 228 | - } | |
| 228 | + } | |
| 229 | 229 | |
| 230 | - public function getCurrent($key) | |
| 231 | -  { | |
| 230 | + public function getCurrent($key) | |
| 231 | +    { | |
| 232 | 232 | return $this->current[$key]; | 
| 233 | - } | |
| 233 | + } | |
| 234 | 234 | |
| 235 | - private function middleware($middleware, $from = 'admin') | |
| 236 | -  { | |
| 235 | + private function middleware($middleware, $from = 'admin') | |
| 236 | +    { | |
| 237 | 237 | $middlewareInterface = 'App\\Middleware\\MiddlewaresInterface'; | 
| 238 | 238 | |
| 239 | 239 | $middlewares = $this->app->alias['middlewares'][$from]; | 
| @@ -241,7 +241,7 @@ discard block | ||
| 241 | 241 | $middlewareClass = $middlewares[$middleware]; | 
| 242 | 242 | |
| 243 | 243 |      if (!in_array($middlewareInterface, class_implements($middlewareClass))) { | 
| 244 | -      throw new Exception("$middlewareClass not Implement"); | |
| 244 | +        throw new Exception("$middlewareClass not Implement"); | |
| 245 | 245 | } | 
| 246 | 246 | |
| 247 | 247 | $middlewareObject = new $middlewareClass; | 
| @@ -249,9 +249,9 @@ discard block | ||
| 249 | 249 | $output = $middlewareObject->handle($this->app, static::NEXT); | 
| 250 | 250 | |
| 251 | 251 |      if ($output && $output === static::NEXT) { | 
| 252 | - $output = ''; | |
| 252 | + $output = ''; | |
| 253 | 253 | } | 
| 254 | 254 | |
| 255 | 255 | return $output; | 
| 256 | - } | |
| 256 | + } | |
| 257 | 257 | } | 
| 258 | 258 | \ No newline at end of file |