Skip to content

Commit

Permalink
Merge pull request #949 from LuckyCyborg/master
Browse files Browse the repository at this point in the history
 	Split the Application finishing logic from Support\Facades\Session to Support\Facades\App
  • Loading branch information
daveismynamecom committed Jun 6, 2016
2 parents 141c300 + 985fc07 commit 3741776
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 185 deletions.
1 change: 1 addition & 0 deletions app/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'Str' => '\Support\Str',

// The Support Facades
'App' => '\Support\Facades\App',
'Auth' => '\Support\Facades\Auth',
'Cookie' => '\Support\Facades\Cookie',
'Crypt' => '\Support\Facades\Crypt',
Expand Down
8 changes: 4 additions & 4 deletions app/Modules/Users/Views/Admin/Roles/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
echo "
<tr>
<td style='text-align: center; vertical-align: middle;' width='5%'>" .$role->id ."</td>
<td style='text-align: center; vertical-align: middle;' width='15%'>" .$role->name ."</td>
<td style='text-align: center; vertical-align: middle;' width='15%'>" .$role->slug ."</td>
<td style='text-align: left; vertical-align: middle;' width='45%'>" .$role->description ."</td>
<td style='text-align: center; vertical-align: middle;' width='5%'>" .$role->users->count() ."</td>
<td style='text-align: center; vertical-align: middle;' width='17%'>" .$role->name ."</td>
<td style='text-align: center; vertical-align: middle;' width='17%'>" .$role->slug ."</td>
<td style='text-align: left; vertical-align: middle;' width='40%'>" .$role->description ."</td>
<td style='text-align: center; vertical-align: middle;' width='6%'>" .$role->users->count() ."</td>
<td style='text-align: right; vertical-align: middle;' width='15%'>
<div class='btn-group' role='group' aria-label='...'>
<a class='btn btn-sm btn-warning' href='" .site_url('admin/roles/' .$role->id). "' title='". __d('users', 'Show the Details') ."' role='button'><i class='fa fa-search'></i></a>
Expand Down
8 changes: 4 additions & 4 deletions system/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

use App;
use Event;
use Response;
use Session;


/**
Expand Down Expand Up @@ -133,7 +133,7 @@ public function execute($method, $params = array())
// The Method returned a Response instance; send it and stop the processing.
if ($result instanceof SymfonyResponse) {
// Finish the Session and send the Response.
Session::finish($result);
App::finish($result);

return true;
}
Expand Down Expand Up @@ -181,7 +181,7 @@ protected function createResponse($result)
$response = Response::make($content, 200, $headers);

// Finish the Session and send the Response.
Session::finish($response);
App::finish($response);

return true;
} else if (! $result instanceof BaseView) {
Expand All @@ -199,7 +199,7 @@ protected function createResponse($result)
$response = Response::make($result);

// Finish the Session and send the Response.
Session::finish($response);
App::finish($response);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions system/Helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Helpers\Inflector;

use Support\Facades\Redirect;
use Support\Facades\Session as SessionStore;
use Support\Facades\App;


/**
Expand All @@ -36,7 +36,7 @@ public static function redirect($url = null, $fullPath = false, $code = 302)
$response = Redirect::to($url, $code);

// Finish the Session (and send the Response).
SessionStore::finish($response);
App::finish($response);

// Quit the Nova Framework's execution.
exit();
Expand Down
6 changes: 3 additions & 3 deletions system/Routing/BaseRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;

use App;
use Console;
use Response;
use Session;


/**
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function invokeCallback($callback, $params = array())

if($result instanceof SymfonyResponse) {
// Finsih the Session Store.
Session::finish($result);
App::finish($result);

// Send the Response.
$result->send();
Expand All @@ -166,7 +166,7 @@ protected function invokeCallback($callback, $params = array())
$response = Response::make($result);

// Finish the Session Store.
Session::finish($response);
App::finish($response);

// Send the Response.
$response->send();
Expand Down
4 changes: 2 additions & 2 deletions system/Routing/ClassicRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use Routing\BaseRouter;
use Routing\Route;

use App;
use Response;
use Request;
use Session;


/**
Expand Down Expand Up @@ -127,7 +127,7 @@ public function dispatch()
$response = Response::error(404, $data);

// Finish the Session and send the Response.
Session::finish($response);
App::finish($response);

return false;
}
Expand Down
6 changes: 3 additions & 3 deletions system/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

use App;
use Response;
use Request;
use Session;


/**
Expand Down Expand Up @@ -256,7 +256,7 @@ public function dispatch()

if($result instanceof SymfonyResponse) {
// Finish the Session and send the Response.
Session::finish($result);
App::finish($result);

return true;
}
Expand All @@ -279,7 +279,7 @@ public function dispatch()
$response = Response::error(404, $data);

// Finish the Session and send the Response.
Session::finish($response);
App::finish($response);

return false;
}
Expand Down
192 changes: 192 additions & 0 deletions system/Support/Facades/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php
/**
* App - A Facade to the Application.
*
* @author Virgil-Adrian Teaca - [email protected]
* @version 3.0
*/

namespace Support\Facades;

use Core\Config;
use Forensics\Profiler as QuickProfiler;
use Http\Response as HttpResponse;
use Session\SessionInterface;

use Support\Facades\Cookie;
use Support\Facades\Crypt;
use Support\Facades\Request;
use Support\Facades\Session;

use Symfony\Component\HttpFoundation\Cookie as SymfonyCookie;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;



class App
{
/**
* Initialize the Application instance
*
* @return void
*/
public static function init()
{
}

/**
* Finalize the Session Store and send the Response
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
public static function finish(SymfonyResponse $response)
{
// Get the Session Store configuration.
$config = Config::get('session');

// Get the Request instance.
$request = Request::instance();

// Get the Session Store instance.
$session = Session::instance();

// Store the Session ID in a Cookie.
$cookie = Cookie::make(
$config['cookie'],
$session->getId(),
$config['lifetime'],
$config['path'],
$config['domain'],
$config['secure'],
false
);

Cookie::queue($cookie);

// Save the Session Store data.
$session->save();

// Collect the garbage for the Session Store instance.
static::collectGarbage($session, $config);

// Add all Request and queued Cookies.
static::processCookies($response, $config);

// Finally, minify the Response's Content.
static::processContent($response);

// Prepare the Response instance for sending.
$response->prepare($request);

// Send the Response.
$response->send();
}

/**
* Minify the Response instance Content.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
protected static function processContent(SymfonyResponse $response)
{
if (! $response instanceof HttpResponse) {
return;
}

$content = $response->getContent();

if(ENVIRONMENT == 'development') {
// Insert the QuickProfiler Widget in the Response's Content.

$content = str_replace(
'<!-- DO NOT DELETE! - Forensics Profiler -->',
QuickProfiler::process(true),
$content
);
} else if(ENVIRONMENT == 'production') {
// Minify the Response's Content.

$search = array(
'/\>[^\S ]+/s', // Strip whitespaces after tags, except space.
'/[^\S ]+\</s', // Strip whitespaces before tags, except space.
'/(\s)+/s' // Shorten multiple whitespace sequences.
);

$replace = array('>', '<', '\\1');

$content = preg_replace($search, $replace, $content);
}

$response->setContent($content);
}

/**
* Remove the garbage from the session if necessary.
*
* @param \Illuminate\Session\SessionInterface $session
* @return void
*/
protected static function collectGarbage(SessionInterface $session, array $config)
{
$lifeTime = $config['lifetime'] * 60; // The option is in minutes.

// Here we will see if this request hits the garbage collection lottery by hitting
// the odds needed to perform garbage collection on any given request. If we do
// hit it, we'll call this handler to let it delete all the expired sessions.
if (static::configHitsLottery($config)) {
$session->getHandler()->gc($lifeTime);
}
}

/**
* Add all the queued Cookies to Response instance and encrypt all Cookies.
*
* @return void
*/
protected static function processCookies(SymfonyResponse $response, array $config)
{
// Insert all queued Cookies on the Response instance.
foreach (Cookie::getQueuedCookies() as $cookie) {
$response->headers->setCookie($cookie);
}

if($config['encrypt'] == false) {
// The Cookies encryption is disabled.
return;
}

// Encrypt all Cookies present on the Response instance.
foreach ($response->headers->getCookies() as $key => $cookie) {
if($key == 'PHPSESSID') {
// Leave alone the PHPSESSID.
continue;
}

// Create a new Cookie with the content encrypted.
$cookie = new SymfonyCookie(
$cookie->getName(),
Crypt::encrypt($cookie->getValue()),
$cookie->getExpiresTime(),
$cookie->getPath(),
$cookie->getDomain(),
$cookie->isSecure(),
$cookie->isHttpOnly()
);

$response->headers->setCookie($cookie);
}
}

/**
* Determine if the configuration odds hit the lottery.
*
* @param array $config
* @return bool
*/
protected static function configHitsLottery(array $config)
{
return (mt_rand(1, $config['lottery'][1]) <= $config['lottery'][0]);
}
}
Loading

0 comments on commit 3741776

Please sign in to comment.