file.php 1.5 KB
Newer Older
1
<?php namespace Laravel\Session\Drivers;
2

3
class File implements Driver, Sweeper {
4 5

	/**
6 7 8 9 10 11 12
	 * The path to which the session files should be written.
	 *
	 * @var string
	 */
	private $path;

	/**
13 14
	 * Create a new File session driver instance.
	 *
Taylor Otwell committed
15
	 * @param  string        $path
16 17
	 * @return void
	 */
18
	public function __construct($path)
19
	{
20
		$this->path = $path;
21 22 23
	}

	/**
24
	 * Load a session from storage by a given ID.
25
	 *
26
	 * If no session is found for the ID, null will be returned.
27
	 *
28 29 30
	 * @param  string  $id
	 * @return array
	 */
31
	public function load($id)
32
	{
33 34 35 36
		if (file_exists($path = $this->path.$id))
		{
			return unserialize(file_get_contents($path));
		}
37 38
	}

39
	/**
40
	 * Save a given session to storage.
41
	 *
42
	 * @param  array  $session
43
	 * @param  array  $config
44
	 * @param  bool   $exists
45 46
	 * @return void
	 */
47
	public function save($session, $config, $exists)
48
	{
49
		file_put_contents($this->path.$session['id'], serialize($session), LOCK_EX);
50 51
	}

52
	/**
53
	 * Delete a session from storage by a given ID.
54
	 *
55
	 * @param  string  $id
56 57
	 * @return void
	 */
58
	public function delete($id)
59
	{
60 61 62 63
		if (file_exists($this->path.$id))
		{
			@unlink($this->path.$id);
		}
64 65
	}

66 67 68 69 70 71
	/**
	 * Delete all expired sessions from persistant storage.
	 *
	 * @param  int   $expiration
	 * @return void
	 */
72 73
	public function sweep($expiration)
	{
74 75 76 77 78
		$files = glob($this->path.'*');

		if ($files === false) return;

		foreach ($files as $file)
79
		{
80
			if (filetype($file) == 'file' and filemtime($file) < $expiration)
81
			{
82
				@unlink($file);
83
			}
84 85 86 87
		}
	}
	
}