2011-09-14 20:16:13 -05:00
|
|
|
<?php namespace Laravel\Session\Drivers;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2012-02-28 10:06:53 -06:00
|
|
|
class File extends Driver implements Sweeper {
|
2011-08-25 22:53:05 -05:00
|
|
|
|
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 string $path
|
2011-08-25 22:53:05 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-09-20 23:14:09 -05:00
|
|
|
public function __construct($path)
|
2011-08-25 22:53:05 -05:00
|
|
|
{
|
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-10-13 21:32:11 -05:00
|
|
|
if (file_exists($path = $this->path.$id))
|
|
|
|
|
{
|
|
|
|
|
return unserialize(file_get_contents($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-09-29 21:22:48 -05:00
|
|
|
* @param bool $exists
|
2011-08-25 22:53:05 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-09-29 21:22:48 -05:00
|
|
|
public function save($session, $config, $exists)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-13 21:32:11 -05:00
|
|
|
file_put_contents($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-10-15 14:39:52 -05:00
|
|
|
if (file_exists($this->path.$id))
|
|
|
|
|
{
|
|
|
|
|
@unlink($this->path.$id);
|
|
|
|
|
}
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
2012-07-21 20:18:55 -04:00
|
|
|
* Delete all expired sessions from persistent storage.
|
2011-08-25 22:53:05 -05:00
|
|
|
*
|
|
|
|
|
* @param int $expiration
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-06-08 23:45:08 -05:00
|
|
|
public function sweep($expiration)
|
|
|
|
|
{
|
2012-01-17 09:23:11 -06:00
|
|
|
$files = glob($this->path.'*');
|
|
|
|
|
|
|
|
|
|
if ($files === false) return;
|
|
|
|
|
|
|
|
|
|
foreach ($files as $file)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-13 21:32:11 -05:00
|
|
|
if (filetype($file) == 'file' and filemtime($file) < $expiration)
|
2011-09-09 20:55:24 -05:00
|
|
|
{
|
2011-10-13 21:32:11 -05:00
|
|
|
@unlink($file);
|
2011-09-09 20:55:24 -05:00
|
|
|
}
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|