Skip to content

Commit

Permalink
Kernel cleaning
Browse files Browse the repository at this point in the history
General cleaning and formatting of the Kernel class, moved
kernel.handled event into an object based event.
  • Loading branch information
taylorotwell committed Dec 19, 2016
1 parent 96ba7f8 commit 43a5e5f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 40 deletions.
33 changes: 33 additions & 0 deletions src/Illuminate/Foundation/Http/Events/RequestHandled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Foundation\Http\Events;

class RequestHandled
{
/**
* The request instance.
*
* @var \Illuminate\Http\Request
*/
public $request;

/**
* The response instance.
*
* @var \Illuminate\Http\Response
*/
public $response;

/**
* Create a new event instance.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Response $response
* @return void
*/
public function __construct($request, $response)
{
$this->request = $request;
$this->response = $response;
}
}
92 changes: 52 additions & 40 deletions src/Illuminate/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function handle($request)
$response = $this->renderException($request, $e);
}

$this->app['events']->fire('kernel.handled', [$request, $response]);
event(new Events\RequestHandled($request, $response));

return $response;
}
Expand All @@ -149,6 +149,32 @@ protected function sendRequestThroughRouter($request)
->then($this->dispatchToRouter());
}

/**
* Bootstrap the application for HTTP requests.
*
* @return void
*/
public function bootstrap()
{
if (! $this->app->hasBeenBootstrapped()) {
$this->app->bootstrapWith($this->bootstrappers());
}
}

/**
* Get the route dispatcher callback.
*
* @return \Closure
*/
protected function dispatchToRouter()
{
return function ($request) {
$this->app->instance('request', $request);

return $this->router->dispatch($request);
};
}

/**
* Call the terminate method on any terminable middleware.
*
Expand All @@ -157,6 +183,20 @@ protected function sendRequestThroughRouter($request)
* @return void
*/
public function terminate($request, $response)
{
$this->terminateMiddleware($request, $response);

$this->app->terminate();
}

/**
* Call the terminate method on any terminable middleware.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Response $response
* @return void
*/
protected function terminateMiddleware($request, $response)
{
$middlewares = $this->app->shouldSkipMiddleware() ? [] : array_merge(
$this->gatherRouteMiddleware($request),
Expand All @@ -176,8 +216,6 @@ public function terminate($request, $response)
$instance->terminate($request, $response);
}
}

$this->app->terminate();
}

/**
Expand Down Expand Up @@ -212,6 +250,17 @@ protected function parseMiddleware($middleware)
return [$name, $parameters];
}

/**
* Determine if the kernel has a given middleware.
*
* @param string $middleware
* @return bool
*/
public function hasMiddleware($middleware)
{
return in_array($middleware, $this->middleware);
}

/**
* Add a new middleware to beginning of the stack if it does not already exist.
*
Expand Down Expand Up @@ -242,43 +291,6 @@ public function pushMiddleware($middleware)
return $this;
}

/**
* Bootstrap the application for HTTP requests.
*
* @return void
*/
public function bootstrap()
{
if (! $this->app->hasBeenBootstrapped()) {
$this->app->bootstrapWith($this->bootstrappers());
}
}

/**
* Get the route dispatcher callback.
*
* @return \Closure
*/
protected function dispatchToRouter()
{
return function ($request) {
$this->app->instance('request', $request);

return $this->router->dispatch($request);
};
}

/**
* Determine if the kernel has a given middleware.
*
* @param string $middleware
* @return bool
*/
public function hasMiddleware($middleware)
{
return in_array($middleware, $this->middleware);
}

/**
* Get the bootstrap classes for the application.
*
Expand Down

0 comments on commit 43a5e5f

Please sign in to comment.