continued ioc refactoring.
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
use Laravel\Config;
|
||||
|
||||
class APC extends Driver {
|
||||
|
||||
/**
|
||||
@@ -11,15 +9,23 @@ class APC extends Driver {
|
||||
*/
|
||||
private $apc;
|
||||
|
||||
/**
|
||||
* The session lifetime.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $lifetime;
|
||||
|
||||
/**
|
||||
* Create a new APC session driver instance.
|
||||
*
|
||||
* @param Cache\APC $apc
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(\Laravel\Cache\APC $apc)
|
||||
public function __construct(\Laravel\Cache\APC $apc, $lifetime)
|
||||
{
|
||||
$this->apc = $apc;
|
||||
$this->lifetime = $lifetime;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,7 +49,7 @@ class APC extends Driver {
|
||||
*/
|
||||
protected function save()
|
||||
{
|
||||
$this->apc->put($this->session['id'], $this->session, Config::get('session.lifetime'));
|
||||
$this->apc->put($this->session['id'], $this->session, $this->lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
use Laravel\Config;
|
||||
use Laravel\Security\Crypter;
|
||||
|
||||
class Cookie extends Driver {
|
||||
@@ -19,22 +18,26 @@ class Cookie extends Driver {
|
||||
*/
|
||||
private $crypter;
|
||||
|
||||
/**
|
||||
* The session configuration array.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* Create a new Cookie session driver instance.
|
||||
*
|
||||
* @param Crypter $crypter
|
||||
* @param Laravel\Cookie $cookie
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Crypter $crypter, \Laravel\Cookie $cookie)
|
||||
public function __construct(Crypter $crypter, \Laravel\Cookie $cookie, $config)
|
||||
{
|
||||
$this->cookie = $cookie;
|
||||
$this->config = $config;
|
||||
$this->crypter = $crypter;
|
||||
|
||||
if (Config::get('application.key') == '')
|
||||
{
|
||||
throw new \Exception("You must set an application key before using the Cookie session driver.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +66,7 @@ class Cookie extends Driver {
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
extract(Config::get('session'));
|
||||
extract($this->config);
|
||||
|
||||
$payload = $this->crypter->encrypt(serialize($this->session));
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
use Laravel\Config;
|
||||
use Laravel\Database\Connection;
|
||||
|
||||
class Database extends Driver implements Sweeper {
|
||||
@@ -12,14 +11,23 @@ class Database extends Driver implements Sweeper {
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* The database table to which the sessions should be written.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $table;
|
||||
|
||||
/**
|
||||
* Create a new database session driver.
|
||||
*
|
||||
* @param Connection $connection
|
||||
* @param string $table
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Connection $connection)
|
||||
public function __construct(Connection $connection, $table)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
@@ -90,7 +98,7 @@ class Database extends Driver implements Sweeper {
|
||||
*/
|
||||
private function table()
|
||||
{
|
||||
return $this->connection->table(Config::get('session.table'));
|
||||
return $this->connection->table($this->table);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
use Laravel\Str;
|
||||
use Laravel\Config;
|
||||
use Laravel\Input;
|
||||
use Laravel\Cookie;
|
||||
|
||||
abstract class Driver {
|
||||
@@ -22,13 +22,14 @@ abstract class Driver {
|
||||
* If the session has expired, a new, empty session will be generated.
|
||||
*
|
||||
* @param string $id
|
||||
* @param int $lifetime
|
||||
* @return void
|
||||
*/
|
||||
public function start($id)
|
||||
public function start($id, $lifetime)
|
||||
{
|
||||
$this->session = ( ! is_null($id)) ? $this->load($id) : null;
|
||||
|
||||
if (is_null($this->session) or (time() - $this->session['last_activity']) > (Config::get('session.lifetime') * 60))
|
||||
if (is_null($this->session) or (time() - $this->session['last_activity']) > ($lifetime * 60))
|
||||
{
|
||||
$this->session = array('id' => Str::random(40), 'data' => array());
|
||||
}
|
||||
@@ -117,11 +118,13 @@ abstract class Driver {
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
* @return Driver
|
||||
*/
|
||||
public function put($key, $value)
|
||||
{
|
||||
$this->session['data'][$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,11 +140,13 @@ abstract class Driver {
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
* @return Driver
|
||||
*/
|
||||
public function flash($key, $value)
|
||||
{
|
||||
$this->put(':new:'.$key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,7 +158,7 @@ abstract class Driver {
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @return void
|
||||
* @return Driver
|
||||
*/
|
||||
public function forget($key)
|
||||
{
|
||||
@@ -188,59 +193,94 @@ abstract class Driver {
|
||||
* The session will be stored in persistant storage and the session cookie will be
|
||||
* session cookie will be sent to the browser.
|
||||
*
|
||||
* @param Laravel\Cookie $cookie
|
||||
* The input of the current request will also be flashed to the session so it is
|
||||
* available for the next request via the "old" method on the input class.
|
||||
*
|
||||
* @param Laravel\Input $input
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
public function close(\Laravel\Cookie $cookie)
|
||||
public function close(Input $input, $config)
|
||||
{
|
||||
$this->age_flash();
|
||||
$this->flash('laravel_old_input', $input->get())->age();
|
||||
|
||||
$this->save();
|
||||
|
||||
$this->write_cookie($cookie);
|
||||
$this->write_cookie($input->cookies, $config);
|
||||
|
||||
if ($this instanceof Sweeper and mt_rand(1, 100) <= 2)
|
||||
{
|
||||
$this->sweep(time() - ($config['lifetime'] * 60));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Age the session flash data.
|
||||
*
|
||||
* To age the data, we will forget all of the old keys and then rewrite the newly
|
||||
* flashed items to have old keys, which will be available for the next request.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function age_flash()
|
||||
protected function age()
|
||||
{
|
||||
foreach ($this->session['data'] as $key => $value)
|
||||
{
|
||||
if (strpos($key, ':old:') === 0) $this->forget($key);
|
||||
}
|
||||
|
||||
foreach ($this->session['data'] as $key => $value)
|
||||
{
|
||||
if (strpos($key, ':new:') === 0)
|
||||
{
|
||||
$this->put(':old:'.substr($key, 5), $value);
|
||||
$session = $this->session['data'];
|
||||
|
||||
$this->forget($key);
|
||||
}
|
||||
}
|
||||
$this->session['data'] = array_combine(str_replace(':new:', ':old:', array_keys($session)), array_values($session));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the session cookie.
|
||||
*
|
||||
* All of the session cookie configuration options are stored in the session
|
||||
* configuration file. The cookie will only be written if the headers have not
|
||||
* already been sent to the browser.
|
||||
*
|
||||
* @param Laravel\Cookie $cookie
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
protected function write_cookie(\Laravel\Cookie $cookie)
|
||||
protected function write_cookie(Cookie $cookies, $config)
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
$config = Config::get('session');
|
||||
|
||||
extract($config);
|
||||
|
||||
$minutes = ($expire_on_close) ? 0 : $lifetime;
|
||||
|
||||
$cookie->put('laravel_session', $this->session['id'], $minutes, $path, $domain, $https, $http_only);
|
||||
$cookies->put('laravel_session', $this->session['id'], $minutes, $path, $domain, $https, $http_only);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for retrieving items from the session.
|
||||
*
|
||||
* <code>
|
||||
* // Get the "name" item from the session
|
||||
* $name = $application->session->name;
|
||||
* </code>
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for writings items to the session.
|
||||
*
|
||||
* <code>
|
||||
* // Write "Fred" to the session "name" item
|
||||
* $application->session->name = 'Fred';
|
||||
* </code>
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
$this->put($key, $value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,15 +9,24 @@ class File extends Driver implements Sweeper {
|
||||
*/
|
||||
private $file;
|
||||
|
||||
/**
|
||||
* The path to which the session files should be written.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $path;
|
||||
|
||||
/**
|
||||
* Create a new File session driver instance.
|
||||
*
|
||||
* @param Laravel\File $file
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(\Laravel\File $file)
|
||||
public function __construct(\Laravel\File $file, $path)
|
||||
{
|
||||
$this->file = $file;
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,7 +40,7 @@ class File extends Driver implements Sweeper {
|
||||
*/
|
||||
protected function load($id)
|
||||
{
|
||||
if ($this->file->exists($path = SESSION_PATH.$id)) return unserialize($this->file->get($path));
|
||||
if ($this->file->exists($path = $this->path.$id)) return unserialize($this->file->get($path));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,7 +50,7 @@ class File extends Driver implements Sweeper {
|
||||
*/
|
||||
protected function save()
|
||||
{
|
||||
$this->file->put(SESSION_PATH.$this->session['id'], serialize($this->session), LOCK_EX);
|
||||
$this->file->put($this->path.$this->session['id'], serialize($this->session), LOCK_EX);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +60,7 @@ class File extends Driver implements Sweeper {
|
||||
*/
|
||||
protected function delete()
|
||||
{
|
||||
$this->file->delete(SESSION_PATH.$this->session['id']);
|
||||
$this->file->delete($this->path.$this->session['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,7 +71,7 @@ class File extends Driver implements Sweeper {
|
||||
*/
|
||||
public function sweep($expiration)
|
||||
{
|
||||
foreach (glob(SESSION_PATH.'*') as $file)
|
||||
foreach (glob($this->path.'*') as $file)
|
||||
{
|
||||
if ($this->file->type($file) == 'file' and $this->file->modified($file) < $expiration) $this->file->delete($file);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,10 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
use Laravel\IoC;
|
||||
use Laravel\Config;
|
||||
use Laravel\Container;
|
||||
|
||||
class Manager {
|
||||
|
||||
/**
|
||||
* The active session driver.
|
||||
*
|
||||
* @var Session\Driver
|
||||
*/
|
||||
public static $driver;
|
||||
|
||||
/**
|
||||
* Get the session driver.
|
||||
*
|
||||
@@ -19,42 +12,18 @@ class Manager {
|
||||
* file. Only one session driver may be active for a given request, so the driver will
|
||||
* be managed as a singleton.
|
||||
*
|
||||
* @param Container $container
|
||||
* @param string $driver
|
||||
* @return Session\Driver
|
||||
*/
|
||||
public static function driver()
|
||||
public static function driver(Container $container, $driver)
|
||||
{
|
||||
if (is_null(static::$driver))
|
||||
if (in_array($driver, array('cookie', 'file', 'database', 'apc', 'memcached')))
|
||||
{
|
||||
$driver = Config::get('session.driver');
|
||||
|
||||
if (in_array($driver, array('cookie', 'file', 'database', 'apc', 'memcached')))
|
||||
{
|
||||
return static::$driver = IoC::container()->resolve('laravel.session.'.$driver);
|
||||
}
|
||||
|
||||
throw new \Exception("Session driver [$driver] is not supported.");
|
||||
return $container->resolve('laravel.session.'.$driver);
|
||||
}
|
||||
|
||||
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);
|
||||
throw new \Exception("Session driver [$driver] is not supported.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
use Laravel\Config;
|
||||
|
||||
class Memcached extends Driver {
|
||||
|
||||
/**
|
||||
@@ -11,14 +9,22 @@ class Memcached extends Driver {
|
||||
*/
|
||||
private $memcached;
|
||||
|
||||
/**
|
||||
* The session lifetime.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $lifetime;
|
||||
|
||||
/**
|
||||
* Create a new Memcached session driver instance.
|
||||
*
|
||||
* @param Memcached $memcached
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(\Laravel\Cache\Memcached $memcached)
|
||||
public function __construct(\Laravel\Cache\Memcached $memcached, $lifetime)
|
||||
{
|
||||
$this->lifetime = $lifetime;
|
||||
$this->memcached = $memcached;
|
||||
}
|
||||
|
||||
@@ -43,7 +49,7 @@ class Memcached extends Driver {
|
||||
*/
|
||||
protected function save()
|
||||
{
|
||||
$this->memcached->put($this->session['id'], $this->session, Config::get('session.lifetime'));
|
||||
$this->memcached->put($this->session['id'], $this->session, $this->lifetime);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user