2011-09-14 20:16:13 -05:00
|
|
|
<?php namespace Laravel\Session\Drivers;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-09-15 20:23:30 -05:00
|
|
|
class File implements Driver, Sweeper {
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
2011-08-30 22:09:47 -05:00
|
|
|
* The file engine instance.
|
2011-08-25 22:53:05 -05:00
|
|
|
*
|
2011-08-30 22:35:32 -05:00
|
|
|
* @var Laravel\File
|
2011-08-25 22:53:05 -05:00
|
|
|
*/
|
|
|
|
|
private $file;
|
|
|
|
|
|
2011-08-26 21:42:04 -05:00
|
|
|
/**
|
|
|
|
|
* The path to which the session files should be written.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $path;
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Create a new File session driver instance.
|
|
|
|
|
*
|
2011-08-30 22:35:32 -05:00
|
|
|
* @param Laravel\File $file
|
|
|
|
|
* @param string $path
|
2011-08-25 22:53:05 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-08-30 22:35:32 -05:00
|
|
|
public function __construct(\Laravel\File $file, $path)
|
2011-08-25 22:53:05 -05:00
|
|
|
{
|
|
|
|
|
$this->file = $file;
|
2011-08-26 21:42:04 -05:00
|
|
|
$this->path = $path;
|
2011-08-25 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-15 20:23:30 -05:00
|
|
|
* Load a session from storage by a given ID.
|
2011-08-25 22:53:05 -05:00
|
|
|
*
|
2011-09-15 20:23:30 -05:00
|
|
|
* If no session is found for the ID, null will be returned.
|
2011-09-14 23:54:14 -05:00
|
|
|
*
|
2011-08-25 22:53:05 -05:00
|
|
|
* @param string $id
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2011-09-15 20:23:30 -05:00
|
|
|
public function load($id)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-26 21:42:04 -05:00
|
|
|
if ($this->file->exists($path = $this->path.$id)) return unserialize($this->file->get($path));
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
2011-09-15 20:23:30 -05:00
|
|
|
* Save a given session to storage.
|
2011-08-25 22:53:05 -05:00
|
|
|
*
|
2011-09-14 23:54:14 -05:00
|
|
|
* @param array $session
|
2011-09-15 20:23:30 -05:00
|
|
|
* @param array $config
|
2011-08-25 22:53:05 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-09-15 20:23:30 -05:00
|
|
|
public function save($session, $config)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-09-14 23:54:14 -05:00
|
|
|
$this->file->put($this->path.$session['id'], serialize($session), LOCK_EX);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
2011-09-15 20:23:30 -05:00
|
|
|
* Delete a session from storage by a given ID.
|
2011-08-25 22:53:05 -05:00
|
|
|
*
|
2011-09-14 23:54:14 -05:00
|
|
|
* @param string $id
|
2011-08-25 22:53:05 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-09-15 20:23:30 -05:00
|
|
|
public function delete($id)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-09-14 23:54:14 -05:00
|
|
|
$this->file->delete($this->path.$id);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Delete all expired sessions from persistant storage.
|
|
|
|
|
*
|
|
|
|
|
* @param int $expiration
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-06-08 23:45:08 -05:00
|
|
|
public function sweep($expiration)
|
|
|
|
|
{
|
2011-08-26 21:42:04 -05:00
|
|
|
foreach (glob($this->path.'*') as $file)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-09-09 20:55:24 -05:00
|
|
|
if ($this->file->type($file) == 'file' and $this->file->modified($file) < $expiration)
|
|
|
|
|
{
|
|
|
|
|
$this->file->delete($file);
|
|
|
|
|
}
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|