Files
spa-api/laravel/session/drivers/file.php

79 lines
1.4 KiB
PHP
Raw Normal View History

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