@@ -39,272 +39,272 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class Lenevor implements LenevorContract |
| 41 | 41 | { |
| 42 | - /** |
|
| 43 | - * The application implementation. |
|
| 44 | - * |
|
| 45 | - * @var \Syscodes\Contracts\Core\Application $app |
|
| 46 | - */ |
|
| 47 | - protected $app; |
|
| 42 | + /** |
|
| 43 | + * The application implementation. |
|
| 44 | + * |
|
| 45 | + * @var \Syscodes\Contracts\Core\Application $app |
|
| 46 | + */ |
|
| 47 | + protected $app; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * The bootstrap classes for the application. |
|
| 51 | - * |
|
| 52 | - * @var array $bootstrappers |
|
| 53 | - */ |
|
| 54 | - protected $bootstrappers = [ |
|
| 55 | - \Syscodes\Core\Bootstrap\BootDetectEnvironment::class, |
|
| 56 | - \Syscodes\Core\Bootstrap\BootConfiguration::class, |
|
| 57 | - \Syscodes\Core\Bootstrap\BootHandleExceptions::class, |
|
| 58 | - \Syscodes\Core\Bootstrap\BootRegisterFacades::class, |
|
| 59 | - \Syscodes\Core\Bootstrap\BootRegisterProviders::class, |
|
| 60 | - \Syscodes\Core\Bootstrap\BootProviders::class, |
|
| 61 | - ]; |
|
| 49 | + /** |
|
| 50 | + * The bootstrap classes for the application. |
|
| 51 | + * |
|
| 52 | + * @var array $bootstrappers |
|
| 53 | + */ |
|
| 54 | + protected $bootstrappers = [ |
|
| 55 | + \Syscodes\Core\Bootstrap\BootDetectEnvironment::class, |
|
| 56 | + \Syscodes\Core\Bootstrap\BootConfiguration::class, |
|
| 57 | + \Syscodes\Core\Bootstrap\BootHandleExceptions::class, |
|
| 58 | + \Syscodes\Core\Bootstrap\BootRegisterFacades::class, |
|
| 59 | + \Syscodes\Core\Bootstrap\BootRegisterProviders::class, |
|
| 60 | + \Syscodes\Core\Bootstrap\BootProviders::class, |
|
| 61 | + ]; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Get the application's middleware. |
|
| 65 | - * |
|
| 66 | - * @var array $middleware |
|
| 67 | - */ |
|
| 68 | - protected $middleware = []; |
|
| 63 | + /** |
|
| 64 | + * Get the application's middleware. |
|
| 65 | + * |
|
| 66 | + * @var array $middleware |
|
| 67 | + */ |
|
| 68 | + protected $middleware = []; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Get the application's middleware groups. |
|
| 72 | - * |
|
| 73 | - * @var array $middlewareGroups |
|
| 74 | - */ |
|
| 75 | - protected $middlewareGroups = []; |
|
| 70 | + /** |
|
| 71 | + * Get the application's middleware groups. |
|
| 72 | + * |
|
| 73 | + * @var array $middlewareGroups |
|
| 74 | + */ |
|
| 75 | + protected $middlewareGroups = []; |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The router instance. |
|
| 79 | - * |
|
| 80 | - * @var \Syscodes\Routing\Router $router |
|
| 81 | - */ |
|
| 82 | - protected $router; |
|
| 77 | + /** |
|
| 78 | + * The router instance. |
|
| 79 | + * |
|
| 80 | + * @var \Syscodes\Routing\Router $router |
|
| 81 | + */ |
|
| 82 | + protected $router; |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Get the application's route middleware. |
|
| 86 | - * |
|
| 87 | - * @var array $routeMiddleware |
|
| 88 | - */ |
|
| 89 | - protected $routeMiddleware = []; |
|
| 84 | + /** |
|
| 85 | + * Get the application's route middleware. |
|
| 86 | + * |
|
| 87 | + * @var array $routeMiddleware |
|
| 88 | + */ |
|
| 89 | + protected $routeMiddleware = []; |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Total app execution time. |
|
| 93 | - * |
|
| 94 | - * @var float $totalTime |
|
| 95 | - */ |
|
| 96 | - protected $totalTime; |
|
| 91 | + /** |
|
| 92 | + * Total app execution time. |
|
| 93 | + * |
|
| 94 | + * @var float $totalTime |
|
| 95 | + */ |
|
| 96 | + protected $totalTime; |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Constructor. Lenevor class instance. |
|
| 100 | - * |
|
| 101 | - * @param \Syscodes\Contracts\Core\Application $app |
|
| 102 | - * @param \Syscodes\Routing\Router $router |
|
| 103 | - * |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - public function __construct(Application $app, Router $router) |
|
| 107 | - { |
|
| 108 | - $this->app = $app; |
|
| 109 | - $this->router = $router; |
|
| 98 | + /** |
|
| 99 | + * Constructor. Lenevor class instance. |
|
| 100 | + * |
|
| 101 | + * @param \Syscodes\Contracts\Core\Application $app |
|
| 102 | + * @param \Syscodes\Routing\Router $router |
|
| 103 | + * |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + public function __construct(Application $app, Router $router) |
|
| 107 | + { |
|
| 108 | + $this->app = $app; |
|
| 109 | + $this->router = $router; |
|
| 110 | 110 | |
| 111 | - $this->syncMiddlewareRoute(); |
|
| 112 | - } |
|
| 111 | + $this->syncMiddlewareRoute(); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Initializes the framework, this can only be called once. |
|
| 116 | - * Launch the application. |
|
| 117 | - * |
|
| 118 | - * @param \Syscodes\http\Request $request |
|
| 119 | - * |
|
| 120 | - * @return \Syscodes\Http\Response |
|
| 121 | - */ |
|
| 122 | - public function handle($request) |
|
| 123 | - { |
|
| 124 | - try { |
|
| 125 | - $response = $this->sendRequestThroughRouter($request); |
|
| 126 | - } catch (Throwable $e) { |
|
| 127 | - $this->reportException($e); |
|
| 114 | + /** |
|
| 115 | + * Initializes the framework, this can only be called once. |
|
| 116 | + * Launch the application. |
|
| 117 | + * |
|
| 118 | + * @param \Syscodes\http\Request $request |
|
| 119 | + * |
|
| 120 | + * @return \Syscodes\Http\Response |
|
| 121 | + */ |
|
| 122 | + public function handle($request) |
|
| 123 | + { |
|
| 124 | + try { |
|
| 125 | + $response = $this->sendRequestThroughRouter($request); |
|
| 126 | + } catch (Throwable $e) { |
|
| 127 | + $this->reportException($e); |
|
| 128 | 128 | |
| 129 | - $response = $this->renderException($request, $e); |
|
| 130 | - } |
|
| 129 | + $response = $this->renderException($request, $e); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - return $response; |
|
| 133 | - } |
|
| 132 | + return $response; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Send the given request through the router. |
|
| 137 | - * |
|
| 138 | - * @param \Syscodes\Http\Request $request |
|
| 139 | - * |
|
| 140 | - * @return \Syscodes\Http\Response |
|
| 141 | - */ |
|
| 142 | - protected function sendRequestThroughRouter($request) |
|
| 143 | - { |
|
| 144 | - $this->app->instance('request', $request); |
|
| 135 | + /** |
|
| 136 | + * Send the given request through the router. |
|
| 137 | + * |
|
| 138 | + * @param \Syscodes\Http\Request $request |
|
| 139 | + * |
|
| 140 | + * @return \Syscodes\Http\Response |
|
| 141 | + */ |
|
| 142 | + protected function sendRequestThroughRouter($request) |
|
| 143 | + { |
|
| 144 | + $this->app->instance('request', $request); |
|
| 145 | 145 | |
| 146 | - Facade::clearResolvedInstance('request'); |
|
| 146 | + Facade::clearResolvedInstance('request'); |
|
| 147 | 147 | |
| 148 | - // Load configuration system |
|
| 149 | - $this->bootstrap(); |
|
| 148 | + // Load configuration system |
|
| 149 | + $this->bootstrap(); |
|
| 150 | 150 | |
| 151 | - return (new Pipeline($this->app)) |
|
| 152 | - ->send($request) |
|
| 153 | - ->through($this->app->skipGoingMiddleware() ? [] : $this->middleware) |
|
| 154 | - ->then($this->dispatchToRouter()); |
|
| 155 | - } |
|
| 151 | + return (new Pipeline($this->app)) |
|
| 152 | + ->send($request) |
|
| 153 | + ->through($this->app->skipGoingMiddleware() ? [] : $this->middleware) |
|
| 154 | + ->then($this->dispatchToRouter()); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * Bootstrap the application for HTTP requests. |
|
| 159 | - * |
|
| 160 | - * @return void |
|
| 161 | - */ |
|
| 162 | - protected function bootstrap() |
|
| 163 | - { |
|
| 164 | - if ( ! $this->app->hasBeenBootstrapped()) { |
|
| 165 | - $this->app->bootstrapWith($this->bootstrappers()); |
|
| 166 | - } |
|
| 167 | - } |
|
| 157 | + /** |
|
| 158 | + * Bootstrap the application for HTTP requests. |
|
| 159 | + * |
|
| 160 | + * @return void |
|
| 161 | + */ |
|
| 162 | + protected function bootstrap() |
|
| 163 | + { |
|
| 164 | + if ( ! $this->app->hasBeenBootstrapped()) { |
|
| 165 | + $this->app->bootstrapWith($this->bootstrappers()); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Get the bootstrap classes for the application. |
|
| 171 | - * |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - protected function bootstrappers() |
|
| 175 | - { |
|
| 176 | - return $this->bootstrappers; |
|
| 177 | - } |
|
| 169 | + /** |
|
| 170 | + * Get the bootstrap classes for the application. |
|
| 171 | + * |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + protected function bootstrappers() |
|
| 175 | + { |
|
| 176 | + return $this->bootstrappers; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Sync the current state of the middleware to the router. |
|
| 181 | - * |
|
| 182 | - * @return void |
|
| 183 | - */ |
|
| 184 | - protected function syncMiddlewareRoute() |
|
| 185 | - { |
|
| 186 | - foreach ($this->middlewareGroups as $key => $middleware) { |
|
| 187 | - $this->router->middlewareGroup($key, $middleware); |
|
| 188 | - } |
|
| 179 | + /** |
|
| 180 | + * Sync the current state of the middleware to the router. |
|
| 181 | + * |
|
| 182 | + * @return void |
|
| 183 | + */ |
|
| 184 | + protected function syncMiddlewareRoute() |
|
| 185 | + { |
|
| 186 | + foreach ($this->middlewareGroups as $key => $middleware) { |
|
| 187 | + $this->router->middlewareGroup($key, $middleware); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - foreach ($this->routeMiddleware as $key => $middleware) { |
|
| 191 | - $this->router->aliasMiddleware($key, $middleware); |
|
| 192 | - } |
|
| 193 | - } |
|
| 190 | + foreach ($this->routeMiddleware as $key => $middleware) { |
|
| 191 | + $this->router->aliasMiddleware($key, $middleware); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Get the dispatcher of routes. |
|
| 197 | - * |
|
| 198 | - * @return \Closure |
|
| 199 | - */ |
|
| 200 | - protected function dispatchToRouter() |
|
| 201 | - { |
|
| 202 | - return function ($request) { |
|
| 203 | - $this->app->instance('request', $request); |
|
| 195 | + /** |
|
| 196 | + * Get the dispatcher of routes. |
|
| 197 | + * |
|
| 198 | + * @return \Closure |
|
| 199 | + */ |
|
| 200 | + protected function dispatchToRouter() |
|
| 201 | + { |
|
| 202 | + return function ($request) { |
|
| 203 | + $this->app->instance('request', $request); |
|
| 204 | 204 | |
| 205 | - return $this->router->dispatch($request); |
|
| 206 | - }; |
|
| 207 | - } |
|
| 205 | + return $this->router->dispatch($request); |
|
| 206 | + }; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Call the shutdown method on any terminable middleware. |
|
| 211 | - * |
|
| 212 | - * @param \Syscodes\Http\Request $request |
|
| 213 | - * @param \Syscodes\Http\Response $response |
|
| 214 | - * |
|
| 215 | - * @return void |
|
| 216 | - */ |
|
| 217 | - public function shutdown($request, $response) |
|
| 218 | - { |
|
| 219 | - $this->shutdownMiddleware($request, $response); |
|
| 220 | - } |
|
| 209 | + /** |
|
| 210 | + * Call the shutdown method on any terminable middleware. |
|
| 211 | + * |
|
| 212 | + * @param \Syscodes\Http\Request $request |
|
| 213 | + * @param \Syscodes\Http\Response $response |
|
| 214 | + * |
|
| 215 | + * @return void |
|
| 216 | + */ |
|
| 217 | + public function shutdown($request, $response) |
|
| 218 | + { |
|
| 219 | + $this->shutdownMiddleware($request, $response); |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - /** |
|
| 223 | - * Call the terminate method on any terminable middleware. |
|
| 224 | - * |
|
| 225 | - * @param \Syscodes\Http\Request $request |
|
| 226 | - * @param \Syscodes\Http\Response $response |
|
| 227 | - * |
|
| 228 | - * @return void |
|
| 229 | - */ |
|
| 230 | - protected function shutdownMiddleware($request, $response) |
|
| 231 | - { |
|
| 232 | - $middlewares = $this->app->skipGoingMiddleware() ? [] : array_merge( |
|
| 233 | - $this->gatherRouteMiddleware($request), |
|
| 234 | - $this->middleware |
|
| 235 | - ); |
|
| 222 | + /** |
|
| 223 | + * Call the terminate method on any terminable middleware. |
|
| 224 | + * |
|
| 225 | + * @param \Syscodes\Http\Request $request |
|
| 226 | + * @param \Syscodes\Http\Response $response |
|
| 227 | + * |
|
| 228 | + * @return void |
|
| 229 | + */ |
|
| 230 | + protected function shutdownMiddleware($request, $response) |
|
| 231 | + { |
|
| 232 | + $middlewares = $this->app->skipGoingMiddleware() ? [] : array_merge( |
|
| 233 | + $this->gatherRouteMiddleware($request), |
|
| 234 | + $this->middleware |
|
| 235 | + ); |
|
| 236 | 236 | |
| 237 | - foreach ($middlewares as $middleware) { |
|
| 238 | - if (! is_string($middleware)) { |
|
| 239 | - continue; |
|
| 240 | - } |
|
| 237 | + foreach ($middlewares as $middleware) { |
|
| 238 | + if (! is_string($middleware)) { |
|
| 239 | + continue; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - [$name] = $this->parseMiddleware($middleware); |
|
| 242 | + [$name] = $this->parseMiddleware($middleware); |
|
| 243 | 243 | |
| 244 | - $instance = $this->app->make($name); |
|
| 244 | + $instance = $this->app->make($name); |
|
| 245 | 245 | |
| 246 | - if (method_exists($instance, 'shutdown')) { |
|
| 247 | - $instance->shutdown($request, $response); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - } |
|
| 246 | + if (method_exists($instance, 'shutdown')) { |
|
| 247 | + $instance->shutdown($request, $response); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - /** |
|
| 253 | - * Gather the route middleware for the given request. |
|
| 254 | - * |
|
| 255 | - * @param \Syscodes\Http\Request $request |
|
| 256 | - * |
|
| 257 | - * @return array |
|
| 258 | - */ |
|
| 259 | - protected function gatherRouteMiddleware($request) |
|
| 260 | - { |
|
| 261 | - if ( ! is_null($route = $request->route())) { |
|
| 262 | - return $this->router->gatherRouteMiddleware($route); |
|
| 263 | - } |
|
| 252 | + /** |
|
| 253 | + * Gather the route middleware for the given request. |
|
| 254 | + * |
|
| 255 | + * @param \Syscodes\Http\Request $request |
|
| 256 | + * |
|
| 257 | + * @return array |
|
| 258 | + */ |
|
| 259 | + protected function gatherRouteMiddleware($request) |
|
| 260 | + { |
|
| 261 | + if ( ! is_null($route = $request->route())) { |
|
| 262 | + return $this->router->gatherRouteMiddleware($route); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - return []; |
|
| 266 | - } |
|
| 265 | + return []; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * Parse a middleware string to get the name and parameters. |
|
| 270 | - * |
|
| 271 | - * @param string $middleware |
|
| 272 | - * |
|
| 273 | - * @return array |
|
| 274 | - */ |
|
| 275 | - protected function parseMiddleware($middleware) |
|
| 276 | - { |
|
| 277 | - [$name, $parameters] = array_pad(explode(':', $middleware, 2), 2, []); |
|
| 268 | + /** |
|
| 269 | + * Parse a middleware string to get the name and parameters. |
|
| 270 | + * |
|
| 271 | + * @param string $middleware |
|
| 272 | + * |
|
| 273 | + * @return array |
|
| 274 | + */ |
|
| 275 | + protected function parseMiddleware($middleware) |
|
| 276 | + { |
|
| 277 | + [$name, $parameters] = array_pad(explode(':', $middleware, 2), 2, []); |
|
| 278 | 278 | |
| 279 | - if (is_string($parameters)) { |
|
| 280 | - $parameters = explode(',', $parameters); |
|
| 281 | - } |
|
| 279 | + if (is_string($parameters)) { |
|
| 280 | + $parameters = explode(',', $parameters); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - return [$name, $parameters]; |
|
| 283 | + return [$name, $parameters]; |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Report the exception to the exception handler. |
|
| 288 | - * |
|
| 289 | - * @param \Throwable $e |
|
| 290 | - * |
|
| 291 | - * @return void |
|
| 292 | - */ |
|
| 293 | - protected function reportException(Throwable $e) |
|
| 294 | - { |
|
| 295 | - return $this->app[ExceptionHandler::class]->report($e); |
|
| 296 | - } |
|
| 286 | + /** |
|
| 287 | + * Report the exception to the exception handler. |
|
| 288 | + * |
|
| 289 | + * @param \Throwable $e |
|
| 290 | + * |
|
| 291 | + * @return void |
|
| 292 | + */ |
|
| 293 | + protected function reportException(Throwable $e) |
|
| 294 | + { |
|
| 295 | + return $this->app[ExceptionHandler::class]->report($e); |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - /** |
|
| 299 | - * Render the exception to a response. |
|
| 300 | - * |
|
| 301 | - * @param \Syscodes\Http\Request $request |
|
| 302 | - * @param \Throwable $e |
|
| 303 | - * |
|
| 304 | - * @return \Syscodes\Http\Response |
|
| 305 | - */ |
|
| 306 | - protected function renderException($request, Throwable $e) |
|
| 307 | - { |
|
| 308 | - return $this->app[ExceptionHandler::class]->render($request, $e); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | 298 | \ No newline at end of file |
| 299 | + /** |
|
| 300 | + * Render the exception to a response. |
|
| 301 | + * |
|
| 302 | + * @param \Syscodes\Http\Request $request |
|
| 303 | + * @param \Throwable $e |
|
| 304 | + * |
|
| 305 | + * @return \Syscodes\Http\Response |
|
| 306 | + */ |
|
| 307 | + protected function renderException($request, Throwable $e) |
|
| 308 | + { |
|
| 309 | + return $this->app[ExceptionHandler::class]->render($request, $e); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | 312 | \ No newline at end of file |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | protected function dispatchToRouter() |
| 201 | 201 | { |
| 202 | - return function ($request) { |
|
| 202 | + return function($request) { |
|
| 203 | 203 | $this->app->instance('request', $request); |
| 204 | 204 | |
| 205 | 205 | return $this->router->dispatch($request); |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | ); |
| 236 | 236 | |
| 237 | 237 | foreach ($middlewares as $middleware) { |
| 238 | - if (! is_string($middleware)) { |
|
| 238 | + if ( ! is_string($middleware)) { |
|
| 239 | 239 | continue; |
| 240 | 240 | } |
| 241 | 241 | |
@@ -40,738 +40,738 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | class Route |
| 42 | 42 | { |
| 43 | - use Concerns\RouteCondition, |
|
| 44 | - Concerns\RouteDependencyResolver; |
|
| 43 | + use Concerns\RouteCondition, |
|
| 44 | + Concerns\RouteDependencyResolver; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Action that the route will use when called. |
|
| 48 | - * |
|
| 49 | - * @var \Closure|string|array $action |
|
| 50 | - */ |
|
| 51 | - public $action; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The computed gathered middleware. |
|
| 55 | - * |
|
| 56 | - * @var array|null $computedMiddleware |
|
| 57 | - */ |
|
| 58 | - public $computedMiddleware; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The container instance used by the route. |
|
| 62 | - * |
|
| 63 | - * @var \Syscodes\Container\Container $container |
|
| 64 | - */ |
|
| 65 | - protected $container; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The controller instance. |
|
| 69 | - * |
|
| 70 | - * @var string $controller |
|
| 71 | - */ |
|
| 72 | - public $controller; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * The default values for the route. |
|
| 76 | - * |
|
| 77 | - * @var array $defaults |
|
| 78 | - */ |
|
| 79 | - public $defaults = []; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Variable of HTTP method. |
|
| 83 | - * |
|
| 84 | - * @var array|string $method |
|
| 85 | - */ |
|
| 86 | - public $method; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * The array of matched parameters. |
|
| 90 | - * |
|
| 91 | - * @var array $parameters |
|
| 92 | - */ |
|
| 93 | - public $parameters = []; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * The parameter names for the route. |
|
| 97 | - * |
|
| 98 | - * @var string|null $parameterNames |
|
| 99 | - */ |
|
| 100 | - public $parameterNames; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Patterns that should be replaced. |
|
| 104 | - * |
|
| 105 | - * @var array $patterns |
|
| 106 | - */ |
|
| 107 | - public $patterns = [ |
|
| 108 | - '~/~' => '\/', // Slash |
|
| 109 | - '~{an:[^\/{}]+}~' => '([0-9a-zA-Z]++)', // Placeholder accepts alphabetic and numeric chars |
|
| 110 | - '~{n:[^\/{}]+}~' => '([0-9]++)', // Placeholder accepts only numeric |
|
| 111 | - '~{a:[^\/{}]+}~' => '([a-zA-Z]++)', // Placeholder accepts only alphabetic chars |
|
| 112 | - '~{w:[^\/{}]+}~' => '([0-9a-zA-Z-_]++)', // Placeholder accepts alphanumeric and underscore |
|
| 113 | - '~{\*:[^\/{}]+}~' => '(.++)', // Placeholder match rest of url |
|
| 114 | - '~(\\\/)?{\?:[^\/{}]+}~' => '\/?([^\/]*)', // Optional placeholder |
|
| 115 | - '~{[^\/{}]+}~' => '([^\/]++)' // Normal placeholder |
|
| 116 | - ]; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * The URI pattern the route responds to. |
|
| 120 | - * |
|
| 121 | - * @var array $uri |
|
| 122 | - */ |
|
| 123 | - public $uri = []; |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Contains the arguments of the current route. |
|
| 127 | - * |
|
| 128 | - * @var array $where |
|
| 129 | - */ |
|
| 130 | - public $wheres = []; |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Constructor. Initialize route. |
|
| 134 | - * |
|
| 135 | - * @param array|string|null $method |
|
| 136 | - * @param string|null $uri |
|
| 137 | - * @param \Closure|string|null $action |
|
| 138 | - * |
|
| 139 | - * @return void |
|
| 140 | - */ |
|
| 141 | - public function __construct($method = null, $uri = null, $action = null) |
|
| 142 | - { |
|
| 143 | - $this->uri = $uri; |
|
| 144 | - |
|
| 145 | - // Set the method |
|
| 146 | - $this->parseMethod($method); |
|
| 147 | - |
|
| 148 | - // Set the action |
|
| 149 | - $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
| 150 | - |
|
| 151 | - if (is_null($prefix = Arr::get($this->action, 'prefix'))) { |
|
| 152 | - $this->prefix($prefix); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Getters |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get the action of the current route. |
|
| 160 | - * |
|
| 161 | - * @return \Closure|string|array |
|
| 162 | - */ |
|
| 163 | - public function getAction() |
|
| 164 | - { |
|
| 165 | - return $this->action; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Get the arguments of the current route. |
|
| 170 | - * |
|
| 171 | - * @return array |
|
| 172 | - */ |
|
| 173 | - public function getArguments() |
|
| 174 | - { |
|
| 175 | - return $this->wheres; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Get the controller instance for the route. |
|
| 180 | - * |
|
| 181 | - * @return mixed |
|
| 182 | - */ |
|
| 183 | - public function getController() |
|
| 184 | - { |
|
| 185 | - if ( ! $this->controller) { |
|
| 186 | - $class = $this->parseControllerCallback()[0]; |
|
| 46 | + /** |
|
| 47 | + * Action that the route will use when called. |
|
| 48 | + * |
|
| 49 | + * @var \Closure|string|array $action |
|
| 50 | + */ |
|
| 51 | + public $action; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The computed gathered middleware. |
|
| 55 | + * |
|
| 56 | + * @var array|null $computedMiddleware |
|
| 57 | + */ |
|
| 58 | + public $computedMiddleware; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The container instance used by the route. |
|
| 62 | + * |
|
| 63 | + * @var \Syscodes\Container\Container $container |
|
| 64 | + */ |
|
| 65 | + protected $container; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The controller instance. |
|
| 69 | + * |
|
| 70 | + * @var string $controller |
|
| 71 | + */ |
|
| 72 | + public $controller; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * The default values for the route. |
|
| 76 | + * |
|
| 77 | + * @var array $defaults |
|
| 78 | + */ |
|
| 79 | + public $defaults = []; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Variable of HTTP method. |
|
| 83 | + * |
|
| 84 | + * @var array|string $method |
|
| 85 | + */ |
|
| 86 | + public $method; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * The array of matched parameters. |
|
| 90 | + * |
|
| 91 | + * @var array $parameters |
|
| 92 | + */ |
|
| 93 | + public $parameters = []; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * The parameter names for the route. |
|
| 97 | + * |
|
| 98 | + * @var string|null $parameterNames |
|
| 99 | + */ |
|
| 100 | + public $parameterNames; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Patterns that should be replaced. |
|
| 104 | + * |
|
| 105 | + * @var array $patterns |
|
| 106 | + */ |
|
| 107 | + public $patterns = [ |
|
| 108 | + '~/~' => '\/', // Slash |
|
| 109 | + '~{an:[^\/{}]+}~' => '([0-9a-zA-Z]++)', // Placeholder accepts alphabetic and numeric chars |
|
| 110 | + '~{n:[^\/{}]+}~' => '([0-9]++)', // Placeholder accepts only numeric |
|
| 111 | + '~{a:[^\/{}]+}~' => '([a-zA-Z]++)', // Placeholder accepts only alphabetic chars |
|
| 112 | + '~{w:[^\/{}]+}~' => '([0-9a-zA-Z-_]++)', // Placeholder accepts alphanumeric and underscore |
|
| 113 | + '~{\*:[^\/{}]+}~' => '(.++)', // Placeholder match rest of url |
|
| 114 | + '~(\\\/)?{\?:[^\/{}]+}~' => '\/?([^\/]*)', // Optional placeholder |
|
| 115 | + '~{[^\/{}]+}~' => '([^\/]++)' // Normal placeholder |
|
| 116 | + ]; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * The URI pattern the route responds to. |
|
| 120 | + * |
|
| 121 | + * @var array $uri |
|
| 122 | + */ |
|
| 123 | + public $uri = []; |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Contains the arguments of the current route. |
|
| 127 | + * |
|
| 128 | + * @var array $where |
|
| 129 | + */ |
|
| 130 | + public $wheres = []; |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Constructor. Initialize route. |
|
| 134 | + * |
|
| 135 | + * @param array|string|null $method |
|
| 136 | + * @param string|null $uri |
|
| 137 | + * @param \Closure|string|null $action |
|
| 138 | + * |
|
| 139 | + * @return void |
|
| 140 | + */ |
|
| 141 | + public function __construct($method = null, $uri = null, $action = null) |
|
| 142 | + { |
|
| 143 | + $this->uri = $uri; |
|
| 144 | + |
|
| 145 | + // Set the method |
|
| 146 | + $this->parseMethod($method); |
|
| 147 | + |
|
| 148 | + // Set the action |
|
| 149 | + $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
| 150 | + |
|
| 151 | + if (is_null($prefix = Arr::get($this->action, 'prefix'))) { |
|
| 152 | + $this->prefix($prefix); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Getters |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get the action of the current route. |
|
| 160 | + * |
|
| 161 | + * @return \Closure|string|array |
|
| 162 | + */ |
|
| 163 | + public function getAction() |
|
| 164 | + { |
|
| 165 | + return $this->action; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Get the arguments of the current route. |
|
| 170 | + * |
|
| 171 | + * @return array |
|
| 172 | + */ |
|
| 173 | + public function getArguments() |
|
| 174 | + { |
|
| 175 | + return $this->wheres; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Get the controller instance for the route. |
|
| 180 | + * |
|
| 181 | + * @return mixed |
|
| 182 | + */ |
|
| 183 | + public function getController() |
|
| 184 | + { |
|
| 185 | + if ( ! $this->controller) { |
|
| 186 | + $class = $this->parseControllerCallback()[0]; |
|
| 187 | 187 | |
| 188 | - $this->controller = $this->container->make(ltrim($class, '\\')); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return $this->controller; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Get the controller method used for the route. |
|
| 196 | - * |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - public function getControllerMethod() |
|
| 200 | - { |
|
| 201 | - return $this->parseControllerCallback()[1]; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Get the request method of the current route. |
|
| 206 | - * |
|
| 207 | - * @return array|string |
|
| 208 | - */ |
|
| 209 | - public function getMethod() |
|
| 210 | - { |
|
| 211 | - return $this->method; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Get the url of the current route. |
|
| 216 | - * |
|
| 217 | - * @return string |
|
| 218 | - */ |
|
| 219 | - public function getName() |
|
| 220 | - { |
|
| 221 | - return $this->action['as'] ?? null; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Get the url of the current route. |
|
| 226 | - * |
|
| 227 | - * @return array |
|
| 228 | - */ |
|
| 229 | - public function getRoute() |
|
| 230 | - { |
|
| 231 | - return $this->uri; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Get or set the domain for the route. |
|
| 236 | - * |
|
| 237 | - * @param string|null $domain |
|
| 238 | - * |
|
| 239 | - * @return $this |
|
| 240 | - */ |
|
| 241 | - public function domain($domain = null) |
|
| 242 | - { |
|
| 243 | - if (is_null($domain)) { |
|
| 244 | - return $this->getDomain(); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $this->action['domain'] = $this->parseRoute($domain); |
|
| 248 | - |
|
| 249 | - return $this; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Get the domain defined for the route. |
|
| 254 | - * |
|
| 255 | - * @return string|null |
|
| 256 | - */ |
|
| 257 | - public function getDomain() |
|
| 258 | - { |
|
| 259 | - return isset($this->action['domain']) |
|
| 260 | - ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
| 261 | - : null; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Parse the controller. |
|
| 266 | - * |
|
| 267 | - * @return array |
|
| 268 | - */ |
|
| 269 | - public function parseControllerCallback() |
|
| 270 | - { |
|
| 271 | - return Str::parseCallback($this->action['uses']); |
|
| 272 | - } |
|
| 188 | + $this->controller = $this->container->make(ltrim($class, '\\')); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return $this->controller; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Get the controller method used for the route. |
|
| 196 | + * |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + public function getControllerMethod() |
|
| 200 | + { |
|
| 201 | + return $this->parseControllerCallback()[1]; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Get the request method of the current route. |
|
| 206 | + * |
|
| 207 | + * @return array|string |
|
| 208 | + */ |
|
| 209 | + public function getMethod() |
|
| 210 | + { |
|
| 211 | + return $this->method; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Get the url of the current route. |
|
| 216 | + * |
|
| 217 | + * @return string |
|
| 218 | + */ |
|
| 219 | + public function getName() |
|
| 220 | + { |
|
| 221 | + return $this->action['as'] ?? null; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Get the url of the current route. |
|
| 226 | + * |
|
| 227 | + * @return array |
|
| 228 | + */ |
|
| 229 | + public function getRoute() |
|
| 230 | + { |
|
| 231 | + return $this->uri; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Get or set the domain for the route. |
|
| 236 | + * |
|
| 237 | + * @param string|null $domain |
|
| 238 | + * |
|
| 239 | + * @return $this |
|
| 240 | + */ |
|
| 241 | + public function domain($domain = null) |
|
| 242 | + { |
|
| 243 | + if (is_null($domain)) { |
|
| 244 | + return $this->getDomain(); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $this->action['domain'] = $this->parseRoute($domain); |
|
| 248 | + |
|
| 249 | + return $this; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Get the domain defined for the route. |
|
| 254 | + * |
|
| 255 | + * @return string|null |
|
| 256 | + */ |
|
| 257 | + public function getDomain() |
|
| 258 | + { |
|
| 259 | + return isset($this->action['domain']) |
|
| 260 | + ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
| 261 | + : null; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Parse the controller. |
|
| 266 | + * |
|
| 267 | + * @return array |
|
| 268 | + */ |
|
| 269 | + public function parseControllerCallback() |
|
| 270 | + { |
|
| 271 | + return Str::parseCallback($this->action['uses']); |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Checks whether the route's action is a controller. |
|
| 276 | - * |
|
| 277 | - * @return bool |
|
| 278 | - */ |
|
| 279 | - public function isControllerAction() |
|
| 280 | - { |
|
| 281 | - return is_string($this->action['uses']); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Get the dispatcher for the route's controller. |
|
| 286 | - * |
|
| 287 | - * @return \Syscodes\Controller\ControllerDispatcher |
|
| 288 | - */ |
|
| 289 | - private function controllerDispatcher() |
|
| 290 | - { |
|
| 291 | - return new ControllerDispatcher($this->container); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - // Setters |
|
| 274 | + /** |
|
| 275 | + * Checks whether the route's action is a controller. |
|
| 276 | + * |
|
| 277 | + * @return bool |
|
| 278 | + */ |
|
| 279 | + public function isControllerAction() |
|
| 280 | + { |
|
| 281 | + return is_string($this->action['uses']); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Get the dispatcher for the route's controller. |
|
| 286 | + * |
|
| 287 | + * @return \Syscodes\Controller\ControllerDispatcher |
|
| 288 | + */ |
|
| 289 | + private function controllerDispatcher() |
|
| 290 | + { |
|
| 291 | + return new ControllerDispatcher($this->container); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + // Setters |
|
| 295 | 295 | |
| 296 | - /** |
|
| 297 | - * Run the route action and return the response. |
|
| 298 | - * |
|
| 299 | - * @return mixed |
|
| 300 | - */ |
|
| 301 | - public function runResolver() |
|
| 302 | - { |
|
| 303 | - $this->container = $this->container ?: new Container; |
|
| 304 | - |
|
| 305 | - try { |
|
| 306 | - if ($this->isControllerAction()) { |
|
| 307 | - return $this->runResolverController(); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return $this->runResolverCallable(); |
|
| 311 | - } catch (HttpResponseException $e) { |
|
| 312 | - return $e->getResponse(); |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Run the route action and return the response. |
|
| 318 | - * |
|
| 319 | - * @return mixed |
|
| 320 | - */ |
|
| 321 | - protected function runResolverCallable() |
|
| 322 | - { |
|
| 323 | - $callable = $this->action['uses']; |
|
| 324 | - |
|
| 325 | - return $callable(...array_values($this->resolveMethodDependencies( |
|
| 326 | - $this->parametersWithouNulls(), new ReflectionFunction($this->action['uses']) |
|
| 327 | - ))); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Run the route action and return the response. |
|
| 332 | - * |
|
| 333 | - * @return mixed |
|
| 334 | - */ |
|
| 335 | - protected function runResolverController() |
|
| 336 | - { |
|
| 337 | - return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Set the action. |
|
| 342 | - * |
|
| 343 | - * @param \Closure|string $action |
|
| 344 | - * |
|
| 345 | - * @return $this |
|
| 346 | - * |
|
| 347 | - * @throws \InvalidArgumentException |
|
| 348 | - */ |
|
| 349 | - public function parseAction($action) |
|
| 350 | - { |
|
| 351 | - if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
| 352 | - throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - return RouteAction::parse($this->uri, $action); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Set the method of the current route. |
|
| 360 | - * |
|
| 361 | - * @param array $method |
|
| 362 | - * |
|
| 363 | - * @return string $this |
|
| 364 | - * |
|
| 365 | - * @throws \InvalidArgumentException |
|
| 366 | - */ |
|
| 367 | - public function parseMethod($method) |
|
| 368 | - { |
|
| 369 | - if ($method === null || empty($method)) { |
|
| 370 | - throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - foreach ((array) $method as $httpMethod) { |
|
| 374 | - if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
| 375 | - throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - $this->method = $method; |
|
| 380 | - |
|
| 381 | - return $this; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Set the route. |
|
| 386 | - * |
|
| 387 | - * @param string|array|null $uri |
|
| 388 | - * |
|
| 389 | - * @return string |
|
| 390 | - * |
|
| 391 | - * @throws \InvalidArgumentException |
|
| 392 | - */ |
|
| 393 | - public function parseRoute($uri) |
|
| 394 | - { |
|
| 395 | - if ($uri === null) { |
|
| 396 | - throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - $this->uri = $this->parseRoutePath($uri); |
|
| 400 | - |
|
| 401 | - return $this; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Replace word patterns with regex in route uri. |
|
| 406 | - * |
|
| 407 | - * @param string $uri |
|
| 408 | - * |
|
| 409 | - * @return string |
|
| 410 | - */ |
|
| 411 | - protected function parseRoutePath($uri) |
|
| 412 | - { |
|
| 413 | - $uri = trim($uri, '\/?'); |
|
| 414 | - $uri = trim($uri, '\/'); |
|
| 296 | + /** |
|
| 297 | + * Run the route action and return the response. |
|
| 298 | + * |
|
| 299 | + * @return mixed |
|
| 300 | + */ |
|
| 301 | + public function runResolver() |
|
| 302 | + { |
|
| 303 | + $this->container = $this->container ?: new Container; |
|
| 304 | + |
|
| 305 | + try { |
|
| 306 | + if ($this->isControllerAction()) { |
|
| 307 | + return $this->runResolverController(); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return $this->runResolverCallable(); |
|
| 311 | + } catch (HttpResponseException $e) { |
|
| 312 | + return $e->getResponse(); |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Run the route action and return the response. |
|
| 318 | + * |
|
| 319 | + * @return mixed |
|
| 320 | + */ |
|
| 321 | + protected function runResolverCallable() |
|
| 322 | + { |
|
| 323 | + $callable = $this->action['uses']; |
|
| 324 | + |
|
| 325 | + return $callable(...array_values($this->resolveMethodDependencies( |
|
| 326 | + $this->parametersWithouNulls(), new ReflectionFunction($this->action['uses']) |
|
| 327 | + ))); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Run the route action and return the response. |
|
| 332 | + * |
|
| 333 | + * @return mixed |
|
| 334 | + */ |
|
| 335 | + protected function runResolverController() |
|
| 336 | + { |
|
| 337 | + return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Set the action. |
|
| 342 | + * |
|
| 343 | + * @param \Closure|string $action |
|
| 344 | + * |
|
| 345 | + * @return $this |
|
| 346 | + * |
|
| 347 | + * @throws \InvalidArgumentException |
|
| 348 | + */ |
|
| 349 | + public function parseAction($action) |
|
| 350 | + { |
|
| 351 | + if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
| 352 | + throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + return RouteAction::parse($this->uri, $action); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Set the method of the current route. |
|
| 360 | + * |
|
| 361 | + * @param array $method |
|
| 362 | + * |
|
| 363 | + * @return string $this |
|
| 364 | + * |
|
| 365 | + * @throws \InvalidArgumentException |
|
| 366 | + */ |
|
| 367 | + public function parseMethod($method) |
|
| 368 | + { |
|
| 369 | + if ($method === null || empty($method)) { |
|
| 370 | + throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + foreach ((array) $method as $httpMethod) { |
|
| 374 | + if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
| 375 | + throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + $this->method = $method; |
|
| 380 | + |
|
| 381 | + return $this; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Set the route. |
|
| 386 | + * |
|
| 387 | + * @param string|array|null $uri |
|
| 388 | + * |
|
| 389 | + * @return string |
|
| 390 | + * |
|
| 391 | + * @throws \InvalidArgumentException |
|
| 392 | + */ |
|
| 393 | + public function parseRoute($uri) |
|
| 394 | + { |
|
| 395 | + if ($uri === null) { |
|
| 396 | + throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + $this->uri = $this->parseRoutePath($uri); |
|
| 400 | + |
|
| 401 | + return $this; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Replace word patterns with regex in route uri. |
|
| 406 | + * |
|
| 407 | + * @param string $uri |
|
| 408 | + * |
|
| 409 | + * @return string |
|
| 410 | + */ |
|
| 411 | + protected function parseRoutePath($uri) |
|
| 412 | + { |
|
| 413 | + $uri = trim($uri, '\/?'); |
|
| 414 | + $uri = trim($uri, '\/'); |
|
| 415 | 415 | |
| 416 | - preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); |
|
| 416 | + preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); |
|
| 417 | 417 | |
| 418 | - foreach ($matches[1] as $match) { |
|
| 419 | - if (strpos($match, ':') === false) { |
|
| 420 | - continue; |
|
| 421 | - } |
|
| 418 | + foreach ($matches[1] as $match) { |
|
| 419 | + if (strpos($match, ':') === false) { |
|
| 420 | + continue; |
|
| 421 | + } |
|
| 422 | 422 | |
| 423 | - $pattern = array_keys($this->patterns); |
|
| 424 | - $replace = array_values($this->patterns); |
|
| 425 | - $segments = explode(':', trim($match, '{}?')); |
|
| 423 | + $pattern = array_keys($this->patterns); |
|
| 424 | + $replace = array_values($this->patterns); |
|
| 425 | + $segments = explode(':', trim($match, '{}?')); |
|
| 426 | 426 | |
| 427 | - $uri = strpos($match, ':') !== false |
|
| 428 | - ? preg_replace($pattern, $replace, $uri) |
|
| 429 | - : str_replace($match, '{'.$segments[0].'}', $uri); |
|
| 430 | - } |
|
| 427 | + $uri = strpos($match, ':') !== false |
|
| 428 | + ? preg_replace($pattern, $replace, $uri) |
|
| 429 | + : str_replace($match, '{'.$segments[0].'}', $uri); |
|
| 430 | + } |
|
| 431 | 431 | |
| 432 | - return $uri; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * Add a prefix to the route URI. |
|
| 437 | - * |
|
| 438 | - * @param string $prefix |
|
| 439 | - * |
|
| 440 | - * @return $this |
|
| 441 | - */ |
|
| 442 | - public function prefix($prefix) |
|
| 443 | - { |
|
| 444 | - if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
| 445 | - $this->action['prefix'] = $newPrefix; |
|
| 446 | - } |
|
| 432 | + return $uri; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * Add a prefix to the route URI. |
|
| 437 | + * |
|
| 438 | + * @param string $prefix |
|
| 439 | + * |
|
| 440 | + * @return $this |
|
| 441 | + */ |
|
| 442 | + public function prefix($prefix) |
|
| 443 | + { |
|
| 444 | + if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
| 445 | + $this->action['prefix'] = $newPrefix; |
|
| 446 | + } |
|
| 447 | 447 | |
| 448 | - $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
| 448 | + $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
| 449 | 449 | |
| 450 | - return $this->parseRoute($uri !== '/' ? trim($uri, '/') : $uri); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Set the action array for the route. |
|
| 455 | - * |
|
| 456 | - * @param array $action |
|
| 457 | - * |
|
| 458 | - * @return $this |
|
| 459 | - */ |
|
| 460 | - public function setAction(array $action) |
|
| 461 | - { |
|
| 462 | - $this->action = $action; |
|
| 463 | - |
|
| 464 | - if (isset($this->action['domain'])) { |
|
| 465 | - $this->domain($this->action['domain']); |
|
| 466 | - } |
|
| 450 | + return $this->parseRoute($uri !== '/' ? trim($uri, '/') : $uri); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Set the action array for the route. |
|
| 455 | + * |
|
| 456 | + * @param array $action |
|
| 457 | + * |
|
| 458 | + * @return $this |
|
| 459 | + */ |
|
| 460 | + public function setAction(array $action) |
|
| 461 | + { |
|
| 462 | + $this->action = $action; |
|
| 463 | + |
|
| 464 | + if (isset($this->action['domain'])) { |
|
| 465 | + $this->domain($this->action['domain']); |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | - return $this; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * Set the name. |
|
| 473 | - * |
|
| 474 | - * @param string $name |
|
| 475 | - * |
|
| 476 | - * @return $this |
|
| 477 | - */ |
|
| 478 | - public function name($name) |
|
| 479 | - { |
|
| 480 | - $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
| 481 | - |
|
| 482 | - return $this; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Determine whether the route's name matches the given patterns. |
|
| 487 | - * |
|
| 488 | - * @param mixed ...$patterns |
|
| 489 | - * |
|
| 490 | - * @return bool |
|
| 491 | - */ |
|
| 492 | - public function named(...$patterns) |
|
| 493 | - { |
|
| 494 | - if (is_null($routeName = $this->getName())) { |
|
| 495 | - return false; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - foreach ($patterns as $pattern) { |
|
| 499 | - if (Str::is($pattern, $routeName)) { |
|
| 500 | - return true; |
|
| 501 | - } |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - return false; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Set a default value for the route. |
|
| 509 | - * |
|
| 510 | - * @param string $key |
|
| 511 | - * @param mixed $value |
|
| 512 | - * |
|
| 513 | - * @return $this |
|
| 514 | - */ |
|
| 515 | - public function defaults($key, $value) |
|
| 516 | - { |
|
| 517 | - $this->defaults[$key] = $value; |
|
| 518 | - |
|
| 519 | - return $this; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Set a default values for the route. |
|
| 524 | - * |
|
| 525 | - * @param string $defaults |
|
| 526 | - * |
|
| 527 | - * @return $this |
|
| 528 | - */ |
|
| 529 | - public function setDefaults(array $defaults) |
|
| 530 | - { |
|
| 531 | - $this->defaults = $defaults; |
|
| 532 | - |
|
| 533 | - return $this; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * Set the where. |
|
| 538 | - * |
|
| 539 | - * @param array|string $name |
|
| 540 | - * @param string|null $expression |
|
| 541 | - * |
|
| 542 | - * @return $this |
|
| 543 | - */ |
|
| 544 | - public function where($name, string $expression = null) |
|
| 545 | - { |
|
| 546 | - $wheres = is_array($name) ? $name : [$name => $expression]; |
|
| 468 | + return $this; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * Set the name. |
|
| 473 | + * |
|
| 474 | + * @param string $name |
|
| 475 | + * |
|
| 476 | + * @return $this |
|
| 477 | + */ |
|
| 478 | + public function name($name) |
|
| 479 | + { |
|
| 480 | + $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
| 481 | + |
|
| 482 | + return $this; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Determine whether the route's name matches the given patterns. |
|
| 487 | + * |
|
| 488 | + * @param mixed ...$patterns |
|
| 489 | + * |
|
| 490 | + * @return bool |
|
| 491 | + */ |
|
| 492 | + public function named(...$patterns) |
|
| 493 | + { |
|
| 494 | + if (is_null($routeName = $this->getName())) { |
|
| 495 | + return false; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + foreach ($patterns as $pattern) { |
|
| 499 | + if (Str::is($pattern, $routeName)) { |
|
| 500 | + return true; |
|
| 501 | + } |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + return false; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Set a default value for the route. |
|
| 509 | + * |
|
| 510 | + * @param string $key |
|
| 511 | + * @param mixed $value |
|
| 512 | + * |
|
| 513 | + * @return $this |
|
| 514 | + */ |
|
| 515 | + public function defaults($key, $value) |
|
| 516 | + { |
|
| 517 | + $this->defaults[$key] = $value; |
|
| 518 | + |
|
| 519 | + return $this; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Set a default values for the route. |
|
| 524 | + * |
|
| 525 | + * @param string $defaults |
|
| 526 | + * |
|
| 527 | + * @return $this |
|
| 528 | + */ |
|
| 529 | + public function setDefaults(array $defaults) |
|
| 530 | + { |
|
| 531 | + $this->defaults = $defaults; |
|
| 532 | + |
|
| 533 | + return $this; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Set the where. |
|
| 538 | + * |
|
| 539 | + * @param array|string $name |
|
| 540 | + * @param string|null $expression |
|
| 541 | + * |
|
| 542 | + * @return $this |
|
| 543 | + */ |
|
| 544 | + public function where($name, string $expression = null) |
|
| 545 | + { |
|
| 546 | + $wheres = is_array($name) ? $name : [$name => $expression]; |
|
| 547 | 547 | |
| 548 | - foreach ($wheres as $name => $expression) { |
|
| 549 | - $this->wheres[$name] = $expression; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - return $this; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * Bind the route to a given request for execution. |
|
| 557 | - * |
|
| 558 | - * @param \Syscodes\Http\Request $request |
|
| 559 | - * |
|
| 560 | - * @return $this |
|
| 561 | - */ |
|
| 562 | - public function bind(Request $request) |
|
| 563 | - { |
|
| 564 | - $this->parameters = (new RouteParamBinding($this))->parameters($request); |
|
| 565 | - |
|
| 566 | - return $this; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Get all of the parameter names for the route. |
|
| 571 | - * |
|
| 572 | - * @return array |
|
| 573 | - */ |
|
| 574 | - public function parameterNames() |
|
| 575 | - { |
|
| 576 | - if (isset($this->parameterNames)) { |
|
| 577 | - return $this->parameterNames; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - return $this->parameterNames = $this->compileParamNames(); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Get the parameter names for the route. |
|
| 585 | - * |
|
| 586 | - * @return array |
|
| 587 | - */ |
|
| 588 | - protected function compileParamNames() |
|
| 589 | - { |
|
| 590 | - preg_match_all('~[^\/\{(.*?)\}]~', $this->domain().$this->uri, $matches); |
|
| 591 | - |
|
| 592 | - return array_map(function ($match) { |
|
| 593 | - return trim($match, '?'); |
|
| 594 | - }, $matches[0]); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Get a given parameter from the route. |
|
| 599 | - * |
|
| 600 | - * @param string $name |
|
| 601 | - * @param mixed $default |
|
| 602 | - * |
|
| 603 | - * @return array |
|
| 604 | - */ |
|
| 605 | - public function parameter($name, $default = null) |
|
| 606 | - { |
|
| 607 | - return Arr::get($this->parameters(), $name, $default); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Set a parameter to the given value. |
|
| 612 | - * |
|
| 613 | - * @param string $name |
|
| 614 | - * @param mixed $value |
|
| 615 | - * |
|
| 616 | - * @return array |
|
| 617 | - */ |
|
| 618 | - public function setParameter($name, $value) |
|
| 619 | - { |
|
| 620 | - $this->parameters(); |
|
| 621 | - |
|
| 622 | - $this->parameters[$name] = $value; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * Get the key / value list of parameters without null values. |
|
| 627 | - * |
|
| 628 | - * @return array |
|
| 629 | - */ |
|
| 630 | - public function parametersWithouNulls() |
|
| 631 | - { |
|
| 632 | - return array_filter($this->parameters(), function ($parameter) { |
|
| 633 | - return ! is_null($parameter); |
|
| 634 | - }); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * Get the key / value list of parameters for the route. |
|
| 639 | - * |
|
| 640 | - * @return array |
|
| 641 | - */ |
|
| 642 | - public function parameters() |
|
| 643 | - { |
|
| 644 | - if (isset($this->parameters)) { |
|
| 645 | - return $this->parameters; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - throw new LogicException('The route is not bound.'); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Get all middleware, including the ones from the controller. |
|
| 653 | - * |
|
| 654 | - * @return array |
|
| 655 | - */ |
|
| 656 | - public function gatherMiddleware() |
|
| 657 | - { |
|
| 658 | - if ( ! is_null($this->computedMiddleware)) { |
|
| 659 | - return $this->computedMiddleware; |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - $this->computedMiddleware = []; |
|
| 663 | - |
|
| 664 | - return $this->computedMiddleware = array_unique(array_merge( |
|
| 665 | - $this->middleware(), |
|
| 666 | - $this->controllerMiddleware() |
|
| 667 | - ), SORT_REGULAR); |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * Get or set the middlewares attached to the route. |
|
| 672 | - * |
|
| 673 | - * @param array|string|null $middleware |
|
| 674 | - * |
|
| 675 | - * @return $this|array |
|
| 676 | - */ |
|
| 677 | - public function middleware($middleware = null) |
|
| 678 | - { |
|
| 679 | - if (is_null($middleware)) { |
|
| 680 | - return $this->getMiddleware(); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - if (is_string($middleware)) { |
|
| 684 | - $middleware = func_get_args(); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - $this->action['middleware'] = array_merge( |
|
| 688 | - $this->getMiddleware(), |
|
| 689 | - $middleware |
|
| 690 | - ); |
|
| 691 | - |
|
| 692 | - return $this; |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - /** |
|
| 696 | - * Get the middlewares attached to the route. |
|
| 697 | - * |
|
| 698 | - * @return array |
|
| 699 | - */ |
|
| 700 | - protected function getMiddleware() |
|
| 701 | - { |
|
| 702 | - return (array) ($this->action['middleware'] ?? []); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Get the middleware for the route's controller. |
|
| 707 | - * |
|
| 708 | - * @return array |
|
| 709 | - */ |
|
| 710 | - public function controllerMiddleware() |
|
| 711 | - { |
|
| 712 | - if ( ! $this->isControllerAction()) { |
|
| 713 | - return []; |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - return $this->controllerDispatcher()->getMiddleware( |
|
| 717 | - $this->getController(), |
|
| 718 | - $this->getControllerMethod() |
|
| 719 | - ); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * Determine if the route only responds to HTTP requests. |
|
| 724 | - * |
|
| 725 | - * @return bool |
|
| 726 | - */ |
|
| 727 | - public function httpOnly() |
|
| 728 | - { |
|
| 729 | - return in_array('http', $this->action, true); |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - /** |
|
| 733 | - * Determine if the route only responds to HTTPS requests. |
|
| 734 | - * |
|
| 735 | - * @return bool |
|
| 736 | - */ |
|
| 737 | - public function httpsOnly() |
|
| 738 | - { |
|
| 739 | - return $this->secure(); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Determine if the route only responds to HTTPS requests. |
|
| 744 | - * |
|
| 745 | - * @return bool |
|
| 746 | - */ |
|
| 747 | - public function secure() |
|
| 748 | - { |
|
| 749 | - return in_array('https', $this->action, true); |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - /** |
|
| 753 | - * Set the container instance on the route. |
|
| 754 | - * |
|
| 755 | - * @param \Syscodes\Container\Container $container |
|
| 756 | - * |
|
| 757 | - * @return $this |
|
| 758 | - */ |
|
| 759 | - public function setContainer(Container $container) |
|
| 760 | - { |
|
| 761 | - $this->container = $container; |
|
| 762 | - |
|
| 763 | - return $this; |
|
| 764 | - } |
|
| 765 | - |
|
| 766 | - /** |
|
| 767 | - * Dynamically access route parameters. |
|
| 768 | - * |
|
| 769 | - * @param string $key |
|
| 770 | - * |
|
| 771 | - * @return mixed |
|
| 772 | - */ |
|
| 773 | - public function __get($key) |
|
| 774 | - { |
|
| 775 | - return $this->parameter($key); |
|
| 776 | - } |
|
| 548 | + foreach ($wheres as $name => $expression) { |
|
| 549 | + $this->wheres[$name] = $expression; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + return $this; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * Bind the route to a given request for execution. |
|
| 557 | + * |
|
| 558 | + * @param \Syscodes\Http\Request $request |
|
| 559 | + * |
|
| 560 | + * @return $this |
|
| 561 | + */ |
|
| 562 | + public function bind(Request $request) |
|
| 563 | + { |
|
| 564 | + $this->parameters = (new RouteParamBinding($this))->parameters($request); |
|
| 565 | + |
|
| 566 | + return $this; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Get all of the parameter names for the route. |
|
| 571 | + * |
|
| 572 | + * @return array |
|
| 573 | + */ |
|
| 574 | + public function parameterNames() |
|
| 575 | + { |
|
| 576 | + if (isset($this->parameterNames)) { |
|
| 577 | + return $this->parameterNames; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + return $this->parameterNames = $this->compileParamNames(); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Get the parameter names for the route. |
|
| 585 | + * |
|
| 586 | + * @return array |
|
| 587 | + */ |
|
| 588 | + protected function compileParamNames() |
|
| 589 | + { |
|
| 590 | + preg_match_all('~[^\/\{(.*?)\}]~', $this->domain().$this->uri, $matches); |
|
| 591 | + |
|
| 592 | + return array_map(function ($match) { |
|
| 593 | + return trim($match, '?'); |
|
| 594 | + }, $matches[0]); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Get a given parameter from the route. |
|
| 599 | + * |
|
| 600 | + * @param string $name |
|
| 601 | + * @param mixed $default |
|
| 602 | + * |
|
| 603 | + * @return array |
|
| 604 | + */ |
|
| 605 | + public function parameter($name, $default = null) |
|
| 606 | + { |
|
| 607 | + return Arr::get($this->parameters(), $name, $default); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Set a parameter to the given value. |
|
| 612 | + * |
|
| 613 | + * @param string $name |
|
| 614 | + * @param mixed $value |
|
| 615 | + * |
|
| 616 | + * @return array |
|
| 617 | + */ |
|
| 618 | + public function setParameter($name, $value) |
|
| 619 | + { |
|
| 620 | + $this->parameters(); |
|
| 621 | + |
|
| 622 | + $this->parameters[$name] = $value; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * Get the key / value list of parameters without null values. |
|
| 627 | + * |
|
| 628 | + * @return array |
|
| 629 | + */ |
|
| 630 | + public function parametersWithouNulls() |
|
| 631 | + { |
|
| 632 | + return array_filter($this->parameters(), function ($parameter) { |
|
| 633 | + return ! is_null($parameter); |
|
| 634 | + }); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * Get the key / value list of parameters for the route. |
|
| 639 | + * |
|
| 640 | + * @return array |
|
| 641 | + */ |
|
| 642 | + public function parameters() |
|
| 643 | + { |
|
| 644 | + if (isset($this->parameters)) { |
|
| 645 | + return $this->parameters; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + throw new LogicException('The route is not bound.'); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Get all middleware, including the ones from the controller. |
|
| 653 | + * |
|
| 654 | + * @return array |
|
| 655 | + */ |
|
| 656 | + public function gatherMiddleware() |
|
| 657 | + { |
|
| 658 | + if ( ! is_null($this->computedMiddleware)) { |
|
| 659 | + return $this->computedMiddleware; |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + $this->computedMiddleware = []; |
|
| 663 | + |
|
| 664 | + return $this->computedMiddleware = array_unique(array_merge( |
|
| 665 | + $this->middleware(), |
|
| 666 | + $this->controllerMiddleware() |
|
| 667 | + ), SORT_REGULAR); |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * Get or set the middlewares attached to the route. |
|
| 672 | + * |
|
| 673 | + * @param array|string|null $middleware |
|
| 674 | + * |
|
| 675 | + * @return $this|array |
|
| 676 | + */ |
|
| 677 | + public function middleware($middleware = null) |
|
| 678 | + { |
|
| 679 | + if (is_null($middleware)) { |
|
| 680 | + return $this->getMiddleware(); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + if (is_string($middleware)) { |
|
| 684 | + $middleware = func_get_args(); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + $this->action['middleware'] = array_merge( |
|
| 688 | + $this->getMiddleware(), |
|
| 689 | + $middleware |
|
| 690 | + ); |
|
| 691 | + |
|
| 692 | + return $this; |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + /** |
|
| 696 | + * Get the middlewares attached to the route. |
|
| 697 | + * |
|
| 698 | + * @return array |
|
| 699 | + */ |
|
| 700 | + protected function getMiddleware() |
|
| 701 | + { |
|
| 702 | + return (array) ($this->action['middleware'] ?? []); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Get the middleware for the route's controller. |
|
| 707 | + * |
|
| 708 | + * @return array |
|
| 709 | + */ |
|
| 710 | + public function controllerMiddleware() |
|
| 711 | + { |
|
| 712 | + if ( ! $this->isControllerAction()) { |
|
| 713 | + return []; |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + return $this->controllerDispatcher()->getMiddleware( |
|
| 717 | + $this->getController(), |
|
| 718 | + $this->getControllerMethod() |
|
| 719 | + ); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * Determine if the route only responds to HTTP requests. |
|
| 724 | + * |
|
| 725 | + * @return bool |
|
| 726 | + */ |
|
| 727 | + public function httpOnly() |
|
| 728 | + { |
|
| 729 | + return in_array('http', $this->action, true); |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** |
|
| 733 | + * Determine if the route only responds to HTTPS requests. |
|
| 734 | + * |
|
| 735 | + * @return bool |
|
| 736 | + */ |
|
| 737 | + public function httpsOnly() |
|
| 738 | + { |
|
| 739 | + return $this->secure(); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + /** |
|
| 743 | + * Determine if the route only responds to HTTPS requests. |
|
| 744 | + * |
|
| 745 | + * @return bool |
|
| 746 | + */ |
|
| 747 | + public function secure() |
|
| 748 | + { |
|
| 749 | + return in_array('https', $this->action, true); |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + /** |
|
| 753 | + * Set the container instance on the route. |
|
| 754 | + * |
|
| 755 | + * @param \Syscodes\Container\Container $container |
|
| 756 | + * |
|
| 757 | + * @return $this |
|
| 758 | + */ |
|
| 759 | + public function setContainer(Container $container) |
|
| 760 | + { |
|
| 761 | + $this->container = $container; |
|
| 762 | + |
|
| 763 | + return $this; |
|
| 764 | + } |
|
| 765 | + |
|
| 766 | + /** |
|
| 767 | + * Dynamically access route parameters. |
|
| 768 | + * |
|
| 769 | + * @param string $key |
|
| 770 | + * |
|
| 771 | + * @return mixed |
|
| 772 | + */ |
|
| 773 | + public function __get($key) |
|
| 774 | + { |
|
| 775 | + return $this->parameter($key); |
|
| 776 | + } |
|
| 777 | 777 | } |
| 778 | 778 | \ No newline at end of file |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | |
| 54 | 54 | [$name, $parameters] = array_pad(explode(':', $name, 2), 2, null); |
| 55 | 55 | |
| 56 | - return ($map[$name] ?? $name).(! is_null($parameters) ? ':'.$parameters : ''); |
|
| 56 | + return ($map[$name] ?? $name).( ! is_null($parameters) ? ':'.$parameters : ''); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -37,117 +37,117 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | trait RouteResolver |
| 39 | 39 | { |
| 40 | - /** |
|
| 41 | - * The currently dispatched route instance. |
|
| 42 | - * |
|
| 43 | - * @var \Syscodes\Routing\Route|null |
|
| 44 | - */ |
|
| 45 | - protected $current; |
|
| 40 | + /** |
|
| 41 | + * The currently dispatched route instance. |
|
| 42 | + * |
|
| 43 | + * @var \Syscodes\Routing\Route|null |
|
| 44 | + */ |
|
| 45 | + protected $current; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Resolve the given route and call the method that belongs to the route. |
|
| 49 | - * |
|
| 50 | - * @param \Syscodes\Http\Request $request |
|
| 51 | - * |
|
| 52 | - * @return \Syscodes\Http\Response |
|
| 53 | - */ |
|
| 54 | - public function resolve(Request $request) |
|
| 55 | - { |
|
| 56 | - return $this->dispatchToRoute($request); |
|
| 57 | - } |
|
| 47 | + /** |
|
| 48 | + * Resolve the given route and call the method that belongs to the route. |
|
| 49 | + * |
|
| 50 | + * @param \Syscodes\Http\Request $request |
|
| 51 | + * |
|
| 52 | + * @return \Syscodes\Http\Response |
|
| 53 | + */ |
|
| 54 | + public function resolve(Request $request) |
|
| 55 | + { |
|
| 56 | + return $this->dispatchToRoute($request); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Dispatch the request to a route and return the response. |
|
| 61 | - * |
|
| 62 | - * @param \Syscodes\Http\Request $request |
|
| 63 | - * |
|
| 64 | - * @return \Syscodes\Http\Response |
|
| 65 | - */ |
|
| 66 | - protected function dispatchToRoute(Request $request) |
|
| 67 | - { |
|
| 68 | - return $this->runRoute($request, $this->findRoute($request)); |
|
| 69 | - } |
|
| 59 | + /** |
|
| 60 | + * Dispatch the request to a route and return the response. |
|
| 61 | + * |
|
| 62 | + * @param \Syscodes\Http\Request $request |
|
| 63 | + * |
|
| 64 | + * @return \Syscodes\Http\Response |
|
| 65 | + */ |
|
| 66 | + protected function dispatchToRoute(Request $request) |
|
| 67 | + { |
|
| 68 | + return $this->runRoute($request, $this->findRoute($request)); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Find the route matching a given request. |
|
| 73 | - * |
|
| 74 | - * @param \Syscodes\Http\Request $request |
|
| 75 | - * |
|
| 76 | - * @return \Syscodes\Routing\Route |
|
| 77 | - */ |
|
| 78 | - protected function findRoute($request) |
|
| 79 | - { |
|
| 80 | - // Get all register routes with the same request method |
|
| 81 | - $this->current = $route = $this->routes->match($request); |
|
| 71 | + /** |
|
| 72 | + * Find the route matching a given request. |
|
| 73 | + * |
|
| 74 | + * @param \Syscodes\Http\Request $request |
|
| 75 | + * |
|
| 76 | + * @return \Syscodes\Routing\Route |
|
| 77 | + */ |
|
| 78 | + protected function findRoute($request) |
|
| 79 | + { |
|
| 80 | + // Get all register routes with the same request method |
|
| 81 | + $this->current = $route = $this->routes->match($request); |
|
| 82 | 82 | |
| 83 | - $this->container->instance(Route::class, $route); |
|
| 83 | + $this->container->instance(Route::class, $route); |
|
| 84 | 84 | |
| 85 | - return $route; |
|
| 86 | - } |
|
| 85 | + return $route; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Return the response for the given route. |
|
| 90 | - * |
|
| 91 | - * @param \Syscodes\Http\Request $request |
|
| 92 | - * @param \Syscodes\Routing\Route $route |
|
| 93 | - * |
|
| 94 | - * @return \Syscodes\Http\Response |
|
| 95 | - */ |
|
| 96 | - protected function runRoute(Request $request, Route $route) |
|
| 97 | - { |
|
| 98 | - $request->setRouteResolver(function () use ($route) { |
|
| 99 | - return $route; |
|
| 100 | - }); |
|
| 88 | + /** |
|
| 89 | + * Return the response for the given route. |
|
| 90 | + * |
|
| 91 | + * @param \Syscodes\Http\Request $request |
|
| 92 | + * @param \Syscodes\Routing\Route $route |
|
| 93 | + * |
|
| 94 | + * @return \Syscodes\Http\Response |
|
| 95 | + */ |
|
| 96 | + protected function runRoute(Request $request, Route $route) |
|
| 97 | + { |
|
| 98 | + $request->setRouteResolver(function () use ($route) { |
|
| 99 | + return $route; |
|
| 100 | + }); |
|
| 101 | 101 | |
| 102 | - return $this->callResponse($request, |
|
| 103 | - $this->runRouteStack($route, $request) |
|
| 104 | - ); |
|
| 105 | - } |
|
| 102 | + return $this->callResponse($request, |
|
| 103 | + $this->runRouteStack($route, $request) |
|
| 104 | + ); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Run the given route through a stack response instance. |
|
| 109 | - * |
|
| 110 | - * @param \Syscodes\Routing\Route $route |
|
| 111 | - * @param \Syscodes\Http\Request $request |
|
| 112 | - * |
|
| 113 | - * @return mixed |
|
| 114 | - */ |
|
| 115 | - protected function runRouteStack(Route $route, Request $request) |
|
| 116 | - { |
|
| 117 | - $skipMiddleware = $this->container->bound('middleware.disable') && |
|
| 118 | - ($this->container->make('middleware.disable') === true); |
|
| 107 | + /** |
|
| 108 | + * Run the given route through a stack response instance. |
|
| 109 | + * |
|
| 110 | + * @param \Syscodes\Routing\Route $route |
|
| 111 | + * @param \Syscodes\Http\Request $request |
|
| 112 | + * |
|
| 113 | + * @return mixed |
|
| 114 | + */ |
|
| 115 | + protected function runRouteStack(Route $route, Request $request) |
|
| 116 | + { |
|
| 117 | + $skipMiddleware = $this->container->bound('middleware.disable') && |
|
| 118 | + ($this->container->make('middleware.disable') === true); |
|
| 119 | 119 | |
| 120 | 120 | |
| 121 | - $middleware = $skipMiddleware ? [] : $this->gatherRouteMiddleware($route); |
|
| 121 | + $middleware = $skipMiddleware ? [] : $this->gatherRouteMiddleware($route); |
|
| 122 | 122 | |
| 123 | - return (new Pipeline($this->container)) |
|
| 124 | - ->send($request) |
|
| 125 | - ->through($middleware) |
|
| 126 | - ->then(function ($request) use ($route) { |
|
| 127 | - return $this->callResponse( |
|
| 128 | - $request, $route->runResolver() |
|
| 129 | - ); |
|
| 130 | - }); |
|
| 131 | - } |
|
| 123 | + return (new Pipeline($this->container)) |
|
| 124 | + ->send($request) |
|
| 125 | + ->through($middleware) |
|
| 126 | + ->then(function ($request) use ($route) { |
|
| 127 | + return $this->callResponse( |
|
| 128 | + $request, $route->runResolver() |
|
| 129 | + ); |
|
| 130 | + }); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Create a response instance from the given value. |
|
| 135 | - * |
|
| 136 | - * @param \Syscodes\Http\Request $request |
|
| 137 | - * @param mixed $response |
|
| 138 | - * |
|
| 139 | - * @return \Syscodes\Http\Response |
|
| 140 | - */ |
|
| 141 | - protected function callResponse($request, $response) |
|
| 142 | - { |
|
| 143 | - if ( ! $response instanceof Response && |
|
| 144 | - ($response instanceof Jsonserializable || |
|
| 145 | - is_array($response))) { |
|
| 146 | - $response = new JsonResponse($response); |
|
| 147 | - } elseif ( ! $response instanceof Response) { |
|
| 148 | - $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
| 149 | - } |
|
| 133 | + /** |
|
| 134 | + * Create a response instance from the given value. |
|
| 135 | + * |
|
| 136 | + * @param \Syscodes\Http\Request $request |
|
| 137 | + * @param mixed $response |
|
| 138 | + * |
|
| 139 | + * @return \Syscodes\Http\Response |
|
| 140 | + */ |
|
| 141 | + protected function callResponse($request, $response) |
|
| 142 | + { |
|
| 143 | + if ( ! $response instanceof Response && |
|
| 144 | + ($response instanceof Jsonserializable || |
|
| 145 | + is_array($response))) { |
|
| 146 | + $response = new JsonResponse($response); |
|
| 147 | + } elseif ( ! $response instanceof Response) { |
|
| 148 | + $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - return $response->prepare($request); |
|
| 152 | - } |
|
| 151 | + return $response->prepare($request); |
|
| 152 | + } |
|
| 153 | 153 | } |
| 154 | 154 | \ No newline at end of file |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | protected function runRoute(Request $request, Route $route) |
| 97 | 97 | { |
| 98 | - $request->setRouteResolver(function () use ($route) { |
|
| 98 | + $request->setRouteResolver(function() use ($route) { |
|
| 99 | 99 | return $route; |
| 100 | 100 | }); |
| 101 | 101 | |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | return (new Pipeline($this->container)) |
| 124 | 124 | ->send($request) |
| 125 | 125 | ->through($middleware) |
| 126 | - ->then(function ($request) use ($route) { |
|
| 126 | + ->then(function($request) use ($route) { |
|
| 127 | 127 | return $this->callResponse( |
| 128 | 128 | $request, $route->runResolver() |
| 129 | 129 | ); |
@@ -39,651 +39,651 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class Router implements Routable |
| 41 | 41 | { |
| 42 | - use Concerns\RouteMap, |
|
| 43 | - Concerns\RouteResolver; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The registered route value binders. |
|
| 47 | - * |
|
| 48 | - * @var array $binders |
|
| 49 | - */ |
|
| 50 | - protected $binders = []; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The container instance used by the router. |
|
| 54 | - * |
|
| 55 | - * @var \Syscodes\Contracts\Container\Container $container |
|
| 56 | - */ |
|
| 57 | - protected $container; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Variable of group route. |
|
| 61 | - * |
|
| 62 | - * @var array $groupStack |
|
| 63 | - */ |
|
| 64 | - protected $groupStack = []; |
|
| 42 | + use Concerns\RouteMap, |
|
| 43 | + Concerns\RouteResolver; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The registered route value binders. |
|
| 47 | + * |
|
| 48 | + * @var array $binders |
|
| 49 | + */ |
|
| 50 | + protected $binders = []; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The container instance used by the router. |
|
| 54 | + * |
|
| 55 | + * @var \Syscodes\Contracts\Container\Container $container |
|
| 56 | + */ |
|
| 57 | + protected $container; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Variable of group route. |
|
| 61 | + * |
|
| 62 | + * @var array $groupStack |
|
| 63 | + */ |
|
| 64 | + protected $groupStack = []; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * The registered string macros. |
|
| 68 | - * |
|
| 69 | - * @var array $macros |
|
| 70 | - */ |
|
| 71 | - protected $macros = []; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Middleware for function of filters |
|
| 75 | - * |
|
| 76 | - * @var array $middleware |
|
| 77 | - */ |
|
| 78 | - protected $middleware = []; |
|
| 66 | + /** |
|
| 67 | + * The registered string macros. |
|
| 68 | + * |
|
| 69 | + * @var array $macros |
|
| 70 | + */ |
|
| 71 | + protected $macros = []; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Middleware for function of filters |
|
| 75 | + * |
|
| 76 | + * @var array $middleware |
|
| 77 | + */ |
|
| 78 | + protected $middleware = []; |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * All of the middleware groups. |
|
| 82 | - * |
|
| 83 | - * @var array $middlewareGroups |
|
| 84 | - */ |
|
| 85 | - protected $middlewareGroups = []; |
|
| 80 | + /** |
|
| 81 | + * All of the middleware groups. |
|
| 82 | + * |
|
| 83 | + * @var array $middlewareGroups |
|
| 84 | + */ |
|
| 85 | + protected $middlewareGroups = []; |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * The priority-sorted list of middleware. |
|
| 89 | - * |
|
| 90 | - * @var array $middlewarePriority |
|
| 91 | - */ |
|
| 92 | - public $middlewarePriority = []; |
|
| 87 | + /** |
|
| 88 | + * The priority-sorted list of middleware. |
|
| 89 | + * |
|
| 90 | + * @var array $middlewarePriority |
|
| 91 | + */ |
|
| 92 | + public $middlewarePriority = []; |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * The globally available parameter patterns. |
|
| 96 | - * |
|
| 97 | - * @var array $patterns |
|
| 98 | - */ |
|
| 99 | - protected $patterns = []; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * The route collection instance. |
|
| 103 | - * |
|
| 104 | - * @var \Syscodes\Routing\RouteCollection $routes |
|
| 105 | - */ |
|
| 106 | - protected $routes; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * The Resource instance. |
|
| 110 | - * |
|
| 111 | - * @var \Syscodes\Routing\ResourceRegister $resources |
|
| 112 | - */ |
|
| 113 | - protected $resources; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Constructor. Create a new Router instance. |
|
| 117 | - * |
|
| 118 | - * @param \Syscodes\Contracts\Container\Container|null $container (null by default) |
|
| 119 | - * |
|
| 120 | - * @return void |
|
| 121 | - */ |
|
| 122 | - public function __construct(Container $container = null) |
|
| 123 | - { |
|
| 124 | - $this->routes = new RouteCollection(); |
|
| 125 | - |
|
| 126 | - $this->container = $container ?: new Container; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get the prefix from the group on the stack. |
|
| 131 | - * |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function getGroupPrefix() |
|
| 135 | - { |
|
| 136 | - if ( ! empty($this->groupStack)) { |
|
| 137 | - $last = end($this->groupStack); |
|
| 138 | - |
|
| 139 | - return $last['prefix'] ?? ''; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return ''; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Group a series of routes under a single URL segment. This is handy |
|
| 147 | - * for grouping items into an admin area, like: |
|
| 148 | - * |
|
| 149 | - * Example: |
|
| 150 | - * // Creates route: /admin show the word 'User' |
|
| 151 | - * Route::group(['prefix' => 'admin'], function() { |
|
| 152 | - * |
|
| 153 | - * Route::get('/user', function() { |
|
| 154 | - * echo 'Hello world..!'; |
|
| 155 | - * }); |
|
| 156 | - * |
|
| 157 | - * }); /admin/user |
|
| 158 | - * |
|
| 159 | - * @param array $attributes |
|
| 160 | - * @param \Closure|string $callback |
|
| 161 | - * |
|
| 162 | - * @return void |
|
| 163 | - */ |
|
| 164 | - public function group(array $attributes, $callback) |
|
| 165 | - { |
|
| 166 | - $this->updateGroupStack($attributes); |
|
| 167 | - |
|
| 168 | - $this->loadRoutes($callback); |
|
| 169 | - |
|
| 170 | - array_pop($this->groupStack); |
|
| 171 | - |
|
| 172 | - return $this; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Update the group stack with the given attributes. |
|
| 177 | - * |
|
| 178 | - * @param array $attributes |
|
| 179 | - * |
|
| 180 | - * @return void |
|
| 181 | - */ |
|
| 182 | - protected function updateGroupStack(array $attributes) |
|
| 183 | - { |
|
| 184 | - if ( ! empty($this->groupStack)) { |
|
| 185 | - $attributes = $this->mergeGroup($attributes); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $this->groupStack[] = $attributes; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Merge the given group attributes. |
|
| 193 | - * |
|
| 194 | - * @param array $new |
|
| 195 | - * |
|
| 196 | - * @return array |
|
| 197 | - */ |
|
| 198 | - protected function mergeGroup($new) |
|
| 199 | - { |
|
| 200 | - return RouteGroup::mergeGroup($new, end($this->groupStack)); |
|
| 201 | - } |
|
| 94 | + /** |
|
| 95 | + * The globally available parameter patterns. |
|
| 96 | + * |
|
| 97 | + * @var array $patterns |
|
| 98 | + */ |
|
| 99 | + protected $patterns = []; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * The route collection instance. |
|
| 103 | + * |
|
| 104 | + * @var \Syscodes\Routing\RouteCollection $routes |
|
| 105 | + */ |
|
| 106 | + protected $routes; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * The Resource instance. |
|
| 110 | + * |
|
| 111 | + * @var \Syscodes\Routing\ResourceRegister $resources |
|
| 112 | + */ |
|
| 113 | + protected $resources; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Constructor. Create a new Router instance. |
|
| 117 | + * |
|
| 118 | + * @param \Syscodes\Contracts\Container\Container|null $container (null by default) |
|
| 119 | + * |
|
| 120 | + * @return void |
|
| 121 | + */ |
|
| 122 | + public function __construct(Container $container = null) |
|
| 123 | + { |
|
| 124 | + $this->routes = new RouteCollection(); |
|
| 125 | + |
|
| 126 | + $this->container = $container ?: new Container; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get the prefix from the group on the stack. |
|
| 131 | + * |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function getGroupPrefix() |
|
| 135 | + { |
|
| 136 | + if ( ! empty($this->groupStack)) { |
|
| 137 | + $last = end($this->groupStack); |
|
| 138 | + |
|
| 139 | + return $last['prefix'] ?? ''; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return ''; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Group a series of routes under a single URL segment. This is handy |
|
| 147 | + * for grouping items into an admin area, like: |
|
| 148 | + * |
|
| 149 | + * Example: |
|
| 150 | + * // Creates route: /admin show the word 'User' |
|
| 151 | + * Route::group(['prefix' => 'admin'], function() { |
|
| 152 | + * |
|
| 153 | + * Route::get('/user', function() { |
|
| 154 | + * echo 'Hello world..!'; |
|
| 155 | + * }); |
|
| 156 | + * |
|
| 157 | + * }); /admin/user |
|
| 158 | + * |
|
| 159 | + * @param array $attributes |
|
| 160 | + * @param \Closure|string $callback |
|
| 161 | + * |
|
| 162 | + * @return void |
|
| 163 | + */ |
|
| 164 | + public function group(array $attributes, $callback) |
|
| 165 | + { |
|
| 166 | + $this->updateGroupStack($attributes); |
|
| 167 | + |
|
| 168 | + $this->loadRoutes($callback); |
|
| 169 | + |
|
| 170 | + array_pop($this->groupStack); |
|
| 171 | + |
|
| 172 | + return $this; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Update the group stack with the given attributes. |
|
| 177 | + * |
|
| 178 | + * @param array $attributes |
|
| 179 | + * |
|
| 180 | + * @return void |
|
| 181 | + */ |
|
| 182 | + protected function updateGroupStack(array $attributes) |
|
| 183 | + { |
|
| 184 | + if ( ! empty($this->groupStack)) { |
|
| 185 | + $attributes = $this->mergeGroup($attributes); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $this->groupStack[] = $attributes; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Merge the given group attributes. |
|
| 193 | + * |
|
| 194 | + * @param array $new |
|
| 195 | + * |
|
| 196 | + * @return array |
|
| 197 | + */ |
|
| 198 | + protected function mergeGroup($new) |
|
| 199 | + { |
|
| 200 | + return RouteGroup::mergeGroup($new, end($this->groupStack)); |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * Load the provided routes. |
|
| 205 | - * |
|
| 206 | - * @param \Closure|string $callback |
|
| 207 | - * |
|
| 208 | - * @return void |
|
| 209 | - */ |
|
| 210 | - protected function loadRoutes($callback) |
|
| 211 | - { |
|
| 212 | - if ($callback instanceof Closure) { |
|
| 213 | - $callback($this); |
|
| 214 | - } else { |
|
| 215 | - (new RouteFileRegister($this))->register($callback); |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Add a route to the underlying route collection. |
|
| 221 | - * |
|
| 222 | - * @param array|string $method |
|
| 223 | - * @param string $route |
|
| 224 | - * @param mixed $action |
|
| 225 | - * |
|
| 226 | - * @return \Syscodes\Routing\Route |
|
| 227 | - */ |
|
| 228 | - public function addRoute($method, $route, $action) |
|
| 229 | - { |
|
| 230 | - return $this->routes->add($this->map($method, $route, $action)); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Create a redirect from one URI to another. |
|
| 235 | - * |
|
| 236 | - * @param string $uri |
|
| 237 | - * @param string $destination |
|
| 238 | - * @param int $status (302 by default) |
|
| 239 | - * |
|
| 240 | - * @return \Syscodes\Routing\Route |
|
| 241 | - */ |
|
| 242 | - public function redirect($uri, $destination, $status = 302) |
|
| 243 | - { |
|
| 244 | - return $this->any($uri, function () use ($destination, $status) { |
|
| 245 | - return new RedirectResponse($destination, $status); |
|
| 246 | - }); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Register a new route that returns a view. |
|
| 251 | - * |
|
| 252 | - * @param string $uri |
|
| 253 | - * @param string $view |
|
| 254 | - * @param array $data |
|
| 255 | - * |
|
| 256 | - * @return \Syscodes\Routing\Route |
|
| 257 | - */ |
|
| 258 | - public function view($uri, $view, $data = []) |
|
| 259 | - { |
|
| 260 | - return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
| 261 | - return $this->container->make('view')->make($view, $data); |
|
| 262 | - }); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Add new route to routes array. |
|
| 267 | - * |
|
| 268 | - * @param array|string $method |
|
| 269 | - * @param string $route |
|
| 270 | - * @param mixed $action |
|
| 271 | - * |
|
| 272 | - * @return void |
|
| 273 | - * |
|
| 274 | - * @throws \InvalidArgumentException |
|
| 275 | - */ |
|
| 276 | - public function map($method, $route, $action) |
|
| 277 | - { |
|
| 278 | - if ($this->actionReferencesController($action)) { |
|
| 279 | - $action = $this->convertToControllerAction($action); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - $route = $this->newRoute( |
|
| 283 | - array_map('strtoupper', (array) $method), |
|
| 284 | - $this->prefix($route), |
|
| 285 | - $action |
|
| 286 | - ); |
|
| 287 | - |
|
| 288 | - if ($this->hasGroupStack()) { |
|
| 289 | - $this->mergeGroupAttributesIntoRoute($route); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $this->addWhereClausesToRoute($route); |
|
| 203 | + /** |
|
| 204 | + * Load the provided routes. |
|
| 205 | + * |
|
| 206 | + * @param \Closure|string $callback |
|
| 207 | + * |
|
| 208 | + * @return void |
|
| 209 | + */ |
|
| 210 | + protected function loadRoutes($callback) |
|
| 211 | + { |
|
| 212 | + if ($callback instanceof Closure) { |
|
| 213 | + $callback($this); |
|
| 214 | + } else { |
|
| 215 | + (new RouteFileRegister($this))->register($callback); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Add a route to the underlying route collection. |
|
| 221 | + * |
|
| 222 | + * @param array|string $method |
|
| 223 | + * @param string $route |
|
| 224 | + * @param mixed $action |
|
| 225 | + * |
|
| 226 | + * @return \Syscodes\Routing\Route |
|
| 227 | + */ |
|
| 228 | + public function addRoute($method, $route, $action) |
|
| 229 | + { |
|
| 230 | + return $this->routes->add($this->map($method, $route, $action)); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Create a redirect from one URI to another. |
|
| 235 | + * |
|
| 236 | + * @param string $uri |
|
| 237 | + * @param string $destination |
|
| 238 | + * @param int $status (302 by default) |
|
| 239 | + * |
|
| 240 | + * @return \Syscodes\Routing\Route |
|
| 241 | + */ |
|
| 242 | + public function redirect($uri, $destination, $status = 302) |
|
| 243 | + { |
|
| 244 | + return $this->any($uri, function () use ($destination, $status) { |
|
| 245 | + return new RedirectResponse($destination, $status); |
|
| 246 | + }); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Register a new route that returns a view. |
|
| 251 | + * |
|
| 252 | + * @param string $uri |
|
| 253 | + * @param string $view |
|
| 254 | + * @param array $data |
|
| 255 | + * |
|
| 256 | + * @return \Syscodes\Routing\Route |
|
| 257 | + */ |
|
| 258 | + public function view($uri, $view, $data = []) |
|
| 259 | + { |
|
| 260 | + return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
| 261 | + return $this->container->make('view')->make($view, $data); |
|
| 262 | + }); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Add new route to routes array. |
|
| 267 | + * |
|
| 268 | + * @param array|string $method |
|
| 269 | + * @param string $route |
|
| 270 | + * @param mixed $action |
|
| 271 | + * |
|
| 272 | + * @return void |
|
| 273 | + * |
|
| 274 | + * @throws \InvalidArgumentException |
|
| 275 | + */ |
|
| 276 | + public function map($method, $route, $action) |
|
| 277 | + { |
|
| 278 | + if ($this->actionReferencesController($action)) { |
|
| 279 | + $action = $this->convertToControllerAction($action); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + $route = $this->newRoute( |
|
| 283 | + array_map('strtoupper', (array) $method), |
|
| 284 | + $this->prefix($route), |
|
| 285 | + $action |
|
| 286 | + ); |
|
| 287 | + |
|
| 288 | + if ($this->hasGroupStack()) { |
|
| 289 | + $this->mergeGroupAttributesIntoRoute($route); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $this->addWhereClausesToRoute($route); |
|
| 293 | 293 | |
| 294 | - return $route; |
|
| 295 | - } |
|
| 294 | + return $route; |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - /** |
|
| 298 | - * Determine if the action is routing to a controller. |
|
| 299 | - * |
|
| 300 | - * @param array $action |
|
| 301 | - * |
|
| 302 | - * @return bool |
|
| 303 | - */ |
|
| 304 | - protected function actionReferencesController($action) |
|
| 305 | - { |
|
| 306 | - if ($action instanceof Closure) { |
|
| 307 | - return false; |
|
| 308 | - } |
|
| 297 | + /** |
|
| 298 | + * Determine if the action is routing to a controller. |
|
| 299 | + * |
|
| 300 | + * @param array $action |
|
| 301 | + * |
|
| 302 | + * @return bool |
|
| 303 | + */ |
|
| 304 | + protected function actionReferencesController($action) |
|
| 305 | + { |
|
| 306 | + if ($action instanceof Closure) { |
|
| 307 | + return false; |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | - return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
| 311 | - } |
|
| 310 | + return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - /** |
|
| 314 | - * Add a controller based route action to the action array. |
|
| 315 | - * |
|
| 316 | - * @param array|string $action |
|
| 317 | - * |
|
| 318 | - * @return array |
|
| 319 | - */ |
|
| 320 | - protected function convertToControllerAction($action) |
|
| 321 | - { |
|
| 322 | - if (is_string($action)) { |
|
| 323 | - $action = ['uses' => $action]; |
|
| 324 | - } |
|
| 313 | + /** |
|
| 314 | + * Add a controller based route action to the action array. |
|
| 315 | + * |
|
| 316 | + * @param array|string $action |
|
| 317 | + * |
|
| 318 | + * @return array |
|
| 319 | + */ |
|
| 320 | + protected function convertToControllerAction($action) |
|
| 321 | + { |
|
| 322 | + if (is_string($action)) { |
|
| 323 | + $action = ['uses' => $action]; |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | - if ( ! empty($this->groupStack)) { |
|
| 327 | - $action['uses'] = $this->prependGroupUses($action['uses']); |
|
| 328 | - } |
|
| 326 | + if ( ! empty($this->groupStack)) { |
|
| 327 | + $action['uses'] = $this->prependGroupUses($action['uses']); |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - $action['controller'] = $action['uses']; |
|
| 330 | + $action['controller'] = $action['uses']; |
|
| 331 | 331 | |
| 332 | - return $action; |
|
| 333 | - } |
|
| 332 | + return $action; |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Prepend the last group uses onto the use clause. |
|
| 337 | - * |
|
| 338 | - * @param string $uses |
|
| 339 | - * |
|
| 340 | - * @return string |
|
| 341 | - */ |
|
| 342 | - protected function prependGroupUses($uses) |
|
| 343 | - { |
|
| 344 | - $group = end($this->groupStack); |
|
| 335 | + /** |
|
| 336 | + * Prepend the last group uses onto the use clause. |
|
| 337 | + * |
|
| 338 | + * @param string $uses |
|
| 339 | + * |
|
| 340 | + * @return string |
|
| 341 | + */ |
|
| 342 | + protected function prependGroupUses($uses) |
|
| 343 | + { |
|
| 344 | + $group = end($this->groupStack); |
|
| 345 | 345 | |
| 346 | - return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Create a new Route object. |
|
| 351 | - * |
|
| 352 | - * @param array|string $method |
|
| 353 | - * @param string $uri |
|
| 354 | - * @param mixed $action |
|
| 355 | - * |
|
| 356 | - * @return \Syscodes\Routing\Route |
|
| 357 | - */ |
|
| 358 | - public function newRoute($method, $uri, $action) |
|
| 359 | - { |
|
| 360 | - return take(new Route($method, $uri, $action)) |
|
| 361 | - ->setContainer($this->container); |
|
| 362 | - } |
|
| 346 | + return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Create a new Route object. |
|
| 351 | + * |
|
| 352 | + * @param array|string $method |
|
| 353 | + * @param string $uri |
|
| 354 | + * @param mixed $action |
|
| 355 | + * |
|
| 356 | + * @return \Syscodes\Routing\Route |
|
| 357 | + */ |
|
| 358 | + public function newRoute($method, $uri, $action) |
|
| 359 | + { |
|
| 360 | + return take(new Route($method, $uri, $action)) |
|
| 361 | + ->setContainer($this->container); |
|
| 362 | + } |
|
| 363 | 363 | |
| 364 | - /** |
|
| 365 | - * Determine if the router currently has a group stack. |
|
| 366 | - * |
|
| 367 | - * @return bool |
|
| 368 | - */ |
|
| 369 | - public function hasGroupStack() |
|
| 370 | - { |
|
| 371 | - return ! empty($this->groupStack); |
|
| 372 | - } |
|
| 364 | + /** |
|
| 365 | + * Determine if the router currently has a group stack. |
|
| 366 | + * |
|
| 367 | + * @return bool |
|
| 368 | + */ |
|
| 369 | + public function hasGroupStack() |
|
| 370 | + { |
|
| 371 | + return ! empty($this->groupStack); |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - /** |
|
| 375 | - * Merge the group stack with the controller action. |
|
| 376 | - * |
|
| 377 | - * @param \Syscpde\Routing\Route $route |
|
| 378 | - * |
|
| 379 | - * @return void |
|
| 380 | - */ |
|
| 381 | - protected function mergeGroupAttributesIntoRoute($route) |
|
| 382 | - { |
|
| 383 | - $action = static::mergeGroup($route->getAction(), end($this->groupStack)); |
|
| 374 | + /** |
|
| 375 | + * Merge the group stack with the controller action. |
|
| 376 | + * |
|
| 377 | + * @param \Syscpde\Routing\Route $route |
|
| 378 | + * |
|
| 379 | + * @return void |
|
| 380 | + */ |
|
| 381 | + protected function mergeGroupAttributesIntoRoute($route) |
|
| 382 | + { |
|
| 383 | + $action = static::mergeGroup($route->getAction(), end($this->groupStack)); |
|
| 384 | 384 | |
| 385 | - $route->setAction($action); |
|
| 386 | - } |
|
| 385 | + $route->setAction($action); |
|
| 386 | + } |
|
| 387 | 387 | |
| 388 | - /** |
|
| 389 | - * Add the necessary where clauses to the route based on its initial registration. |
|
| 390 | - * |
|
| 391 | - * @param \Syscodes\Routing\Route $route |
|
| 392 | - * |
|
| 393 | - * @return \Syscodes\Routing\Route |
|
| 394 | - */ |
|
| 395 | - protected function addWhereClausesToRoute($route) |
|
| 396 | - { |
|
| 397 | - return $route->where(array_merge( |
|
| 398 | - $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
| 399 | - )); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Add a prefix to the route URI. |
|
| 404 | - * |
|
| 405 | - * @param string $uri |
|
| 406 | - * |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - protected function prefix($uri) |
|
| 410 | - { |
|
| 411 | - $uri = is_null($uri) ? '' : trim($uri, '/').'/'; |
|
| 412 | - |
|
| 413 | - $uri = filter_var($uri, FILTER_SANITIZE_STRING); |
|
| 414 | - |
|
| 415 | - // While we want to add a route within a group of '/', |
|
| 416 | - // it doens't work with matching, so remove them... |
|
| 417 | - if ($uri != '/') { |
|
| 418 | - $uri = ltrim($uri, '/'); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Set a global where pattern on all routes. |
|
| 426 | - * |
|
| 427 | - * @param string $name |
|
| 428 | - * @param string $pattern |
|
| 429 | - * |
|
| 430 | - * @return void |
|
| 431 | - */ |
|
| 432 | - public function pattern($name, $pattern) |
|
| 433 | - { |
|
| 434 | - return $this->patterns[$name] = $pattern; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Set a group of global where patterns on all routes. |
|
| 439 | - * |
|
| 440 | - * @param array $patterns |
|
| 441 | - * |
|
| 442 | - * @return void |
|
| 443 | - */ |
|
| 444 | - public function patterns($patterns) |
|
| 445 | - { |
|
| 446 | - foreach ($patterns as $key => $pattern) { |
|
| 447 | - $this->patterns[$key] = $pattern; |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Get a Resource instance. |
|
| 453 | - * |
|
| 454 | - * @return \Syscodes\Routing\ResourceRegister |
|
| 455 | - */ |
|
| 456 | - public function getResource() |
|
| 457 | - { |
|
| 458 | - if (isset($this->resources)) { |
|
| 459 | - return $this->resources; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - return $this->resources = new ResourceRegister($this); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Dispatches the given url and call the method that belongs to the route. |
|
| 467 | - * |
|
| 468 | - * @param \Syscodes\Http\Request $request |
|
| 469 | - * |
|
| 470 | - * @return mixed |
|
| 471 | - */ |
|
| 472 | - public function dispatch(Request $request) |
|
| 473 | - { |
|
| 474 | - return $this->resolve($request); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Gather the middleware for the given route. |
|
| 479 | - * |
|
| 480 | - * @param \Syscodes\Routing\Route $route |
|
| 481 | - * |
|
| 482 | - * @return array |
|
| 483 | - */ |
|
| 484 | - public function gatherRouteMiddleware(Route $route) |
|
| 485 | - { |
|
| 486 | - $middleware = array_map(function ($name) { |
|
| 388 | + /** |
|
| 389 | + * Add the necessary where clauses to the route based on its initial registration. |
|
| 390 | + * |
|
| 391 | + * @param \Syscodes\Routing\Route $route |
|
| 392 | + * |
|
| 393 | + * @return \Syscodes\Routing\Route |
|
| 394 | + */ |
|
| 395 | + protected function addWhereClausesToRoute($route) |
|
| 396 | + { |
|
| 397 | + return $route->where(array_merge( |
|
| 398 | + $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
| 399 | + )); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Add a prefix to the route URI. |
|
| 404 | + * |
|
| 405 | + * @param string $uri |
|
| 406 | + * |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + protected function prefix($uri) |
|
| 410 | + { |
|
| 411 | + $uri = is_null($uri) ? '' : trim($uri, '/').'/'; |
|
| 412 | + |
|
| 413 | + $uri = filter_var($uri, FILTER_SANITIZE_STRING); |
|
| 414 | + |
|
| 415 | + // While we want to add a route within a group of '/', |
|
| 416 | + // it doens't work with matching, so remove them... |
|
| 417 | + if ($uri != '/') { |
|
| 418 | + $uri = ltrim($uri, '/'); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Set a global where pattern on all routes. |
|
| 426 | + * |
|
| 427 | + * @param string $name |
|
| 428 | + * @param string $pattern |
|
| 429 | + * |
|
| 430 | + * @return void |
|
| 431 | + */ |
|
| 432 | + public function pattern($name, $pattern) |
|
| 433 | + { |
|
| 434 | + return $this->patterns[$name] = $pattern; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Set a group of global where patterns on all routes. |
|
| 439 | + * |
|
| 440 | + * @param array $patterns |
|
| 441 | + * |
|
| 442 | + * @return void |
|
| 443 | + */ |
|
| 444 | + public function patterns($patterns) |
|
| 445 | + { |
|
| 446 | + foreach ($patterns as $key => $pattern) { |
|
| 447 | + $this->patterns[$key] = $pattern; |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Get a Resource instance. |
|
| 453 | + * |
|
| 454 | + * @return \Syscodes\Routing\ResourceRegister |
|
| 455 | + */ |
|
| 456 | + public function getResource() |
|
| 457 | + { |
|
| 458 | + if (isset($this->resources)) { |
|
| 459 | + return $this->resources; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + return $this->resources = new ResourceRegister($this); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Dispatches the given url and call the method that belongs to the route. |
|
| 467 | + * |
|
| 468 | + * @param \Syscodes\Http\Request $request |
|
| 469 | + * |
|
| 470 | + * @return mixed |
|
| 471 | + */ |
|
| 472 | + public function dispatch(Request $request) |
|
| 473 | + { |
|
| 474 | + return $this->resolve($request); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Gather the middleware for the given route. |
|
| 479 | + * |
|
| 480 | + * @param \Syscodes\Routing\Route $route |
|
| 481 | + * |
|
| 482 | + * @return array |
|
| 483 | + */ |
|
| 484 | + public function gatherRouteMiddleware(Route $route) |
|
| 485 | + { |
|
| 486 | + $middleware = array_map(function ($name) { |
|
| 487 | 487 | return MiddlewareResolver::resolve($name, $this->middleware, $this->middlewareGroups); |
| 488 | 488 | }, $route->gatherMiddleware()); |
| 489 | 489 | |
| 490 | 490 | return Arr::flatten($middleware); |
| 491 | - } |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * Get all of the defined middleware |
|
| 495 | - * |
|
| 496 | - * @return array |
|
| 497 | - */ |
|
| 498 | - public function getMiddleware() |
|
| 499 | - { |
|
| 500 | - return $this->middleware; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * Register a short-hand name for a middleware. |
|
| 505 | - * |
|
| 506 | - * @param string $name |
|
| 507 | - * @param string $class |
|
| 508 | - * |
|
| 509 | - * @return $this |
|
| 510 | - */ |
|
| 511 | - public function aliasMiddleware($name, $class) |
|
| 512 | - { |
|
| 513 | - $this->middleware[$name] = $class; |
|
| 514 | - |
|
| 515 | - return $this; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Register a group of middleware. |
|
| 520 | - * |
|
| 521 | - * @param string $name |
|
| 522 | - * @param array $middleware |
|
| 523 | - * |
|
| 524 | - * @return $this |
|
| 525 | - */ |
|
| 526 | - public function middlewareGroup($name, array $middleware) |
|
| 527 | - { |
|
| 528 | - $this->middlewareGroups[$name] = $middleware; |
|
| 529 | - |
|
| 530 | - return $this; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * Check if a route with the given name exists. |
|
| 535 | - * |
|
| 536 | - * @param string $name |
|
| 537 | - * |
|
| 538 | - * @return bool |
|
| 539 | - */ |
|
| 540 | - public function has($name) |
|
| 541 | - { |
|
| 542 | - $names = is_array($name) ? $name : func_get_args(); |
|
| 543 | - |
|
| 544 | - foreach ($names as $value) { |
|
| 545 | - if ( ! $this->routes->hasNamedRoute($value)) { |
|
| 546 | - return false; |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - return true; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * Get the currently dispatched route instance. |
|
| 555 | - * |
|
| 556 | - * @return \Syscodes\Routing\Route|null |
|
| 557 | - */ |
|
| 558 | - public function current() |
|
| 559 | - { |
|
| 560 | - return $this->current; |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * Determine if the current route matches a pattern. |
|
| 565 | - * |
|
| 566 | - * @param mixed ...$patterns |
|
| 567 | - * |
|
| 568 | - * @return bool |
|
| 569 | - */ |
|
| 570 | - public function is(...$patterns) |
|
| 571 | - { |
|
| 572 | - return $this->currentRouteNamed(...$patterns); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Determine if the current route matches a pattern. |
|
| 577 | - * |
|
| 578 | - * @param mixed ...$patterns |
|
| 579 | - * |
|
| 580 | - * @return bool |
|
| 581 | - */ |
|
| 582 | - public function currentRouteNamed(...$patterns) |
|
| 583 | - { |
|
| 584 | - return $this->current() && $this->current()->named(...$patterns); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Register an array of resource controllers. |
|
| 589 | - * |
|
| 590 | - * @param array $resources |
|
| 591 | - * @param array $options |
|
| 592 | - * |
|
| 593 | - * @return void |
|
| 594 | - */ |
|
| 595 | - public function resources(array $resources, array $options = []) |
|
| 596 | - { |
|
| 597 | - foreach ($resources as $name => $controller) { |
|
| 598 | - $this->resource($name, $controller, $options); |
|
| 599 | - } |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Route a resource to a controller. |
|
| 604 | - * |
|
| 605 | - * @param string $name |
|
| 606 | - * @param string $controller |
|
| 607 | - * @param array $options |
|
| 608 | - * |
|
| 609 | - * @return \Syscodes\Routing\AwaitingResourceRegistration |
|
| 610 | - */ |
|
| 611 | - public function resource($name, $controller, array $options = []) |
|
| 612 | - { |
|
| 613 | - if ($this->container) { |
|
| 614 | - $register = $this->container->make(ResourceRegister::class); |
|
| 615 | - } else { |
|
| 616 | - $register = new ResourceRegister($this); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - return new AwaitingResourceRegistration( |
|
| 620 | - $register, $name, $controller, $options |
|
| 621 | - ); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * Get the route collection. |
|
| 626 | - * |
|
| 627 | - * @return array |
|
| 628 | - */ |
|
| 629 | - public function getRoutes() |
|
| 630 | - { |
|
| 631 | - return $this->routes; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * Get or set the verbs used in the resource URIs. |
|
| 636 | - * |
|
| 637 | - * @param array $verbs |
|
| 638 | - * |
|
| 639 | - * @return array|null |
|
| 640 | - */ |
|
| 641 | - public function resourceVerbs(array $verbs = []) |
|
| 642 | - { |
|
| 643 | - ResourceRegister::verbs($verbs); |
|
| 644 | - } |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * Get all of the defined middleware |
|
| 495 | + * |
|
| 496 | + * @return array |
|
| 497 | + */ |
|
| 498 | + public function getMiddleware() |
|
| 499 | + { |
|
| 500 | + return $this->middleware; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * Register a short-hand name for a middleware. |
|
| 505 | + * |
|
| 506 | + * @param string $name |
|
| 507 | + * @param string $class |
|
| 508 | + * |
|
| 509 | + * @return $this |
|
| 510 | + */ |
|
| 511 | + public function aliasMiddleware($name, $class) |
|
| 512 | + { |
|
| 513 | + $this->middleware[$name] = $class; |
|
| 514 | + |
|
| 515 | + return $this; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Register a group of middleware. |
|
| 520 | + * |
|
| 521 | + * @param string $name |
|
| 522 | + * @param array $middleware |
|
| 523 | + * |
|
| 524 | + * @return $this |
|
| 525 | + */ |
|
| 526 | + public function middlewareGroup($name, array $middleware) |
|
| 527 | + { |
|
| 528 | + $this->middlewareGroups[$name] = $middleware; |
|
| 529 | + |
|
| 530 | + return $this; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * Check if a route with the given name exists. |
|
| 535 | + * |
|
| 536 | + * @param string $name |
|
| 537 | + * |
|
| 538 | + * @return bool |
|
| 539 | + */ |
|
| 540 | + public function has($name) |
|
| 541 | + { |
|
| 542 | + $names = is_array($name) ? $name : func_get_args(); |
|
| 543 | + |
|
| 544 | + foreach ($names as $value) { |
|
| 545 | + if ( ! $this->routes->hasNamedRoute($value)) { |
|
| 546 | + return false; |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + return true; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * Get the currently dispatched route instance. |
|
| 555 | + * |
|
| 556 | + * @return \Syscodes\Routing\Route|null |
|
| 557 | + */ |
|
| 558 | + public function current() |
|
| 559 | + { |
|
| 560 | + return $this->current; |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * Determine if the current route matches a pattern. |
|
| 565 | + * |
|
| 566 | + * @param mixed ...$patterns |
|
| 567 | + * |
|
| 568 | + * @return bool |
|
| 569 | + */ |
|
| 570 | + public function is(...$patterns) |
|
| 571 | + { |
|
| 572 | + return $this->currentRouteNamed(...$patterns); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * Determine if the current route matches a pattern. |
|
| 577 | + * |
|
| 578 | + * @param mixed ...$patterns |
|
| 579 | + * |
|
| 580 | + * @return bool |
|
| 581 | + */ |
|
| 582 | + public function currentRouteNamed(...$patterns) |
|
| 583 | + { |
|
| 584 | + return $this->current() && $this->current()->named(...$patterns); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Register an array of resource controllers. |
|
| 589 | + * |
|
| 590 | + * @param array $resources |
|
| 591 | + * @param array $options |
|
| 592 | + * |
|
| 593 | + * @return void |
|
| 594 | + */ |
|
| 595 | + public function resources(array $resources, array $options = []) |
|
| 596 | + { |
|
| 597 | + foreach ($resources as $name => $controller) { |
|
| 598 | + $this->resource($name, $controller, $options); |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Route a resource to a controller. |
|
| 604 | + * |
|
| 605 | + * @param string $name |
|
| 606 | + * @param string $controller |
|
| 607 | + * @param array $options |
|
| 608 | + * |
|
| 609 | + * @return \Syscodes\Routing\AwaitingResourceRegistration |
|
| 610 | + */ |
|
| 611 | + public function resource($name, $controller, array $options = []) |
|
| 612 | + { |
|
| 613 | + if ($this->container) { |
|
| 614 | + $register = $this->container->make(ResourceRegister::class); |
|
| 615 | + } else { |
|
| 616 | + $register = new ResourceRegister($this); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + return new AwaitingResourceRegistration( |
|
| 620 | + $register, $name, $controller, $options |
|
| 621 | + ); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * Get the route collection. |
|
| 626 | + * |
|
| 627 | + * @return array |
|
| 628 | + */ |
|
| 629 | + public function getRoutes() |
|
| 630 | + { |
|
| 631 | + return $this->routes; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * Get or set the verbs used in the resource URIs. |
|
| 636 | + * |
|
| 637 | + * @param array $verbs |
|
| 638 | + * |
|
| 639 | + * @return array|null |
|
| 640 | + */ |
|
| 641 | + public function resourceVerbs(array $verbs = []) |
|
| 642 | + { |
|
| 643 | + ResourceRegister::verbs($verbs); |
|
| 644 | + } |
|
| 645 | 645 | |
| 646 | - /** |
|
| 647 | - * Register a custom macro. |
|
| 648 | - * |
|
| 649 | - * @param string $name |
|
| 650 | - * @param callable $callback |
|
| 651 | - * |
|
| 652 | - * @return void |
|
| 653 | - */ |
|
| 654 | - public function macro($name, callable $callback) |
|
| 655 | - { |
|
| 656 | - $this->macros[$name] = $callback; |
|
| 657 | - } |
|
| 646 | + /** |
|
| 647 | + * Register a custom macro. |
|
| 648 | + * |
|
| 649 | + * @param string $name |
|
| 650 | + * @param callable $callback |
|
| 651 | + * |
|
| 652 | + * @return void |
|
| 653 | + */ |
|
| 654 | + public function macro($name, callable $callback) |
|
| 655 | + { |
|
| 656 | + $this->macros[$name] = $callback; |
|
| 657 | + } |
|
| 658 | 658 | |
| 659 | - /** |
|
| 660 | - * Checks if macro is registered. |
|
| 661 | - * |
|
| 662 | - * @param string $name |
|
| 663 | - * |
|
| 664 | - * @return boolean |
|
| 665 | - */ |
|
| 666 | - public function hasMacro($name) |
|
| 667 | - { |
|
| 668 | - return isset($this->macros[$name]); |
|
| 669 | - } |
|
| 659 | + /** |
|
| 660 | + * Checks if macro is registered. |
|
| 661 | + * |
|
| 662 | + * @param string $name |
|
| 663 | + * |
|
| 664 | + * @return boolean |
|
| 665 | + */ |
|
| 666 | + public function hasMacro($name) |
|
| 667 | + { |
|
| 668 | + return isset($this->macros[$name]); |
|
| 669 | + } |
|
| 670 | 670 | |
| 671 | - /** |
|
| 672 | - * Dynamically handle calls into the router instance. |
|
| 673 | - * |
|
| 674 | - * @param string $method |
|
| 675 | - * @param array $parameters |
|
| 676 | - * |
|
| 677 | - * @return mixed |
|
| 678 | - */ |
|
| 679 | - public function __call($method, $parameters) |
|
| 680 | - { |
|
| 681 | - if (isset($this->macros[$method])) { |
|
| 682 | - $callback = $this->macros[$method]; |
|
| 683 | - |
|
| 684 | - return call_user_func_array($callback, $parameters); |
|
| 685 | - } |
|
| 671 | + /** |
|
| 672 | + * Dynamically handle calls into the router instance. |
|
| 673 | + * |
|
| 674 | + * @param string $method |
|
| 675 | + * @param array $parameters |
|
| 676 | + * |
|
| 677 | + * @return mixed |
|
| 678 | + */ |
|
| 679 | + public function __call($method, $parameters) |
|
| 680 | + { |
|
| 681 | + if (isset($this->macros[$method])) { |
|
| 682 | + $callback = $this->macros[$method]; |
|
| 683 | + |
|
| 684 | + return call_user_func_array($callback, $parameters); |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
| 688 | - } |
|
| 687 | + return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
| 688 | + } |
|
| 689 | 689 | } |
| 690 | 690 | \ No newline at end of file |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | */ |
| 242 | 242 | public function redirect($uri, $destination, $status = 302) |
| 243 | 243 | { |
| 244 | - return $this->any($uri, function () use ($destination, $status) { |
|
| 244 | + return $this->any($uri, function() use ($destination, $status) { |
|
| 245 | 245 | return new RedirectResponse($destination, $status); |
| 246 | 246 | }); |
| 247 | 247 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | */ |
| 258 | 258 | public function view($uri, $view, $data = []) |
| 259 | 259 | { |
| 260 | - return $this->match(['GET', 'HEAD'], $uri, function () use ($view, $data) { |
|
| 260 | + return $this->match(['GET', 'HEAD'], $uri, function() use ($view, $data) { |
|
| 261 | 261 | return $this->container->make('view')->make($view, $data); |
| 262 | 262 | }); |
| 263 | 263 | } |
@@ -483,7 +483,7 @@ discard block |
||
| 483 | 483 | */ |
| 484 | 484 | public function gatherRouteMiddleware(Route $route) |
| 485 | 485 | { |
| 486 | - $middleware = array_map(function ($name) { |
|
| 486 | + $middleware = array_map(function($name) { |
|
| 487 | 487 | return MiddlewareResolver::resolve($name, $this->middleware, $this->middlewareGroups); |
| 488 | 488 | }, $route->gatherMiddleware()); |
| 489 | 489 | |
@@ -94,7 +94,7 @@ |
||
| 94 | 94 | |
| 95 | 95 | $middleware = $controller->getMiddleware(); |
| 96 | 96 | |
| 97 | - return collect($middleware)->filter(function ($data) use ($method) { |
|
| 97 | + return collect($middleware)->filter(function($data) use ($method) { |
|
| 98 | 98 | return static::methodExcludedByOptions($method, $data['options']); |
| 99 | 99 | })->all(); |
| 100 | 100 | } |
@@ -41,891 +41,891 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | class Request |
| 43 | 43 | { |
| 44 | - /** |
|
| 45 | - * Holds the global active request instance. |
|
| 46 | - * |
|
| 47 | - * @var bool $requestURI |
|
| 48 | - */ |
|
| 49 | - protected static $requestURI; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The base URL. |
|
| 53 | - * |
|
| 54 | - * @var string $baseUrl |
|
| 55 | - */ |
|
| 56 | - protected $baseUrl; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Gets cookies ($_COOKIE). |
|
| 60 | - * |
|
| 61 | - * @var string $cookies |
|
| 62 | - */ |
|
| 63 | - public $cookies; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Gets the string with format JSON. |
|
| 67 | - * |
|
| 68 | - * @var string|resource|null $content |
|
| 69 | - */ |
|
| 70 | - protected $content; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * The default Locale this request. |
|
| 74 | - * |
|
| 75 | - * @var string $defaultLocale |
|
| 76 | - */ |
|
| 77 | - protected $defaultLocale = 'en'; |
|
| 44 | + /** |
|
| 45 | + * Holds the global active request instance. |
|
| 46 | + * |
|
| 47 | + * @var bool $requestURI |
|
| 48 | + */ |
|
| 49 | + protected static $requestURI; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The base URL. |
|
| 53 | + * |
|
| 54 | + * @var string $baseUrl |
|
| 55 | + */ |
|
| 56 | + protected $baseUrl; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Gets cookies ($_COOKIE). |
|
| 60 | + * |
|
| 61 | + * @var string $cookies |
|
| 62 | + */ |
|
| 63 | + public $cookies; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Gets the string with format JSON. |
|
| 67 | + * |
|
| 68 | + * @var string|resource|null $content |
|
| 69 | + */ |
|
| 70 | + protected $content; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * The default Locale this request. |
|
| 74 | + * |
|
| 75 | + * @var string $defaultLocale |
|
| 76 | + */ |
|
| 77 | + protected $defaultLocale = 'en'; |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Gets files request ($_FILES). |
|
| 81 | - * |
|
| 82 | - * @var string $files |
|
| 83 | - */ |
|
| 84 | - public $files; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * The detected uri and server variables. |
|
| 88 | - * |
|
| 89 | - * @var string $http |
|
| 90 | - */ |
|
| 91 | - protected $http; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * The decoded JSON content for the request. |
|
| 95 | - * |
|
| 96 | - * @var \Syscodes\Http\Contributors\Parameters|null $json |
|
| 97 | - */ |
|
| 98 | - protected $json; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * The current language of the application. |
|
| 102 | - * |
|
| 103 | - * @var string $languages |
|
| 104 | - */ |
|
| 105 | - protected $languages; |
|
| 79 | + /** |
|
| 80 | + * Gets files request ($_FILES). |
|
| 81 | + * |
|
| 82 | + * @var string $files |
|
| 83 | + */ |
|
| 84 | + public $files; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * The detected uri and server variables. |
|
| 88 | + * |
|
| 89 | + * @var string $http |
|
| 90 | + */ |
|
| 91 | + protected $http; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * The decoded JSON content for the request. |
|
| 95 | + * |
|
| 96 | + * @var \Syscodes\Http\Contributors\Parameters|null $json |
|
| 97 | + */ |
|
| 98 | + protected $json; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * The current language of the application. |
|
| 102 | + * |
|
| 103 | + * @var string $languages |
|
| 104 | + */ |
|
| 105 | + protected $languages; |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * The method name. |
|
| 109 | - * |
|
| 110 | - * @var string $method |
|
| 111 | - */ |
|
| 112 | - protected $method; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * The path info of URL. |
|
| 116 | - * |
|
| 117 | - * @var string $pathInfo |
|
| 118 | - */ |
|
| 119 | - protected $pathInfo; |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Request body parameters ($_POST). |
|
| 123 | - * |
|
| 124 | - * @var \Syscodes\Http\Contributors\Parameters $request |
|
| 125 | - */ |
|
| 126 | - public $request; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get request URI. |
|
| 130 | - * |
|
| 131 | - * @var string $requestToURI |
|
| 132 | - */ |
|
| 133 | - protected $requestToURI; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Get the route resolver callback. |
|
| 137 | - * |
|
| 138 | - * @var \Closure $routeResolver |
|
| 139 | - */ |
|
| 140 | - protected $routeResolver; |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * The detected uri and server variables ($_FILES). |
|
| 144 | - * |
|
| 145 | - * @var array $server |
|
| 146 | - */ |
|
| 147 | - public $server = []; |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * List of routes uri. |
|
| 151 | - * |
|
| 152 | - * @var string|array $uri |
|
| 153 | - */ |
|
| 154 | - public $uri; |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Stores the valid locale codes. |
|
| 158 | - * |
|
| 159 | - * @var array $validLocales |
|
| 160 | - */ |
|
| 161 | - protected $validLocales = []; |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Constructor: Create new the Request class. |
|
| 165 | - * |
|
| 166 | - * @param array $request |
|
| 167 | - * @param array $cookies |
|
| 168 | - * @param array $files |
|
| 169 | - * @param array $server |
|
| 170 | - * @param string|resource|null $content |
|
| 171 | - * |
|
| 172 | - * @return void |
|
| 173 | - */ |
|
| 174 | - public function __construct(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
| 175 | - { |
|
| 176 | - static::$requestURI = $this; |
|
| 107 | + /** |
|
| 108 | + * The method name. |
|
| 109 | + * |
|
| 110 | + * @var string $method |
|
| 111 | + */ |
|
| 112 | + protected $method; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * The path info of URL. |
|
| 116 | + * |
|
| 117 | + * @var string $pathInfo |
|
| 118 | + */ |
|
| 119 | + protected $pathInfo; |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Request body parameters ($_POST). |
|
| 123 | + * |
|
| 124 | + * @var \Syscodes\Http\Contributors\Parameters $request |
|
| 125 | + */ |
|
| 126 | + public $request; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get request URI. |
|
| 130 | + * |
|
| 131 | + * @var string $requestToURI |
|
| 132 | + */ |
|
| 133 | + protected $requestToURI; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Get the route resolver callback. |
|
| 137 | + * |
|
| 138 | + * @var \Closure $routeResolver |
|
| 139 | + */ |
|
| 140 | + protected $routeResolver; |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * The detected uri and server variables ($_FILES). |
|
| 144 | + * |
|
| 145 | + * @var array $server |
|
| 146 | + */ |
|
| 147 | + public $server = []; |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * List of routes uri. |
|
| 151 | + * |
|
| 152 | + * @var string|array $uri |
|
| 153 | + */ |
|
| 154 | + public $uri; |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Stores the valid locale codes. |
|
| 158 | + * |
|
| 159 | + * @var array $validLocales |
|
| 160 | + */ |
|
| 161 | + protected $validLocales = []; |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Constructor: Create new the Request class. |
|
| 165 | + * |
|
| 166 | + * @param array $request |
|
| 167 | + * @param array $cookies |
|
| 168 | + * @param array $files |
|
| 169 | + * @param array $server |
|
| 170 | + * @param string|resource|null $content |
|
| 171 | + * |
|
| 172 | + * @return void |
|
| 173 | + */ |
|
| 174 | + public function __construct(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
| 175 | + { |
|
| 176 | + static::$requestURI = $this; |
|
| 177 | 177 | |
| 178 | - $this->initialize($request, $cookies, $files, $server, $content); |
|
| 179 | - |
|
| 180 | - $this->detectURI(config('app.uriProtocol'), config('app.baseUrl')); |
|
| 181 | - |
|
| 182 | - $this->detectLocale(); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Sets the parameters for this request. |
|
| 187 | - * |
|
| 188 | - * @param array $request |
|
| 189 | - * @param array $cookies |
|
| 190 | - * @param array $files |
|
| 191 | - * @param array $server |
|
| 192 | - * |
|
| 193 | - * @return void |
|
| 194 | - */ |
|
| 195 | - public function initialize(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
| 196 | - { |
|
| 197 | - $this->request = new Parameters($request); |
|
| 198 | - $this->cookies = new Inputs($cookies); |
|
| 199 | - $this->files = new Files($files); |
|
| 200 | - $this->server = new Server($server); |
|
| 201 | - $this->headers = new Headers($this->server->all()); |
|
| 202 | - |
|
| 203 | - $this->uri = new URI; |
|
| 204 | - $this->http = new Http; |
|
| 205 | - $this->method = null; |
|
| 206 | - $this->baseUrl = null; |
|
| 207 | - $this->content = $content; |
|
| 208 | - $this->pathInfo = null; |
|
| 209 | - $this->languages = null; |
|
| 210 | - $this->validLocales = config('app.supportedLocales'); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Create a new Syscodes HTTP request from server variables. |
|
| 215 | - * |
|
| 216 | - * @return static |
|
| 217 | - */ |
|
| 218 | - public static function capture() |
|
| 219 | - { |
|
| 220 | - return static::createFromRequest(static::createFromRequestGlobals()); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Creates an Syscodes request from of the Request class instance. |
|
| 225 | - * |
|
| 226 | - * @param \Syscodes\Http\Request $request |
|
| 227 | - * |
|
| 228 | - * @return static |
|
| 229 | - */ |
|
| 230 | - public static function createFromRequest($request) |
|
| 231 | - { |
|
| 232 | - $newRequest = (new static)->duplicate( |
|
| 233 | - $request->request->all(), $request->cookies->all(), |
|
| 234 | - $request->files->all(), $request->server->all() |
|
| 235 | - ); |
|
| 236 | - |
|
| 237 | - $newRequest->content = $request->content; |
|
| 238 | - |
|
| 239 | - return $newRequest; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Creates a new request with value from PHP's super global. |
|
| 244 | - * |
|
| 245 | - * @return static |
|
| 246 | - */ |
|
| 247 | - public static function createFromRequestGlobals() |
|
| 248 | - { |
|
| 249 | - $request = static::createFromRequestFactory($_POST, $_COOKIE, $_FILES, $_SERVER); |
|
| 250 | - |
|
| 251 | - return $request; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Creates a new request from a factory. |
|
| 256 | - * |
|
| 257 | - * @param array $request |
|
| 258 | - * @param array $cookies |
|
| 259 | - * @param array $files |
|
| 260 | - * @param array $server |
|
| 261 | - * |
|
| 262 | - * @return static |
|
| 263 | - */ |
|
| 264 | - protected static function createFromRequestFactory(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
| 265 | - { |
|
| 266 | - if (self::$requestURI) { |
|
| 267 | - $request = (self::$requestURI)($request, $cookies, $files, $server); |
|
| 268 | - |
|
| 269 | - if ( ! $request instanceof self) { |
|
| 270 | - throw new LogicException('The Request active must return an instance of Syscodes\Http\Request'); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return $request; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return new static($request, $cookies, $files, $server); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Clones a request and overrides some of its parameters. |
|
| 281 | - * |
|
| 282 | - * @param array $request |
|
| 283 | - * @param array $cookies |
|
| 284 | - * @param array $files |
|
| 285 | - * @param array $server |
|
| 286 | - * |
|
| 287 | - * @return static |
|
| 288 | - */ |
|
| 289 | - public function duplicate(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
| 290 | - { |
|
| 291 | - $duplicate = clone $this; |
|
| 292 | - |
|
| 293 | - if (null !== $request) { |
|
| 294 | - $duplicate->request = new Parameters($request); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - if (null !== $cookies) { |
|
| 298 | - $duplicate->cookies = new Inputs($cookies); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - if (null !== $files) { |
|
| 302 | - $duplicate->files = new Files($files); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - if (null !== $server) { |
|
| 306 | - $duplicate->server = new Server($server); |
|
| 307 | - $duplicate->headers = new Headers($duplicate->server->all()); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $duplicate->uri = new URI; |
|
| 311 | - $duplicate->http = new Http; |
|
| 312 | - $duplicate->locale = null; |
|
| 313 | - $duplicate->method = null; |
|
| 314 | - $duplicate->baseUrl = null; |
|
| 315 | - $duplicate->pathInfo = null; |
|
| 316 | - $duplicate->validLocales = config('app.supportedLocales'); |
|
| 317 | - |
|
| 318 | - return $duplicate; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Returns the active request currently being used. |
|
| 323 | - * |
|
| 324 | - * @param \Syscodes\Http\Request|bool|null $request Overwrite current request |
|
| 325 | - * before returning, false prevents |
|
| 326 | - * overwrite |
|
| 327 | - * |
|
| 328 | - * @return \Syscodes\Http\Request |
|
| 329 | - */ |
|
| 330 | - public static function active($request = false) |
|
| 331 | - { |
|
| 332 | - if ($request !== false) { |
|
| 333 | - static::$requestURI = $request; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - return static::$requestURI; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Returns the desired segment, or $default if it does not exist. |
|
| 341 | - * |
|
| 342 | - * @param int $index The segment number (1-based index) |
|
| 343 | - * @param mixed $default Default value to return |
|
| 344 | - * |
|
| 345 | - * @return string |
|
| 346 | - */ |
|
| 347 | - public function segment($index, $default = null) |
|
| 348 | - { |
|
| 349 | - if ($request = static::active()) { |
|
| 350 | - return $request->uri->getSegment($index, $default); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - return null; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Returns all segments in an array. For total of segments |
|
| 358 | - * used the function PHP count(). |
|
| 359 | - * |
|
| 360 | - * @return array |
|
| 361 | - */ |
|
| 362 | - public function segments() |
|
| 363 | - { |
|
| 364 | - if ($request = static::active()) { |
|
| 365 | - return $request->uri->getSegments(); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - return null; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Returns the total number of segment. |
|
| 373 | - * |
|
| 374 | - * @return int|null |
|
| 375 | - */ |
|
| 376 | - public function totalSegments() |
|
| 377 | - { |
|
| 378 | - if ($request = static::active()) { |
|
| 379 | - return $request->uri->getTotalSegments(); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - return null; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Detects and returns the current URI based on a number of different server variables. |
|
| 387 | - * |
|
| 388 | - * @param string $protocol |
|
| 389 | - * @param string $baseUrl |
|
| 390 | - * |
|
| 391 | - * @return string |
|
| 392 | - */ |
|
| 393 | - protected function detectURI(string $protocol, string $baseUrl) |
|
| 394 | - { |
|
| 395 | - $this->uri->setPath($this->http->detectPath($protocol)); |
|
| 396 | - |
|
| 397 | - $baseUrl = ! empty($baseUrl) ? rtrim($baseUrl, '/ ').'/' : $baseUrl; |
|
| 398 | - |
|
| 399 | - if ( ! empty($baseUrl)) { |
|
| 400 | - $this->uri->setScheme(parse_url($baseUrl, PHP_URL_SCHEME)); |
|
| 401 | - $this->uri->setHost(parse_url($baseUrl, PHP_URL_HOST)); |
|
| 402 | - $this->uri->setPort(parse_url($baseUrl, PHP_URL_PORT)); |
|
| 403 | - } else { |
|
| 404 | - if ( ! $this->http->isCli()) { |
|
| 405 | - exit('You have an empty or invalid base URL. The baseURL value must be set in config/app.php, or through the .env file.'); |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Handles setting up the locale, auto-detecting of language. |
|
| 412 | - * |
|
| 413 | - * @return void |
|
| 414 | - */ |
|
| 415 | - public function detectLocale() |
|
| 416 | - { |
|
| 417 | - $this->languages = $this->defaultLocale = config('app.locale'); |
|
| 418 | - |
|
| 419 | - $this->setLocale($this->validLocales); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Returns the default locale as set. |
|
| 424 | - * |
|
| 425 | - * @return string |
|
| 426 | - */ |
|
| 427 | - public function getDefaultLocale() |
|
| 428 | - { |
|
| 429 | - return $this->defaultLocale; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Gets the current locale, with a fallback to the default. |
|
| 434 | - * |
|
| 435 | - * @return string |
|
| 436 | - */ |
|
| 437 | - public function getLocale() |
|
| 438 | - { |
|
| 439 | - return $this->languages ?: $this->defaultLocale; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Sets the locale string for this request. |
|
| 444 | - * |
|
| 445 | - * @param string $locale |
|
| 446 | - * |
|
| 447 | - * @return \Syscodes\Http\Request |
|
| 448 | - */ |
|
| 449 | - public function setLocale($locale) |
|
| 450 | - { |
|
| 451 | - if ( ! in_array($locale, $this->validLocales)) { |
|
| 452 | - $locale = $this->defaultLocale; |
|
| 453 | - } |
|
| 178 | + $this->initialize($request, $cookies, $files, $server, $content); |
|
| 179 | + |
|
| 180 | + $this->detectURI(config('app.uriProtocol'), config('app.baseUrl')); |
|
| 181 | + |
|
| 182 | + $this->detectLocale(); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Sets the parameters for this request. |
|
| 187 | + * |
|
| 188 | + * @param array $request |
|
| 189 | + * @param array $cookies |
|
| 190 | + * @param array $files |
|
| 191 | + * @param array $server |
|
| 192 | + * |
|
| 193 | + * @return void |
|
| 194 | + */ |
|
| 195 | + public function initialize(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
| 196 | + { |
|
| 197 | + $this->request = new Parameters($request); |
|
| 198 | + $this->cookies = new Inputs($cookies); |
|
| 199 | + $this->files = new Files($files); |
|
| 200 | + $this->server = new Server($server); |
|
| 201 | + $this->headers = new Headers($this->server->all()); |
|
| 202 | + |
|
| 203 | + $this->uri = new URI; |
|
| 204 | + $this->http = new Http; |
|
| 205 | + $this->method = null; |
|
| 206 | + $this->baseUrl = null; |
|
| 207 | + $this->content = $content; |
|
| 208 | + $this->pathInfo = null; |
|
| 209 | + $this->languages = null; |
|
| 210 | + $this->validLocales = config('app.supportedLocales'); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Create a new Syscodes HTTP request from server variables. |
|
| 215 | + * |
|
| 216 | + * @return static |
|
| 217 | + */ |
|
| 218 | + public static function capture() |
|
| 219 | + { |
|
| 220 | + return static::createFromRequest(static::createFromRequestGlobals()); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Creates an Syscodes request from of the Request class instance. |
|
| 225 | + * |
|
| 226 | + * @param \Syscodes\Http\Request $request |
|
| 227 | + * |
|
| 228 | + * @return static |
|
| 229 | + */ |
|
| 230 | + public static function createFromRequest($request) |
|
| 231 | + { |
|
| 232 | + $newRequest = (new static)->duplicate( |
|
| 233 | + $request->request->all(), $request->cookies->all(), |
|
| 234 | + $request->files->all(), $request->server->all() |
|
| 235 | + ); |
|
| 236 | + |
|
| 237 | + $newRequest->content = $request->content; |
|
| 238 | + |
|
| 239 | + return $newRequest; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Creates a new request with value from PHP's super global. |
|
| 244 | + * |
|
| 245 | + * @return static |
|
| 246 | + */ |
|
| 247 | + public static function createFromRequestGlobals() |
|
| 248 | + { |
|
| 249 | + $request = static::createFromRequestFactory($_POST, $_COOKIE, $_FILES, $_SERVER); |
|
| 250 | + |
|
| 251 | + return $request; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Creates a new request from a factory. |
|
| 256 | + * |
|
| 257 | + * @param array $request |
|
| 258 | + * @param array $cookies |
|
| 259 | + * @param array $files |
|
| 260 | + * @param array $server |
|
| 261 | + * |
|
| 262 | + * @return static |
|
| 263 | + */ |
|
| 264 | + protected static function createFromRequestFactory(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
| 265 | + { |
|
| 266 | + if (self::$requestURI) { |
|
| 267 | + $request = (self::$requestURI)($request, $cookies, $files, $server); |
|
| 268 | + |
|
| 269 | + if ( ! $request instanceof self) { |
|
| 270 | + throw new LogicException('The Request active must return an instance of Syscodes\Http\Request'); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return $request; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + return new static($request, $cookies, $files, $server); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Clones a request and overrides some of its parameters. |
|
| 281 | + * |
|
| 282 | + * @param array $request |
|
| 283 | + * @param array $cookies |
|
| 284 | + * @param array $files |
|
| 285 | + * @param array $server |
|
| 286 | + * |
|
| 287 | + * @return static |
|
| 288 | + */ |
|
| 289 | + public function duplicate(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
| 290 | + { |
|
| 291 | + $duplicate = clone $this; |
|
| 292 | + |
|
| 293 | + if (null !== $request) { |
|
| 294 | + $duplicate->request = new Parameters($request); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + if (null !== $cookies) { |
|
| 298 | + $duplicate->cookies = new Inputs($cookies); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + if (null !== $files) { |
|
| 302 | + $duplicate->files = new Files($files); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + if (null !== $server) { |
|
| 306 | + $duplicate->server = new Server($server); |
|
| 307 | + $duplicate->headers = new Headers($duplicate->server->all()); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + $duplicate->uri = new URI; |
|
| 311 | + $duplicate->http = new Http; |
|
| 312 | + $duplicate->locale = null; |
|
| 313 | + $duplicate->method = null; |
|
| 314 | + $duplicate->baseUrl = null; |
|
| 315 | + $duplicate->pathInfo = null; |
|
| 316 | + $duplicate->validLocales = config('app.supportedLocales'); |
|
| 317 | + |
|
| 318 | + return $duplicate; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Returns the active request currently being used. |
|
| 323 | + * |
|
| 324 | + * @param \Syscodes\Http\Request|bool|null $request Overwrite current request |
|
| 325 | + * before returning, false prevents |
|
| 326 | + * overwrite |
|
| 327 | + * |
|
| 328 | + * @return \Syscodes\Http\Request |
|
| 329 | + */ |
|
| 330 | + public static function active($request = false) |
|
| 331 | + { |
|
| 332 | + if ($request !== false) { |
|
| 333 | + static::$requestURI = $request; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + return static::$requestURI; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Returns the desired segment, or $default if it does not exist. |
|
| 341 | + * |
|
| 342 | + * @param int $index The segment number (1-based index) |
|
| 343 | + * @param mixed $default Default value to return |
|
| 344 | + * |
|
| 345 | + * @return string |
|
| 346 | + */ |
|
| 347 | + public function segment($index, $default = null) |
|
| 348 | + { |
|
| 349 | + if ($request = static::active()) { |
|
| 350 | + return $request->uri->getSegment($index, $default); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + return null; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Returns all segments in an array. For total of segments |
|
| 358 | + * used the function PHP count(). |
|
| 359 | + * |
|
| 360 | + * @return array |
|
| 361 | + */ |
|
| 362 | + public function segments() |
|
| 363 | + { |
|
| 364 | + if ($request = static::active()) { |
|
| 365 | + return $request->uri->getSegments(); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + return null; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Returns the total number of segment. |
|
| 373 | + * |
|
| 374 | + * @return int|null |
|
| 375 | + */ |
|
| 376 | + public function totalSegments() |
|
| 377 | + { |
|
| 378 | + if ($request = static::active()) { |
|
| 379 | + return $request->uri->getTotalSegments(); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + return null; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Detects and returns the current URI based on a number of different server variables. |
|
| 387 | + * |
|
| 388 | + * @param string $protocol |
|
| 389 | + * @param string $baseUrl |
|
| 390 | + * |
|
| 391 | + * @return string |
|
| 392 | + */ |
|
| 393 | + protected function detectURI(string $protocol, string $baseUrl) |
|
| 394 | + { |
|
| 395 | + $this->uri->setPath($this->http->detectPath($protocol)); |
|
| 396 | + |
|
| 397 | + $baseUrl = ! empty($baseUrl) ? rtrim($baseUrl, '/ ').'/' : $baseUrl; |
|
| 398 | + |
|
| 399 | + if ( ! empty($baseUrl)) { |
|
| 400 | + $this->uri->setScheme(parse_url($baseUrl, PHP_URL_SCHEME)); |
|
| 401 | + $this->uri->setHost(parse_url($baseUrl, PHP_URL_HOST)); |
|
| 402 | + $this->uri->setPort(parse_url($baseUrl, PHP_URL_PORT)); |
|
| 403 | + } else { |
|
| 404 | + if ( ! $this->http->isCli()) { |
|
| 405 | + exit('You have an empty or invalid base URL. The baseURL value must be set in config/app.php, or through the .env file.'); |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Handles setting up the locale, auto-detecting of language. |
|
| 412 | + * |
|
| 413 | + * @return void |
|
| 414 | + */ |
|
| 415 | + public function detectLocale() |
|
| 416 | + { |
|
| 417 | + $this->languages = $this->defaultLocale = config('app.locale'); |
|
| 418 | + |
|
| 419 | + $this->setLocale($this->validLocales); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Returns the default locale as set. |
|
| 424 | + * |
|
| 425 | + * @return string |
|
| 426 | + */ |
|
| 427 | + public function getDefaultLocale() |
|
| 428 | + { |
|
| 429 | + return $this->defaultLocale; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Gets the current locale, with a fallback to the default. |
|
| 434 | + * |
|
| 435 | + * @return string |
|
| 436 | + */ |
|
| 437 | + public function getLocale() |
|
| 438 | + { |
|
| 439 | + return $this->languages ?: $this->defaultLocale; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Sets the locale string for this request. |
|
| 444 | + * |
|
| 445 | + * @param string $locale |
|
| 446 | + * |
|
| 447 | + * @return \Syscodes\Http\Request |
|
| 448 | + */ |
|
| 449 | + public function setLocale($locale) |
|
| 450 | + { |
|
| 451 | + if ( ! in_array($locale, $this->validLocales)) { |
|
| 452 | + $locale = $this->defaultLocale; |
|
| 453 | + } |
|
| 454 | 454 | |
| 455 | - $this->languages = $locale; |
|
| 456 | - |
|
| 457 | - try { |
|
| 458 | - if (class_exists('Locale', false)) { |
|
| 459 | - Locale::setDefault($locale); |
|
| 460 | - } |
|
| 461 | - } catch (Exception $exception) {} |
|
| 462 | - |
|
| 463 | - return $this; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Returns the full request string. |
|
| 468 | - * |
|
| 469 | - * @return string|null The Request string |
|
| 470 | - */ |
|
| 471 | - public function get() |
|
| 472 | - { |
|
| 473 | - if ($request = static::active()) { |
|
| 474 | - return $request->uri->get(); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - return null; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * Get the JSON payload for the request. |
|
| 482 | - * |
|
| 483 | - * @param string|null $key |
|
| 484 | - * @param mixed $default |
|
| 485 | - * |
|
| 486 | - * @return \Syscodes\Http\Contributors\Parameters|mixed |
|
| 487 | - */ |
|
| 488 | - public function json($key = null, $default = null) |
|
| 489 | - { |
|
| 490 | - if ( ! isset($this->json)) { |
|
| 491 | - $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - if (is_null($key)) { |
|
| 495 | - return $this->json; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - return Arr::get($this->json->all(), $key, $default); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * Set the JSON payload for the request |
|
| 503 | - * |
|
| 504 | - * @param \Syscodes\Http\Contributors\Parameters $json |
|
| 505 | - * |
|
| 506 | - * @return $this |
|
| 507 | - */ |
|
| 508 | - public function setJson($json) |
|
| 509 | - { |
|
| 510 | - $this->json = $json; |
|
| 511 | - |
|
| 512 | - return $this; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * Returns whether this is an AJAX request or not. |
|
| 517 | - * |
|
| 518 | - * @return bool |
|
| 519 | - */ |
|
| 520 | - public function isXmlHttpRequest() |
|
| 521 | - { |
|
| 522 | - return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
| 523 | - strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Returns the input method used (GET, POST, DELETE, etc.). |
|
| 528 | - * |
|
| 529 | - * @return string |
|
| 530 | - * |
|
| 531 | - * @throws \LogicException |
|
| 532 | - */ |
|
| 533 | - public function getmethod() |
|
| 534 | - { |
|
| 535 | - if (null !== $this->method) { |
|
| 536 | - return $this->method; |
|
| 537 | - } |
|
| 455 | + $this->languages = $locale; |
|
| 456 | + |
|
| 457 | + try { |
|
| 458 | + if (class_exists('Locale', false)) { |
|
| 459 | + Locale::setDefault($locale); |
|
| 460 | + } |
|
| 461 | + } catch (Exception $exception) {} |
|
| 462 | + |
|
| 463 | + return $this; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Returns the full request string. |
|
| 468 | + * |
|
| 469 | + * @return string|null The Request string |
|
| 470 | + */ |
|
| 471 | + public function get() |
|
| 472 | + { |
|
| 473 | + if ($request = static::active()) { |
|
| 474 | + return $request->uri->get(); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + return null; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * Get the JSON payload for the request. |
|
| 482 | + * |
|
| 483 | + * @param string|null $key |
|
| 484 | + * @param mixed $default |
|
| 485 | + * |
|
| 486 | + * @return \Syscodes\Http\Contributors\Parameters|mixed |
|
| 487 | + */ |
|
| 488 | + public function json($key = null, $default = null) |
|
| 489 | + { |
|
| 490 | + if ( ! isset($this->json)) { |
|
| 491 | + $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + if (is_null($key)) { |
|
| 495 | + return $this->json; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + return Arr::get($this->json->all(), $key, $default); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * Set the JSON payload for the request |
|
| 503 | + * |
|
| 504 | + * @param \Syscodes\Http\Contributors\Parameters $json |
|
| 505 | + * |
|
| 506 | + * @return $this |
|
| 507 | + */ |
|
| 508 | + public function setJson($json) |
|
| 509 | + { |
|
| 510 | + $this->json = $json; |
|
| 511 | + |
|
| 512 | + return $this; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * Returns whether this is an AJAX request or not. |
|
| 517 | + * |
|
| 518 | + * @return bool |
|
| 519 | + */ |
|
| 520 | + public function isXmlHttpRequest() |
|
| 521 | + { |
|
| 522 | + return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
| 523 | + strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Returns the input method used (GET, POST, DELETE, etc.). |
|
| 528 | + * |
|
| 529 | + * @return string |
|
| 530 | + * |
|
| 531 | + * @throws \LogicException |
|
| 532 | + */ |
|
| 533 | + public function getmethod() |
|
| 534 | + { |
|
| 535 | + if (null !== $this->method) { |
|
| 536 | + return $this->method; |
|
| 537 | + } |
|
| 538 | 538 | |
| 539 | - $method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
| 539 | + $method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
| 540 | 540 | |
| 541 | - if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
| 542 | - return $this->method = $method; |
|
| 543 | - } |
|
| 541 | + if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
| 542 | + return $this->method = $method; |
|
| 543 | + } |
|
| 544 | 544 | |
| 545 | - if ( ! preg_match('~^[A-Z]++$#~D', $method)) { |
|
| 546 | - throw new logicException(sprintf('Invalid method override "%s"', $method)); |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - return $this->method = $method; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * Sets the request method. |
|
| 554 | - * |
|
| 555 | - * @param string $method |
|
| 556 | - * |
|
| 557 | - * @return string |
|
| 558 | - */ |
|
| 559 | - public function setMethod(string $method) |
|
| 560 | - { |
|
| 561 | - $this->method = null; |
|
| 562 | - |
|
| 563 | - $this->server->set('REQUEST_METHOD', $method); |
|
| 564 | - } |
|
| 545 | + if ( ! preg_match('~^[A-Z]++$#~D', $method)) { |
|
| 546 | + throw new logicException(sprintf('Invalid method override "%s"', $method)); |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + return $this->method = $method; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * Sets the request method. |
|
| 554 | + * |
|
| 555 | + * @param string $method |
|
| 556 | + * |
|
| 557 | + * @return string |
|
| 558 | + */ |
|
| 559 | + public function setMethod(string $method) |
|
| 560 | + { |
|
| 561 | + $this->method = null; |
|
| 562 | + |
|
| 563 | + $this->server->set('REQUEST_METHOD', $method); |
|
| 564 | + } |
|
| 565 | 565 | |
| 566 | - /** |
|
| 567 | - * Determine if the current request URI matches a pattern. |
|
| 568 | - * |
|
| 569 | - * @param mixed ...$patterns |
|
| 570 | - * |
|
| 571 | - * @return bool |
|
| 572 | - */ |
|
| 573 | - public function is(...$patterns) |
|
| 574 | - { |
|
| 575 | - $path = $this->decodedPath(); |
|
| 566 | + /** |
|
| 567 | + * Determine if the current request URI matches a pattern. |
|
| 568 | + * |
|
| 569 | + * @param mixed ...$patterns |
|
| 570 | + * |
|
| 571 | + * @return bool |
|
| 572 | + */ |
|
| 573 | + public function is(...$patterns) |
|
| 574 | + { |
|
| 575 | + $path = $this->decodedPath(); |
|
| 576 | 576 | |
| 577 | - foreach ($patterns as $pattern) { |
|
| 578 | - if (Str::is($pattern, $path)) { |
|
| 579 | - return true; |
|
| 580 | - } |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - return false; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Determine if the route name matches a given pattern. |
|
| 588 | - * |
|
| 589 | - * @param mixed ...$patterns |
|
| 590 | - * |
|
| 591 | - * @return bool |
|
| 592 | - */ |
|
| 593 | - public function routeIs(...$patterns) |
|
| 594 | - { |
|
| 595 | - return $this->route() && $this->route()->is(...$patterns); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Get the route handling the request. |
|
| 600 | - * |
|
| 601 | - * @param string|null $param |
|
| 602 | - * @param mixed $default |
|
| 603 | - * |
|
| 604 | - * @return \Syscodes\Routing\Route|object|string|null |
|
| 605 | - */ |
|
| 606 | - public function route($param = null, $default = null) |
|
| 607 | - { |
|
| 608 | - $route = call_user_func($this->getRouteResolver()); |
|
| 609 | - |
|
| 610 | - if (is_null($route) || is_null($param)) { |
|
| 611 | - return $route; |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - return $route->parameter($param, $default); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Get the current decoded path info for the request. |
|
| 619 | - * |
|
| 620 | - * @return string |
|
| 621 | - */ |
|
| 622 | - public function decodedPath() |
|
| 623 | - { |
|
| 624 | - return rawurldecode($this->path()); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * Get the current path info for the request. |
|
| 629 | - * |
|
| 630 | - * @return string |
|
| 631 | - */ |
|
| 632 | - public function path() |
|
| 633 | - { |
|
| 634 | - $path = trim($this->getPathInfo(), '/'); |
|
| 635 | - |
|
| 636 | - return $path == '' ? '/' : $path; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Retunrs the request body content. |
|
| 641 | - * |
|
| 642 | - * @return string |
|
| 643 | - */ |
|
| 644 | - public function getContent() |
|
| 645 | - { |
|
| 646 | - if (null === $this->content || false === $this->content) |
|
| 647 | - { |
|
| 648 | - $this->content = file_get_contents('php://input'); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - return $this->content; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * Returns the path being requested relative to the executed script. |
|
| 656 | - * |
|
| 657 | - * @return string |
|
| 658 | - */ |
|
| 659 | - public function getPathInfo() |
|
| 660 | - { |
|
| 661 | - if (null === $this->pathInfo) |
|
| 662 | - { |
|
| 663 | - $this->pathInfo = $this->http->parsePathInfo(); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - return $this->pathInfo; |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Returns the root URL from which this request is executed. |
|
| 671 | - * |
|
| 672 | - * @return string |
|
| 673 | - */ |
|
| 674 | - public function getBaseUrl() |
|
| 675 | - { |
|
| 676 | - if (null === $this->baseUrl) |
|
| 677 | - { |
|
| 678 | - $this->baseUrl = $this->http->parseBaseUrl(); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - return $this->baseUrl; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - /** |
|
| 685 | - * Returns the requested URI. |
|
| 686 | - * |
|
| 687 | - * @return string |
|
| 688 | - */ |
|
| 689 | - public function getRequestUri() |
|
| 690 | - { |
|
| 691 | - if (null === $this->requestToUri) { |
|
| 692 | - $this->requestToUri = $this->http->parseRequestUri(); |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - return $this->requestToUri; |
|
| 696 | - } |
|
| 577 | + foreach ($patterns as $pattern) { |
|
| 578 | + if (Str::is($pattern, $path)) { |
|
| 579 | + return true; |
|
| 580 | + } |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + return false; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Determine if the route name matches a given pattern. |
|
| 588 | + * |
|
| 589 | + * @param mixed ...$patterns |
|
| 590 | + * |
|
| 591 | + * @return bool |
|
| 592 | + */ |
|
| 593 | + public function routeIs(...$patterns) |
|
| 594 | + { |
|
| 595 | + return $this->route() && $this->route()->is(...$patterns); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Get the route handling the request. |
|
| 600 | + * |
|
| 601 | + * @param string|null $param |
|
| 602 | + * @param mixed $default |
|
| 603 | + * |
|
| 604 | + * @return \Syscodes\Routing\Route|object|string|null |
|
| 605 | + */ |
|
| 606 | + public function route($param = null, $default = null) |
|
| 607 | + { |
|
| 608 | + $route = call_user_func($this->getRouteResolver()); |
|
| 609 | + |
|
| 610 | + if (is_null($route) || is_null($param)) { |
|
| 611 | + return $route; |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + return $route->parameter($param, $default); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Get the current decoded path info for the request. |
|
| 619 | + * |
|
| 620 | + * @return string |
|
| 621 | + */ |
|
| 622 | + public function decodedPath() |
|
| 623 | + { |
|
| 624 | + return rawurldecode($this->path()); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * Get the current path info for the request. |
|
| 629 | + * |
|
| 630 | + * @return string |
|
| 631 | + */ |
|
| 632 | + public function path() |
|
| 633 | + { |
|
| 634 | + $path = trim($this->getPathInfo(), '/'); |
|
| 635 | + |
|
| 636 | + return $path == '' ? '/' : $path; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Retunrs the request body content. |
|
| 641 | + * |
|
| 642 | + * @return string |
|
| 643 | + */ |
|
| 644 | + public function getContent() |
|
| 645 | + { |
|
| 646 | + if (null === $this->content || false === $this->content) |
|
| 647 | + { |
|
| 648 | + $this->content = file_get_contents('php://input'); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + return $this->content; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * Returns the path being requested relative to the executed script. |
|
| 656 | + * |
|
| 657 | + * @return string |
|
| 658 | + */ |
|
| 659 | + public function getPathInfo() |
|
| 660 | + { |
|
| 661 | + if (null === $this->pathInfo) |
|
| 662 | + { |
|
| 663 | + $this->pathInfo = $this->http->parsePathInfo(); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + return $this->pathInfo; |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Returns the root URL from which this request is executed. |
|
| 671 | + * |
|
| 672 | + * @return string |
|
| 673 | + */ |
|
| 674 | + public function getBaseUrl() |
|
| 675 | + { |
|
| 676 | + if (null === $this->baseUrl) |
|
| 677 | + { |
|
| 678 | + $this->baseUrl = $this->http->parseBaseUrl(); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + return $this->baseUrl; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + /** |
|
| 685 | + * Returns the requested URI. |
|
| 686 | + * |
|
| 687 | + * @return string |
|
| 688 | + */ |
|
| 689 | + public function getRequestUri() |
|
| 690 | + { |
|
| 691 | + if (null === $this->requestToUri) { |
|
| 692 | + $this->requestToUri = $this->http->parseRequestUri(); |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + return $this->requestToUri; |
|
| 696 | + } |
|
| 697 | 697 | |
| 698 | - /** |
|
| 699 | - * Gets the request's scheme. |
|
| 700 | - * |
|
| 701 | - * @return string |
|
| 702 | - */ |
|
| 703 | - public function getScheme() |
|
| 704 | - { |
|
| 705 | - return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - /** |
|
| 709 | - * Returns the host name. |
|
| 710 | - * |
|
| 711 | - * @return void |
|
| 712 | - */ |
|
| 713 | - public function getHost() |
|
| 714 | - { |
|
| 715 | - if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
| 716 | - $host = $forawardedHost[0]; |
|
| 717 | - } elseif ( ! $host = $this->headers->get('HOST')) { |
|
| 718 | - if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
| 719 | - $host = $this->server->get('REMOTE_ADDR', ''); |
|
| 720 | - } |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - $host = $_SERVER['SERVER_NAME']; |
|
| 724 | - |
|
| 725 | - $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
| 698 | + /** |
|
| 699 | + * Gets the request's scheme. |
|
| 700 | + * |
|
| 701 | + * @return string |
|
| 702 | + */ |
|
| 703 | + public function getScheme() |
|
| 704 | + { |
|
| 705 | + return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + /** |
|
| 709 | + * Returns the host name. |
|
| 710 | + * |
|
| 711 | + * @return void |
|
| 712 | + */ |
|
| 713 | + public function getHost() |
|
| 714 | + { |
|
| 715 | + if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
| 716 | + $host = $forawardedHost[0]; |
|
| 717 | + } elseif ( ! $host = $this->headers->get('HOST')) { |
|
| 718 | + if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
| 719 | + $host = $this->server->get('REMOTE_ADDR', ''); |
|
| 720 | + } |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + $host = $_SERVER['SERVER_NAME']; |
|
| 724 | + |
|
| 725 | + $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
| 726 | 726 | |
| 727 | - return $host; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * Returns the port on which the request is made. |
|
| 732 | - * |
|
| 733 | - * @return int |
|
| 734 | - */ |
|
| 735 | - public function getPort() |
|
| 736 | - { |
|
| 737 | - if ( ! $this->server->get('HTTP_HOST')) |
|
| 738 | - { |
|
| 739 | - return $this->server->get('SERVER_PORT'); |
|
| 740 | - } |
|
| 727 | + return $host; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * Returns the port on which the request is made. |
|
| 732 | + * |
|
| 733 | + * @return int |
|
| 734 | + */ |
|
| 735 | + public function getPort() |
|
| 736 | + { |
|
| 737 | + if ( ! $this->server->get('HTTP_HOST')) |
|
| 738 | + { |
|
| 739 | + return $this->server->get('SERVER_PORT'); |
|
| 740 | + } |
|
| 741 | 741 | |
| 742 | - return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - /** |
|
| 746 | - * Returns the HTTP host being requested. |
|
| 747 | - * |
|
| 748 | - * @return string |
|
| 749 | - */ |
|
| 750 | - public function getHttpHost() |
|
| 751 | - { |
|
| 752 | - $scheme = $this->getScheme(); |
|
| 753 | - $port = $this->getPort(); |
|
| 754 | - |
|
| 755 | - if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) |
|
| 756 | - { |
|
| 757 | - return $this->getHost(); |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - return $this->getHost().':'.$port; |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - /** |
|
| 764 | - * Gets the scheme and HTTP host. |
|
| 765 | - * |
|
| 766 | - * @return string |
|
| 767 | - */ |
|
| 768 | - public function getSchemeWithHttpHost() |
|
| 769 | - { |
|
| 770 | - return $this->getScheme().'://'.$this->getHttpHost(); |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Get the root URL for the application. |
|
| 775 | - * |
|
| 776 | - * @return string |
|
| 777 | - */ |
|
| 778 | - public function root() |
|
| 779 | - { |
|
| 780 | - return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - /** |
|
| 784 | - * Get the URL for the request. |
|
| 785 | - * |
|
| 786 | - * @return string |
|
| 787 | - */ |
|
| 788 | - public function url() |
|
| 789 | - { |
|
| 790 | - return trim(preg_replace('/\?.*/', '', $this->get()), '/'); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - /** |
|
| 794 | - * Returns the referer. |
|
| 795 | - * |
|
| 796 | - * @param string $default |
|
| 797 | - * |
|
| 798 | - * @return string |
|
| 799 | - */ |
|
| 800 | - public function referer(string $default = '') |
|
| 801 | - { |
|
| 802 | - return $this->server->get('HTTP_REFERER', $default); |
|
| 803 | - } |
|
| 742 | + return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + /** |
|
| 746 | + * Returns the HTTP host being requested. |
|
| 747 | + * |
|
| 748 | + * @return string |
|
| 749 | + */ |
|
| 750 | + public function getHttpHost() |
|
| 751 | + { |
|
| 752 | + $scheme = $this->getScheme(); |
|
| 753 | + $port = $this->getPort(); |
|
| 754 | + |
|
| 755 | + if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) |
|
| 756 | + { |
|
| 757 | + return $this->getHost(); |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + return $this->getHost().':'.$port; |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + /** |
|
| 764 | + * Gets the scheme and HTTP host. |
|
| 765 | + * |
|
| 766 | + * @return string |
|
| 767 | + */ |
|
| 768 | + public function getSchemeWithHttpHost() |
|
| 769 | + { |
|
| 770 | + return $this->getScheme().'://'.$this->getHttpHost(); |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Get the root URL for the application. |
|
| 775 | + * |
|
| 776 | + * @return string |
|
| 777 | + */ |
|
| 778 | + public function root() |
|
| 779 | + { |
|
| 780 | + return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + /** |
|
| 784 | + * Get the URL for the request. |
|
| 785 | + * |
|
| 786 | + * @return string |
|
| 787 | + */ |
|
| 788 | + public function url() |
|
| 789 | + { |
|
| 790 | + return trim(preg_replace('/\?.*/', '', $this->get()), '/'); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + /** |
|
| 794 | + * Returns the referer. |
|
| 795 | + * |
|
| 796 | + * @param string $default |
|
| 797 | + * |
|
| 798 | + * @return string |
|
| 799 | + */ |
|
| 800 | + public function referer(string $default = '') |
|
| 801 | + { |
|
| 802 | + return $this->server->get('HTTP_REFERER', $default); |
|
| 803 | + } |
|
| 804 | 804 | |
| 805 | - /** |
|
| 806 | - * Attempts to detect if the current connection is secure through |
|
| 807 | - * over HTTPS protocol. |
|
| 808 | - * |
|
| 809 | - * @return bool |
|
| 810 | - */ |
|
| 811 | - public function secure() |
|
| 812 | - { |
|
| 813 | - if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
| 814 | - return true; |
|
| 815 | - } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
| 816 | - return true; |
|
| 817 | - } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
| 818 | - return true; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - return false; |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * Returns the user agent. |
|
| 826 | - * |
|
| 827 | - * @param string|null $default |
|
| 828 | - * |
|
| 829 | - * @return string |
|
| 830 | - */ |
|
| 831 | - public function userAgent(string $default = null) |
|
| 832 | - { |
|
| 833 | - return $this->server->get('HTTP_USER_AGENT', $default); |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - /** |
|
| 837 | - * Get the route resolver callback. |
|
| 838 | - * |
|
| 839 | - * @return \Closure |
|
| 840 | - */ |
|
| 841 | - public function getRouteResolver() |
|
| 842 | - { |
|
| 843 | - return $this->routeResolver ?: function () { |
|
| 844 | - // |
|
| 845 | - }; |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - /** |
|
| 849 | - * Set the route resolver callback. |
|
| 850 | - * |
|
| 851 | - * @param \Closure $callback |
|
| 852 | - * |
|
| 853 | - * @return $this |
|
| 854 | - */ |
|
| 855 | - public function setRouteResolver(Closure $callback) |
|
| 856 | - { |
|
| 857 | - $this->routeResolver = $callback; |
|
| 858 | - |
|
| 859 | - return $this; |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - /** |
|
| 863 | - * Get an element from the request. |
|
| 864 | - * |
|
| 865 | - * @return string[] |
|
| 866 | - */ |
|
| 867 | - public function __get($key) |
|
| 868 | - { |
|
| 869 | - $all = $this->server->all(); |
|
| 870 | - |
|
| 871 | - if (array_key_exists($key, $all)) { |
|
| 872 | - return $all[$key]; |
|
| 873 | - } else { |
|
| 874 | - return $key; |
|
| 875 | - } |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - /** |
|
| 879 | - * Returns the Request as an HTTP string. |
|
| 880 | - * |
|
| 881 | - * @return string |
|
| 882 | - */ |
|
| 883 | - public function __toString() |
|
| 884 | - { |
|
| 885 | - try |
|
| 886 | - { |
|
| 887 | - $content = $this->getContent(); |
|
| 888 | - } |
|
| 889 | - catch (LogicException $e) |
|
| 890 | - { |
|
| 891 | - if (PHP_VERSION_ID > 70400) |
|
| 892 | - { |
|
| 893 | - throw $e; |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - return trigger_error($e, E_USER_ERROR); |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - $cookieHeader = ''; |
|
| 900 | - $cookies = []; |
|
| 901 | - |
|
| 902 | - foreach ($this->cookies as $key => $value) |
|
| 903 | - { |
|
| 904 | - $cookies[]= "{$key} = {$value}"; |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - if ( ! empty($cookies)) |
|
| 908 | - { |
|
| 909 | - $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
| 910 | - } |
|
| 805 | + /** |
|
| 806 | + * Attempts to detect if the current connection is secure through |
|
| 807 | + * over HTTPS protocol. |
|
| 808 | + * |
|
| 809 | + * @return bool |
|
| 810 | + */ |
|
| 811 | + public function secure() |
|
| 812 | + { |
|
| 813 | + if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
| 814 | + return true; |
|
| 815 | + } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
| 816 | + return true; |
|
| 817 | + } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
| 818 | + return true; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + return false; |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Returns the user agent. |
|
| 826 | + * |
|
| 827 | + * @param string|null $default |
|
| 828 | + * |
|
| 829 | + * @return string |
|
| 830 | + */ |
|
| 831 | + public function userAgent(string $default = null) |
|
| 832 | + { |
|
| 833 | + return $this->server->get('HTTP_USER_AGENT', $default); |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + /** |
|
| 837 | + * Get the route resolver callback. |
|
| 838 | + * |
|
| 839 | + * @return \Closure |
|
| 840 | + */ |
|
| 841 | + public function getRouteResolver() |
|
| 842 | + { |
|
| 843 | + return $this->routeResolver ?: function () { |
|
| 844 | + // |
|
| 845 | + }; |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + /** |
|
| 849 | + * Set the route resolver callback. |
|
| 850 | + * |
|
| 851 | + * @param \Closure $callback |
|
| 852 | + * |
|
| 853 | + * @return $this |
|
| 854 | + */ |
|
| 855 | + public function setRouteResolver(Closure $callback) |
|
| 856 | + { |
|
| 857 | + $this->routeResolver = $callback; |
|
| 858 | + |
|
| 859 | + return $this; |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + /** |
|
| 863 | + * Get an element from the request. |
|
| 864 | + * |
|
| 865 | + * @return string[] |
|
| 866 | + */ |
|
| 867 | + public function __get($key) |
|
| 868 | + { |
|
| 869 | + $all = $this->server->all(); |
|
| 870 | + |
|
| 871 | + if (array_key_exists($key, $all)) { |
|
| 872 | + return $all[$key]; |
|
| 873 | + } else { |
|
| 874 | + return $key; |
|
| 875 | + } |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + /** |
|
| 879 | + * Returns the Request as an HTTP string. |
|
| 880 | + * |
|
| 881 | + * @return string |
|
| 882 | + */ |
|
| 883 | + public function __toString() |
|
| 884 | + { |
|
| 885 | + try |
|
| 886 | + { |
|
| 887 | + $content = $this->getContent(); |
|
| 888 | + } |
|
| 889 | + catch (LogicException $e) |
|
| 890 | + { |
|
| 891 | + if (PHP_VERSION_ID > 70400) |
|
| 892 | + { |
|
| 893 | + throw $e; |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + return trigger_error($e, E_USER_ERROR); |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + $cookieHeader = ''; |
|
| 900 | + $cookies = []; |
|
| 901 | + |
|
| 902 | + foreach ($this->cookies as $key => $value) |
|
| 903 | + { |
|
| 904 | + $cookies[]= "{$key} = {$value}"; |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + if ( ! empty($cookies)) |
|
| 908 | + { |
|
| 909 | + $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
| 910 | + } |
|
| 911 | 911 | |
| 912 | - return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
| 913 | - $this->headers. |
|
| 914 | - $cookieHeader."\r\n". |
|
| 915 | - $content; |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * Clones the current request. |
|
| 920 | - * |
|
| 921 | - * @return void |
|
| 922 | - */ |
|
| 923 | - public function __clone() |
|
| 924 | - { |
|
| 925 | - $this->request = clone $this->request; |
|
| 926 | - $this->cookies = clone $this->cookies; |
|
| 927 | - $this->files = clone $this->files; |
|
| 928 | - $this->server = clone $this->server; |
|
| 929 | - $this->headers = clone $this->headers; |
|
| 930 | - } |
|
| 912 | + return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
| 913 | + $this->headers. |
|
| 914 | + $cookieHeader."\r\n". |
|
| 915 | + $content; |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * Clones the current request. |
|
| 920 | + * |
|
| 921 | + * @return void |
|
| 922 | + */ |
|
| 923 | + public function __clone() |
|
| 924 | + { |
|
| 925 | + $this->request = clone $this->request; |
|
| 926 | + $this->cookies = clone $this->cookies; |
|
| 927 | + $this->files = clone $this->files; |
|
| 928 | + $this->server = clone $this->server; |
|
| 929 | + $this->headers = clone $this->headers; |
|
| 930 | + } |
|
| 931 | 931 | } |
| 932 | 932 | \ No newline at end of file |
@@ -840,7 +840,7 @@ discard block |
||
| 840 | 840 | */ |
| 841 | 841 | public function getRouteResolver() |
| 842 | 842 | { |
| 843 | - return $this->routeResolver ?: function () { |
|
| 843 | + return $this->routeResolver ?: function() { |
|
| 844 | 844 | // |
| 845 | 845 | }; |
| 846 | 846 | } |
@@ -901,7 +901,7 @@ discard block |
||
| 901 | 901 | |
| 902 | 902 | foreach ($this->cookies as $key => $value) |
| 903 | 903 | { |
| 904 | - $cookies[]= "{$key} = {$value}"; |
|
| 904 | + $cookies[] = "{$key} = {$value}"; |
|
| 905 | 905 | } |
| 906 | 906 | |
| 907 | 907 | if ( ! empty($cookies)) |