refactoring for dependency injection and testability.

This commit is contained in:
Taylor Otwell
2011-08-25 22:53:05 -05:00
parent 0b86c94551
commit 1e7850d9ba
75 changed files with 1441 additions and 1461 deletions

View File

@@ -1,10 +1,17 @@
<?php namespace Laravel\Session;
use Laravel\Config;
use Laravel\Crypter;
use Laravel\Security\Crypter;
class Cookie extends Driver {
/**
* The cookie engine instance.
*
* @var Cookie_Engine
*/
private $cookie;
/**
* The Crypter instance.
*
@@ -15,11 +22,14 @@ class Cookie extends Driver {
/**
* Create a new Cookie session driver instance.
*
* @param Crypter $crypter
* @param Cookie $cookie
* @return void
*/
public function __construct()
public function __construct(Crypter $crypter, Cookie $cookie)
{
$this->crypter = new Crypter;
$this->cookie = $cookie;
$this->crypter = $crypter;
if (Config::get('application.key') == '')
{
@@ -27,14 +37,28 @@ class Cookie extends Driver {
}
}
/**
* Load a session by ID.
*
* The session will be retrieved from persistant storage and returned as an array.
* The array contains the session ID, last activity UNIX timestamp, and session data.
*
* @param string $id
* @return array
*/
protected function load($id)
{
if (\System\Cookie::has('session_payload'))
if ($this->cookie->has('session_payload'))
{
return unserialize($this->crypter->decrypt(\System\Cookie::get('session_payload')));
return unserialize($this->crypter->decrypt($this->cookie->get('session_payload')));
}
}
/**
* Save the session to persistant storage.
*
* @return void
*/
protected function save()
{
if ( ! headers_sent())
@@ -43,13 +67,18 @@ class Cookie extends Driver {
$payload = $this->crypter->encrypt(serialize($this->session));
\System\Cookie::put('session_payload', $payload, $lifetime, $path, $domain, $https, $http_only);
$this->cookie->put('session_payload', $payload, $lifetime, $path, $domain, $https, $http_only);
}
}
/**
* Delete the session from persistant storage.
*
* @return void
*/
protected function delete()
{
\System\Cookie::forget('session_payload');
$this->cookie->forget('session_payload');
}
}