@@ -31,409 +31,409 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class Kernel implements KernelContract { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * The application implementation. |
|
| 36 | - * |
|
| 37 | - * @var \Illuminate\Contracts\Foundation\Application |
|
| 38 | - */ |
|
| 39 | - protected $app; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The router instance. |
|
| 43 | - * |
|
| 44 | - * @var \Illuminate\Routing\Router |
|
| 45 | - */ |
|
| 46 | - protected $router; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The bootstrap classes for the application. |
|
| 50 | - * |
|
| 51 | - * @var array |
|
| 52 | - */ |
|
| 53 | - protected $bootstrappers = array(); |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The application's middleware stack. |
|
| 57 | - * |
|
| 58 | - * @var array |
|
| 59 | - */ |
|
| 60 | - protected $middleware = array(); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The application's route middleware groups. |
|
| 64 | - * |
|
| 65 | - * @var array |
|
| 66 | - */ |
|
| 67 | - protected $middleware_groups = array(); |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The application's route middleware. |
|
| 71 | - * |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - protected $route_middleware = array(); |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * The priority-sorted list of middleware. |
|
| 78 | - * |
|
| 79 | - * Forces non-global middleware to always be in the given order. |
|
| 80 | - * |
|
| 81 | - * @var array |
|
| 82 | - */ |
|
| 83 | - protected $middleware_priority = array(); |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Create a new HTTP kernel instance. |
|
| 87 | - * |
|
| 88 | - * @param \Illuminate\Contracts\Container\Container $app The app. |
|
| 89 | - * @param \Illuminate\Routing\Router $router The app router. |
|
| 90 | - * |
|
| 91 | - * @return void |
|
| 92 | - */ |
|
| 93 | - public function __construct( Container $app, Router $router ) { |
|
| 94 | - $this->app = $app; |
|
| 95 | - $this->router = $router; |
|
| 96 | - |
|
| 97 | - $this->sync_middleware_to_router(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Handle an incoming HTTP request. |
|
| 102 | - * |
|
| 103 | - * @param \Illuminate\Http\Request $request The app http request. |
|
| 104 | - * |
|
| 105 | - * @throws \Exception The throwable exception. |
|
| 106 | - * |
|
| 107 | - * @return \Illuminate\Http\Response |
|
| 108 | - */ |
|
| 109 | - public function handle( $request ) { |
|
| 110 | - try { |
|
| 111 | - $request->enableHttpMethodParameterOverride(); |
|
| 112 | - $response = $this->send_request_through_router( $request ); |
|
| 113 | - } catch ( Throwable $e ) { |
|
| 114 | - throw new \Exception( $e, 1 ); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->app['events']->dispatch( |
|
| 118 | - new RequestHandled( $request, $response ) |
|
| 119 | - ); |
|
| 120 | - |
|
| 121 | - return $response; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Send the given request through the middleware / router. |
|
| 126 | - * |
|
| 127 | - * @param \Illuminate\Http\Request $request The app http request. |
|
| 128 | - * |
|
| 129 | - * @return \Illuminate\Http\Response |
|
| 130 | - */ |
|
| 131 | - protected function send_request_through_router( $request ) { |
|
| 132 | - $this->app->instance( 'request', $request ); |
|
| 133 | - |
|
| 134 | - Facade::clearResolvedInstance( 'request' ); |
|
| 135 | - |
|
| 136 | - $this->bootstrap(); |
|
| 137 | - |
|
| 138 | - return ( new Pipeline( $this->app ) ) |
|
| 139 | - ->send( $request ) |
|
| 140 | - ->through( $this->middleware ) |
|
| 141 | - ->then( $this->dispatch_to_router() ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Bootstrap the application for HTTP requests. |
|
| 146 | - * |
|
| 147 | - * @return void |
|
| 148 | - */ |
|
| 149 | - public function bootstrap() {} |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Get the route dispatcher callback. |
|
| 153 | - * |
|
| 154 | - * @return \Closure |
|
| 155 | - */ |
|
| 156 | - protected function dispatch_to_router() { |
|
| 157 | - return function ( $request ) { |
|
| 158 | - $this->app->instance( 'request', $request ); |
|
| 159 | - |
|
| 160 | - return $this->router->dispatch( $request ); |
|
| 161 | - }; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Call the terminate method on any terminable middleware. |
|
| 166 | - * |
|
| 167 | - * @param \Illuminate\Http\Request $request The app http request. |
|
| 168 | - * @param \Illuminate\Http\Response $response The app http response. |
|
| 169 | - * |
|
| 170 | - * @return void |
|
| 171 | - */ |
|
| 172 | - public function terminate( $request, $response ) { |
|
| 173 | - $this->terminate_middleware( $request, $response ); |
|
| 174 | - |
|
| 175 | - $this->app->terminate(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Call the terminate method on any terminable middleware. |
|
| 180 | - * |
|
| 181 | - * @param \Illuminate\Http\Request $request The app http request. |
|
| 182 | - * @param \Illuminate\Http\Response $response The app http response. |
|
| 183 | - * |
|
| 184 | - * @return void |
|
| 185 | - */ |
|
| 186 | - protected function terminate_middleware( $request, $response ) { |
|
| 187 | - $middlewares = $this->app->shouldSkipMiddleware() ? array() : array_merge( |
|
| 188 | - $this->gather_route_middleware( $request ), |
|
| 189 | - $this->middleware |
|
| 190 | - ); |
|
| 191 | - |
|
| 192 | - foreach ( $middlewares as $middleware ) { |
|
| 193 | - if ( ! is_string( $middleware ) ) { |
|
| 194 | - continue; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - list( $name ) = $this->parse_middleware( $middleware ); |
|
| 198 | - |
|
| 199 | - $instance = $this->app->make( $name ); |
|
| 200 | - |
|
| 201 | - if ( method_exists( $instance, 'terminate' ) ) { |
|
| 202 | - $instance->terminate( $request, $response ); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Gather the route middleware for the given request. |
|
| 209 | - * |
|
| 210 | - * @param \Illuminate\Http\Request $request The app http request. |
|
| 211 | - * |
|
| 212 | - * @return array |
|
| 213 | - */ |
|
| 214 | - protected function gather_route_middleware( $request ) { |
|
| 215 | - $route = $request->route(); |
|
| 216 | - if ( $route ) { |
|
| 217 | - return $this->router->gatherRouteMiddleware( $route ); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return array(); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Parse a middleware string to get the name and parameters. |
|
| 225 | - * |
|
| 226 | - * @param string $middleware The app middleware. |
|
| 227 | - * |
|
| 228 | - * @return array |
|
| 229 | - */ |
|
| 230 | - protected function parse_middleware( $middleware ) { |
|
| 231 | - |
|
| 232 | - list( $name, $parameters ) = array_pad( explode( ':', $middleware, 2 ), 2, array() ); |
|
| 233 | - |
|
| 234 | - if ( is_string( $parameters ) ) { |
|
| 235 | - $parameters = explode( ',', $parameters ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return array( $name, $parameters ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Determine if the kernel has a given middleware. |
|
| 243 | - * |
|
| 244 | - * @param string $middleware The app middleware. |
|
| 245 | - * |
|
| 246 | - * @return bool |
|
| 247 | - */ |
|
| 248 | - public function has_middleware( $middleware ) { |
|
| 249 | - return in_array( $middleware, $this->middleware ); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Add a new middleware to beginning of the stack if it does not already exist. |
|
| 254 | - * |
|
| 255 | - * @param string $middleware The app middleware. |
|
| 256 | - * |
|
| 257 | - * @return $this |
|
| 258 | - */ |
|
| 259 | - public function prepend_middleware( $middleware ) { |
|
| 260 | - if ( array_search( $middleware, $this->middleware ) === false ) { |
|
| 261 | - array_unshift( $this->middleware, $middleware ); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return $this; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Add a new middleware to end of the stack if it does not already exist. |
|
| 269 | - * |
|
| 270 | - * @param string $middleware The app middleware. |
|
| 271 | - * |
|
| 272 | - * @return $this |
|
| 273 | - */ |
|
| 274 | - public function push_middleware( $middleware ) { |
|
| 275 | - if ( array_search( $middleware, $this->middleware ) === false ) { |
|
| 276 | - $this->middleware[] = $middleware; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - return $this; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Prepend the given middleware to the given middleware group. |
|
| 284 | - * |
|
| 285 | - * @param string $group The app group. |
|
| 286 | - * @param string $middleware The app middleware. |
|
| 287 | - * |
|
| 288 | - * @throws \InvalidArgumentException The invalid argument exception. |
|
| 289 | - * |
|
| 290 | - * @return $this |
|
| 291 | - */ |
|
| 292 | - public function prepend_middleware_to_group( $group, $middleware ) { |
|
| 293 | - if ( ! isset( $this->middleware_groups[ $group ] ) ) { |
|
| 294 | - throw new InvalidArgumentException( "The [{$group}] middleware group has not been defined." ); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - if ( array_search( $middleware, $this->middleware_groups[ $group ] ) === false ) { |
|
| 298 | - array_unshift( $this->middleware_groups[ $group ], $middleware ); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - $this->sync_middleware_to_router(); |
|
| 302 | - |
|
| 303 | - return $this; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Append the given middleware to the given middleware group. |
|
| 308 | - * |
|
| 309 | - * @param string $group The app group. |
|
| 310 | - * @param string $middleware The app middleware. |
|
| 311 | - * |
|
| 312 | - * @throws \InvalidArgumentException The invalid argument exception. |
|
| 313 | - * |
|
| 314 | - * @return $this |
|
| 315 | - */ |
|
| 316 | - public function append_middleware_to_group( $group, $middleware ) { |
|
| 317 | - if ( ! isset( $this->middleware_groups[ $group ] ) ) { |
|
| 318 | - throw new InvalidArgumentException( "The [{$group}] middleware group has not been defined." ); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - if ( array_search( $middleware, $this->middleware_groups[ $group ] ) === false ) { |
|
| 322 | - $this->middleware_groups[ $group ][] = $middleware; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - $this->sync_middleware_to_router(); |
|
| 326 | - |
|
| 327 | - return $this; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Prepend the given middleware to the middleware priority list. |
|
| 332 | - * |
|
| 333 | - * @param string $middleware The app middleware. |
|
| 334 | - * |
|
| 335 | - * @return $this |
|
| 336 | - */ |
|
| 337 | - public function prepend_to_middleware_priority( $middleware ) { |
|
| 338 | - if ( ! in_array( $middleware, $this->middleware_priority ) ) { |
|
| 339 | - array_unshift( $this->middleware_priority, $middleware ); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $this->sync_middleware_to_router(); |
|
| 343 | - |
|
| 344 | - return $this; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Append the given middleware to the middleware priority list. |
|
| 349 | - * |
|
| 350 | - * @param string $middleware The app middleware. |
|
| 351 | - * |
|
| 352 | - * @return $this |
|
| 353 | - */ |
|
| 354 | - public function append_to_middleware_priority( $middleware ) { |
|
| 355 | - if ( ! in_array( $middleware, $this->middleware_priority ) ) { |
|
| 356 | - $this->middleware_priority[] = $middleware; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $this->sync_middleware_to_router(); |
|
| 360 | - |
|
| 361 | - return $this; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Sync the current state of the middleware to the router. |
|
| 366 | - * |
|
| 367 | - * @return void |
|
| 368 | - */ |
|
| 369 | - protected function sync_middleware_to_router() { |
|
| 370 | - $this->router->middlewarePriority = $this->middleware_priority; |
|
| 371 | - |
|
| 372 | - foreach ( $this->middleware_groups as $key => $middleware ) { |
|
| 373 | - $this->router->middlewareGroup( $key, $middleware ); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - foreach ( $this->route_middleware as $key => $middleware ) { |
|
| 377 | - $this->router->aliasMiddleware( $key, $middleware ); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Get the bootstrap classes for the application. |
|
| 383 | - * |
|
| 384 | - * @return array |
|
| 385 | - */ |
|
| 386 | - protected function bootstrappers() { |
|
| 387 | - return $this->bootstrappers; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Report the exception to the exception handler. |
|
| 392 | - * |
|
| 393 | - * @param \Throwable $e The throwable exception. |
|
| 394 | - * |
|
| 395 | - * @return void |
|
| 396 | - */ |
|
| 397 | - protected function report_exception( Throwable $e ) { |
|
| 398 | - $this->app[ ExceptionHandler::class ]->report( $e ); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Render the exception to a response. |
|
| 403 | - * |
|
| 404 | - * @param \Illuminate\Http\Request $request The app http request. |
|
| 405 | - * @param \Throwable $e The throwable exception. |
|
| 406 | - * |
|
| 407 | - * @return \Symfony\Component\HttpFoundation\Response |
|
| 408 | - */ |
|
| 409 | - protected function render_exception( $request, Throwable $e ) { |
|
| 410 | - return $this->app[ ExceptionHandler::class ]->render( $request, $e ); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Get the application's route middleware groups. |
|
| 415 | - * |
|
| 416 | - * @return array |
|
| 417 | - */ |
|
| 418 | - public function get_middleware_groups() { |
|
| 419 | - return $this->middleware_groups; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Get the application's route middleware. |
|
| 424 | - * |
|
| 425 | - * @return array |
|
| 426 | - */ |
|
| 427 | - public function get_route_middleware() { |
|
| 428 | - return $this->route_middleware; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Get the Laravel application instance. |
|
| 433 | - * |
|
| 434 | - * @return \Illuminate\Contracts\Foundation\Application |
|
| 435 | - */ |
|
| 436 | - public function get_application() { |
|
| 437 | - return $this->app; |
|
| 438 | - } |
|
| 34 | + /** |
|
| 35 | + * The application implementation. |
|
| 36 | + * |
|
| 37 | + * @var \Illuminate\Contracts\Foundation\Application |
|
| 38 | + */ |
|
| 39 | + protected $app; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The router instance. |
|
| 43 | + * |
|
| 44 | + * @var \Illuminate\Routing\Router |
|
| 45 | + */ |
|
| 46 | + protected $router; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The bootstrap classes for the application. |
|
| 50 | + * |
|
| 51 | + * @var array |
|
| 52 | + */ |
|
| 53 | + protected $bootstrappers = array(); |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The application's middleware stack. |
|
| 57 | + * |
|
| 58 | + * @var array |
|
| 59 | + */ |
|
| 60 | + protected $middleware = array(); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The application's route middleware groups. |
|
| 64 | + * |
|
| 65 | + * @var array |
|
| 66 | + */ |
|
| 67 | + protected $middleware_groups = array(); |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The application's route middleware. |
|
| 71 | + * |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + protected $route_middleware = array(); |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * The priority-sorted list of middleware. |
|
| 78 | + * |
|
| 79 | + * Forces non-global middleware to always be in the given order. |
|
| 80 | + * |
|
| 81 | + * @var array |
|
| 82 | + */ |
|
| 83 | + protected $middleware_priority = array(); |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Create a new HTTP kernel instance. |
|
| 87 | + * |
|
| 88 | + * @param \Illuminate\Contracts\Container\Container $app The app. |
|
| 89 | + * @param \Illuminate\Routing\Router $router The app router. |
|
| 90 | + * |
|
| 91 | + * @return void |
|
| 92 | + */ |
|
| 93 | + public function __construct( Container $app, Router $router ) { |
|
| 94 | + $this->app = $app; |
|
| 95 | + $this->router = $router; |
|
| 96 | + |
|
| 97 | + $this->sync_middleware_to_router(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Handle an incoming HTTP request. |
|
| 102 | + * |
|
| 103 | + * @param \Illuminate\Http\Request $request The app http request. |
|
| 104 | + * |
|
| 105 | + * @throws \Exception The throwable exception. |
|
| 106 | + * |
|
| 107 | + * @return \Illuminate\Http\Response |
|
| 108 | + */ |
|
| 109 | + public function handle( $request ) { |
|
| 110 | + try { |
|
| 111 | + $request->enableHttpMethodParameterOverride(); |
|
| 112 | + $response = $this->send_request_through_router( $request ); |
|
| 113 | + } catch ( Throwable $e ) { |
|
| 114 | + throw new \Exception( $e, 1 ); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->app['events']->dispatch( |
|
| 118 | + new RequestHandled( $request, $response ) |
|
| 119 | + ); |
|
| 120 | + |
|
| 121 | + return $response; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Send the given request through the middleware / router. |
|
| 126 | + * |
|
| 127 | + * @param \Illuminate\Http\Request $request The app http request. |
|
| 128 | + * |
|
| 129 | + * @return \Illuminate\Http\Response |
|
| 130 | + */ |
|
| 131 | + protected function send_request_through_router( $request ) { |
|
| 132 | + $this->app->instance( 'request', $request ); |
|
| 133 | + |
|
| 134 | + Facade::clearResolvedInstance( 'request' ); |
|
| 135 | + |
|
| 136 | + $this->bootstrap(); |
|
| 137 | + |
|
| 138 | + return ( new Pipeline( $this->app ) ) |
|
| 139 | + ->send( $request ) |
|
| 140 | + ->through( $this->middleware ) |
|
| 141 | + ->then( $this->dispatch_to_router() ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Bootstrap the application for HTTP requests. |
|
| 146 | + * |
|
| 147 | + * @return void |
|
| 148 | + */ |
|
| 149 | + public function bootstrap() {} |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Get the route dispatcher callback. |
|
| 153 | + * |
|
| 154 | + * @return \Closure |
|
| 155 | + */ |
|
| 156 | + protected function dispatch_to_router() { |
|
| 157 | + return function ( $request ) { |
|
| 158 | + $this->app->instance( 'request', $request ); |
|
| 159 | + |
|
| 160 | + return $this->router->dispatch( $request ); |
|
| 161 | + }; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Call the terminate method on any terminable middleware. |
|
| 166 | + * |
|
| 167 | + * @param \Illuminate\Http\Request $request The app http request. |
|
| 168 | + * @param \Illuminate\Http\Response $response The app http response. |
|
| 169 | + * |
|
| 170 | + * @return void |
|
| 171 | + */ |
|
| 172 | + public function terminate( $request, $response ) { |
|
| 173 | + $this->terminate_middleware( $request, $response ); |
|
| 174 | + |
|
| 175 | + $this->app->terminate(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Call the terminate method on any terminable middleware. |
|
| 180 | + * |
|
| 181 | + * @param \Illuminate\Http\Request $request The app http request. |
|
| 182 | + * @param \Illuminate\Http\Response $response The app http response. |
|
| 183 | + * |
|
| 184 | + * @return void |
|
| 185 | + */ |
|
| 186 | + protected function terminate_middleware( $request, $response ) { |
|
| 187 | + $middlewares = $this->app->shouldSkipMiddleware() ? array() : array_merge( |
|
| 188 | + $this->gather_route_middleware( $request ), |
|
| 189 | + $this->middleware |
|
| 190 | + ); |
|
| 191 | + |
|
| 192 | + foreach ( $middlewares as $middleware ) { |
|
| 193 | + if ( ! is_string( $middleware ) ) { |
|
| 194 | + continue; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + list( $name ) = $this->parse_middleware( $middleware ); |
|
| 198 | + |
|
| 199 | + $instance = $this->app->make( $name ); |
|
| 200 | + |
|
| 201 | + if ( method_exists( $instance, 'terminate' ) ) { |
|
| 202 | + $instance->terminate( $request, $response ); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Gather the route middleware for the given request. |
|
| 209 | + * |
|
| 210 | + * @param \Illuminate\Http\Request $request The app http request. |
|
| 211 | + * |
|
| 212 | + * @return array |
|
| 213 | + */ |
|
| 214 | + protected function gather_route_middleware( $request ) { |
|
| 215 | + $route = $request->route(); |
|
| 216 | + if ( $route ) { |
|
| 217 | + return $this->router->gatherRouteMiddleware( $route ); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return array(); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Parse a middleware string to get the name and parameters. |
|
| 225 | + * |
|
| 226 | + * @param string $middleware The app middleware. |
|
| 227 | + * |
|
| 228 | + * @return array |
|
| 229 | + */ |
|
| 230 | + protected function parse_middleware( $middleware ) { |
|
| 231 | + |
|
| 232 | + list( $name, $parameters ) = array_pad( explode( ':', $middleware, 2 ), 2, array() ); |
|
| 233 | + |
|
| 234 | + if ( is_string( $parameters ) ) { |
|
| 235 | + $parameters = explode( ',', $parameters ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return array( $name, $parameters ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Determine if the kernel has a given middleware. |
|
| 243 | + * |
|
| 244 | + * @param string $middleware The app middleware. |
|
| 245 | + * |
|
| 246 | + * @return bool |
|
| 247 | + */ |
|
| 248 | + public function has_middleware( $middleware ) { |
|
| 249 | + return in_array( $middleware, $this->middleware ); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Add a new middleware to beginning of the stack if it does not already exist. |
|
| 254 | + * |
|
| 255 | + * @param string $middleware The app middleware. |
|
| 256 | + * |
|
| 257 | + * @return $this |
|
| 258 | + */ |
|
| 259 | + public function prepend_middleware( $middleware ) { |
|
| 260 | + if ( array_search( $middleware, $this->middleware ) === false ) { |
|
| 261 | + array_unshift( $this->middleware, $middleware ); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return $this; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Add a new middleware to end of the stack if it does not already exist. |
|
| 269 | + * |
|
| 270 | + * @param string $middleware The app middleware. |
|
| 271 | + * |
|
| 272 | + * @return $this |
|
| 273 | + */ |
|
| 274 | + public function push_middleware( $middleware ) { |
|
| 275 | + if ( array_search( $middleware, $this->middleware ) === false ) { |
|
| 276 | + $this->middleware[] = $middleware; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + return $this; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Prepend the given middleware to the given middleware group. |
|
| 284 | + * |
|
| 285 | + * @param string $group The app group. |
|
| 286 | + * @param string $middleware The app middleware. |
|
| 287 | + * |
|
| 288 | + * @throws \InvalidArgumentException The invalid argument exception. |
|
| 289 | + * |
|
| 290 | + * @return $this |
|
| 291 | + */ |
|
| 292 | + public function prepend_middleware_to_group( $group, $middleware ) { |
|
| 293 | + if ( ! isset( $this->middleware_groups[ $group ] ) ) { |
|
| 294 | + throw new InvalidArgumentException( "The [{$group}] middleware group has not been defined." ); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + if ( array_search( $middleware, $this->middleware_groups[ $group ] ) === false ) { |
|
| 298 | + array_unshift( $this->middleware_groups[ $group ], $middleware ); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + $this->sync_middleware_to_router(); |
|
| 302 | + |
|
| 303 | + return $this; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Append the given middleware to the given middleware group. |
|
| 308 | + * |
|
| 309 | + * @param string $group The app group. |
|
| 310 | + * @param string $middleware The app middleware. |
|
| 311 | + * |
|
| 312 | + * @throws \InvalidArgumentException The invalid argument exception. |
|
| 313 | + * |
|
| 314 | + * @return $this |
|
| 315 | + */ |
|
| 316 | + public function append_middleware_to_group( $group, $middleware ) { |
|
| 317 | + if ( ! isset( $this->middleware_groups[ $group ] ) ) { |
|
| 318 | + throw new InvalidArgumentException( "The [{$group}] middleware group has not been defined." ); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + if ( array_search( $middleware, $this->middleware_groups[ $group ] ) === false ) { |
|
| 322 | + $this->middleware_groups[ $group ][] = $middleware; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + $this->sync_middleware_to_router(); |
|
| 326 | + |
|
| 327 | + return $this; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Prepend the given middleware to the middleware priority list. |
|
| 332 | + * |
|
| 333 | + * @param string $middleware The app middleware. |
|
| 334 | + * |
|
| 335 | + * @return $this |
|
| 336 | + */ |
|
| 337 | + public function prepend_to_middleware_priority( $middleware ) { |
|
| 338 | + if ( ! in_array( $middleware, $this->middleware_priority ) ) { |
|
| 339 | + array_unshift( $this->middleware_priority, $middleware ); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $this->sync_middleware_to_router(); |
|
| 343 | + |
|
| 344 | + return $this; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Append the given middleware to the middleware priority list. |
|
| 349 | + * |
|
| 350 | + * @param string $middleware The app middleware. |
|
| 351 | + * |
|
| 352 | + * @return $this |
|
| 353 | + */ |
|
| 354 | + public function append_to_middleware_priority( $middleware ) { |
|
| 355 | + if ( ! in_array( $middleware, $this->middleware_priority ) ) { |
|
| 356 | + $this->middleware_priority[] = $middleware; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $this->sync_middleware_to_router(); |
|
| 360 | + |
|
| 361 | + return $this; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Sync the current state of the middleware to the router. |
|
| 366 | + * |
|
| 367 | + * @return void |
|
| 368 | + */ |
|
| 369 | + protected function sync_middleware_to_router() { |
|
| 370 | + $this->router->middlewarePriority = $this->middleware_priority; |
|
| 371 | + |
|
| 372 | + foreach ( $this->middleware_groups as $key => $middleware ) { |
|
| 373 | + $this->router->middlewareGroup( $key, $middleware ); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + foreach ( $this->route_middleware as $key => $middleware ) { |
|
| 377 | + $this->router->aliasMiddleware( $key, $middleware ); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Get the bootstrap classes for the application. |
|
| 383 | + * |
|
| 384 | + * @return array |
|
| 385 | + */ |
|
| 386 | + protected function bootstrappers() { |
|
| 387 | + return $this->bootstrappers; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Report the exception to the exception handler. |
|
| 392 | + * |
|
| 393 | + * @param \Throwable $e The throwable exception. |
|
| 394 | + * |
|
| 395 | + * @return void |
|
| 396 | + */ |
|
| 397 | + protected function report_exception( Throwable $e ) { |
|
| 398 | + $this->app[ ExceptionHandler::class ]->report( $e ); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Render the exception to a response. |
|
| 403 | + * |
|
| 404 | + * @param \Illuminate\Http\Request $request The app http request. |
|
| 405 | + * @param \Throwable $e The throwable exception. |
|
| 406 | + * |
|
| 407 | + * @return \Symfony\Component\HttpFoundation\Response |
|
| 408 | + */ |
|
| 409 | + protected function render_exception( $request, Throwable $e ) { |
|
| 410 | + return $this->app[ ExceptionHandler::class ]->render( $request, $e ); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Get the application's route middleware groups. |
|
| 415 | + * |
|
| 416 | + * @return array |
|
| 417 | + */ |
|
| 418 | + public function get_middleware_groups() { |
|
| 419 | + return $this->middleware_groups; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Get the application's route middleware. |
|
| 424 | + * |
|
| 425 | + * @return array |
|
| 426 | + */ |
|
| 427 | + public function get_route_middleware() { |
|
| 428 | + return $this->route_middleware; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Get the Laravel application instance. |
|
| 433 | + * |
|
| 434 | + * @return \Illuminate\Contracts\Foundation\Application |
|
| 435 | + */ |
|
| 436 | + public function get_application() { |
|
| 437 | + return $this->app; |
|
| 438 | + } |
|
| 439 | 439 | } |