Permalink
Browse files

Work like crazy on making sessions cleaner.

  • Loading branch information...
taylorotwell committed Oct 26, 2013
1 parent 9ecfb90 commit e0fe79e398003e54d54f2626e1283e97209b7f50
@@ -143,7 +143,7 @@ protected function getRecaller()
{
if (isset($this->cookie))
{
return $this->getCookieJar()->get($this->getRecallerName());
return $this->request->cookies->get($this->getRecallerName());
}
}
@@ -180,7 +180,7 @@ public function validate(array $credentials = array())
* Attempt to authenticate using HTTP Basic Auth.
*
* @param string $field
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response|null
*/
public function basic($field = 'email', Request $request = null)
@@ -201,7 +201,7 @@ public function basic($field = 'email', Request $request = null)
* Perform a stateless HTTP Basic login attempt.
*
* @param string $field
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response|null
*/
public function onceBasic($field = 'email', Request $request = null)
@@ -217,7 +217,7 @@ public function onceBasic($field = 'email', Request $request = null)
/**
* Attempt to authenticate using basic authentication.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $field
* @return bool
*/
@@ -231,7 +231,7 @@ protected function attemptBasic(Request $request, $field)
/**
* Get the credential array for a HTTP Basic request.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $field
* @return array
*/
@@ -2,18 +2,9 @@
use Closure;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CookieJar {
/**
* The current request instance.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The default path (if specified).
*
@@ -35,40 +26,6 @@ class CookieJar {
*/
protected $queued = array();
/**
* Create a new cookie manager instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Determine if a cookie exists and is not null.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return ! is_null($this->get($key));
}
/**
* Get the value of the given cookie.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
return $this->request->cookies->get($key) ?: value($default);
}
/**
* Create a new cookie instance.
*
@@ -117,6 +74,18 @@ public function forget($name)
return $this->make($name, null, -2628000);
}
/**
* Get a queued cookie instance.
*
* @param string $key
* @param mixed $default
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function queued($key, $default = null)
{
return array_get($this->queued, $key, $default);
}
/**
* Queue a cookie to send with the next response.
*
@@ -173,27 +142,6 @@ public function setDefaultPathAndDomain($path, $domain)
return $this;
}
/**
* Get the request instance.
*
* @return \Symfony\Component\HttpFoundation\Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Set the request instance.
*
* @param \Symfony\Component\HttpFoundation\Request
* @return void
*/
public function setRequest(Request $request)
{
$this->request = $request;
}
/**
* Get the cookies which have been queued for the next request
*
@@ -4,49 +4,6 @@
class CookieServiceProvider extends ServiceProvider {
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->registerRequestBinder();
$this->registerAfterEvent();
}
/**
* Register the request rebinding event.
*
* @return void
*/
protected function registerRequestBinder()
{
$this->app->rebinding('request', function($app, $request)
{
$app['cookie']->setRequest($request);
});
}
/**
* Register the "after" application event.
*
* @return void
*/
protected function registerAfterEvent()
{
$app = $this->app;
$this->app->after(function($request, $response) use ($app)
{
foreach ($app['cookie']->getQueuedCookies() as $cookie)
{
$response->headers->setCookie($cookie);
}
});
}
/**
* Register the service provider.
*
@@ -56,11 +13,9 @@ public function register()
{
$this->app['cookie'] = $this->app->share(function($app)
{
$cookies = new CookieJar($app['request'], $app['encrypter']);
$config = $app['config']['session'];
return $cookies->setDefaultPathAndDomain($config['path'], $config['domain']);
return with(new CookieJar)->setDefaultPathAndDomain($config['path'], $config['domain']);
});
}
}
@@ -0,0 +1,58 @@
<?php namespace Illuminate\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class Queue implements HttpKernelInterface {
/**
* The wrapped kernel implementation.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $app;
/**
* The cookie jar instance.
*
* @var \Illuminate\Cookie\CookieJar
*/
protected $encrypter;
/**
* Create a new CookieQueue instance.
*
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
* @param \Illuminate\Cookie\CookieJar $cookies
* @return void
*/
public function __construct(HttpKernelInterface $app, CookieJar $cookies)
{
$this->app = $app;
$this->cookies = $cookies;
}
/**
* Handle the given request and get the response.
*
* @implements HttpKernelInterface::handle
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param int $type
* @param bool $catch
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
$response = $this->app->handle($request, $type, $catch);
foreach ($this->cookies->getQueuedCookies() as $cookie)
{
$response->headers->setCookie($cookie);
}
return $response;
}
}
@@ -557,8 +557,12 @@ public function booted($callback)
*/
public function run(SymfonyRequest $request)
{
$this->refreshRequest($request = Request::createFromBase($request));
$response = with(new \Stack\Builder)
->push('Illuminate\Cookie\Guard', $this['encrypter'])
->push('Illuminate\Cookie\Queue', $this['cookie'])
->push('Illuminate\Session\Middleware', $this['session'])
->resolve($this)
->handle($request);
@@ -48,7 +48,7 @@ public function root()
public function url()
{
return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
}
}
/**
* Get the full URL for the request.
@@ -117,13 +117,13 @@ public function is($pattern)
return true;
}
}
return false;
}
/**
* Determine if the request is the result of an AJAX call.
*
*
* @return bool
*/
public function ajax()
@@ -233,6 +233,17 @@ public function query($key = null, $default = null)
return $this->retrieveItem('query', $key, $default);
}
/**
* Determine if a cookie is set on the request.
*
* @param string $key
* @return bool
*/
public function hasCookie($key)
{
return ! is_null($this->cookie($key));
}
/**
* Retrieve a cookie from the request.
*
@@ -327,7 +338,7 @@ public function flash($filter = null, $keys = array())
public function flashOnly($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return $this->flash('only', $keys);
}
@@ -340,7 +351,7 @@ public function flashOnly($keys)
public function flashExcept($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return $this->flash('except', $keys);
}
@@ -11,6 +11,13 @@ class CookieSessionHandler implements \SessionHandlerInterface {
*/
protected $cookie;
/**
* The request instance.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Create a new cookie driven handler instance.
*
@@ -45,7 +52,7 @@ public function close()
*/
public function read($sessionId)
{
return $this->cookie->get($sessionId) ?: '';
return $this->request->cookies->get($sessionId) ?: '';
}
/**
@@ -72,4 +79,15 @@ public function gc($lifetime)
return true;
}
/**
* Set the request instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return void
*/
public function setRequest(Request $request)
{
$this->request = $request;
}
}
Oops, something went wrong.

0 comments on commit e0fe79e

Please sign in to comment.