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,23 +1,59 @@
<?php namespace Laravel\Session;
use Laravel\Cache;
use Laravel\Config;
class APC extends Driver {
/**
* The APC cache driver instance.
*
* @var Cache\APC
*/
private $apc;
/**
* Create a new APC session driver instance.
*
* @param Cache\APC $apc
* @return void
*/
public function __construct(\Laravel\Cache\APC $apc)
{
$this->apc = $apc;
}
/**
* 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)
{
return Cache::driver('apc')->get($id);
return $this->apc->get($id);
}
/**
* Save the session to persistant storage.
*
* @return void
*/
protected function save()
{
Cache::driver('apc')->put($this->session['id'], $this->session, Config::get('session.lifetime'));
$this->apc->put($this->session['id'], $this->session, Config::get('session.lifetime'));
}
/**
* Delete the session from persistant storage.
*
* @return void
*/
protected function delete()
{
Cache::driver('apc')->forget($this->session['id']);
$this->apc->forget($this->session['id']);
}
}

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');
}
}

View File

@@ -0,0 +1,96 @@
<?php namespace Laravel\Session;
use Laravel\Config;
use Laravel\Database\Connection;
class Database extends Driver implements Sweeper {
/**
* The database connection.
*
* @var Connection
*/
private $connection;
/**
* Create a new database session driver.
*
* @param Connection $connection
* @return void
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* 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)
{
$session = $this->table()->find($id);
if ( ! is_null($session))
{
return array(
'id' => $session->id,
'last_activity' => $session->last_activity,
'data' => unserialize($session->data)
);
}
}
/**
* Save the session to persistant storage.
*
* @return void
*/
protected function save()
{
$this->delete($this->session['id']);
$this->table()->insert(array(
'id' => $this->session['id'],
'last_activity' => $this->session['last_activity'],
'data' => serialize($this->session['data'])
));
}
/**
* Delete the session from persistant storage.
*
* @return void
*/
protected function delete()
{
$this->table()->delete($this->session['id']);
}
/**
* Delete all expired sessions from persistant storage.
*
* @param int $expiration
* @return void
*/
public function sweep($expiration)
{
$this->table()->where('last_activity', '<', $expiration)->delete();
}
/**
* Get a session database query.
*
* @return Query
*/
private function table()
{
return $this->connection->table(Config::get('session.table'));
}
}

View File

@@ -1,52 +0,0 @@
<?php namespace Laravel\Session;
use Laravel\Config;
class DB extends Driver implements Sweeper {
protected function load($id)
{
$session = $this->table()->find($id);
if ( ! is_null($session))
{
return array(
'id' => $session->id,
'last_activity' => $session->last_activity,
'data' => unserialize($session->data)
);
}
}
protected function save()
{
$this->delete($this->session['id']);
$this->table()->insert(array(
'id' => $this->session['id'],
'last_activity' => $this->session['last_activity'],
'data' => serialize($this->session['data'])
));
}
protected function delete()
{
$this->table()->delete($this->session['id']);
}
public function sweep($expiration)
{
$this->table()->where('last_activity', '<', $expiration)->delete();
}
/**
* Get a session database query.
*
* @return Query
*/
private function table()
{
return \System\DB::connection()->table(Config::get('session.table'));
}
}

View File

@@ -2,26 +2,69 @@
class File extends Driver implements Sweeper {
/**
* The file manager instance.
*
* @var Laravel\File
*/
private $file;
/**
* Create a new File session driver instance.
*
* @param Laravel\File $file
* @return void
*/
public function __construct(\Laravel\File $file)
{
$this->file = $file;
}
/**
* 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 (file_exists($path = SESSION_PATH.$id)) return unserialize(file_get_contents($path));
if ($this->file->exists($path = SESSION_PATH.$id)) return unserialize($this->file->get($path));
}
/**
* Save the session to persistant storage.
*
* @return void
*/
protected function save()
{
file_put_contents(SESSION_PATH.$this->session['id'], serialize($this->session), LOCK_EX);
$this->file->put(SESSION_PATH.$this->session['id'], serialize($this->session), LOCK_EX);
}
/**
* Delete the session from persistant storage.
*
* @return void
*/
protected function delete()
{
@unlink(SESSION_PATH.$this->session['id']);
$this->file->delete(SESSION_PATH.$this->session['id']);
}
/**
* Delete all expired sessions from persistant storage.
*
* @param int $expiration
* @return void
*/
public function sweep($expiration)
{
foreach (glob(SESSION_PATH.'*') as $file)
{
if (filetype($file) == 'file' and filemtime($file) < $expiration) @unlink($file);
if ($this->file->type($file) == 'file' and $this->file->modified($file) < $expiration) $this->file->delete($file);
}
}

View File

@@ -0,0 +1,59 @@
<?php namespace Laravel\Session;
use Laravel\Config;
class Manager {
/**
* The active session driver.
*
* @var Session\Driver
*/
public static $driver;
/**
* Get the session driver.
*
* The session driver returned will be the driver specified in the session configuration
* file. Only one session driver may be active for a given request, so the driver will
* be managed as a singleton.
*
* @return Session\Driver
*/
public static function driver()
{
if (is_null(static::$driver))
{
$driver = Config::get('session.driver');
if (in_array($driver, array('cookie', 'file', 'database', 'memcached')))
{
return static::$driver = IoC::container()->resolve('laravel.session.'.$driver);
}
throw new \Exception("Session driver [$driver] is not supported.");
}
return static::$driver;
}
/**
* Pass all other methods to the default session driver.
*
* By dynamically passing these method calls to the default driver, the developer is
* able to use with a convenient API when working with the session.
*
* <code>
* // Get an item from the default session driver
* $name = Session::get('name');
*
* // Equivalent call using the driver method
* $name = Session::driver()->get('name');
* </code>
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(static::driver(), $method), $parameters);
}
}

View File

@@ -1,23 +1,59 @@
<?php namespace Laravel\Session;
use Laravel\Cache;
use Laravel\Config;
class Memcached extends Driver {
/**
* The Memcache cache driver instance.
*
* @var Memcached
*/
private $memcached;
/**
* Create a new Memcached session driver instance.
*
* @param Memcached $memcached
* @return void
*/
public function __construct(\Laravel\Cache\Memcached $memcached)
{
$this->memcached = $memcached;
}
/**
* 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)
{
return Cache::driver('memcached')->get($id);
return $this->memcached->get($id);
}
/**
* Save the session to persistant storage.
*
* @return void
*/
protected function save()
{
Cache::driver('memcached')->put($this->session['id'], $this->session, Config::get('session.lifetime'));
$this->memcached->put($this->session['id'], $this->session, Config::get('session.lifetime'));
}
/**
* Delete the session from persistant storage.
*
* @return void
*/
protected function delete()
{
Cache::driver('memcached')->forget($this->session['id']);
$this->memcached->forget($this->session['id']);
}
}