1: <?php namespace Laravel\Session\Drivers;
2:
3: class File extends Driver implements Sweeper {
4:
5: /**
6: * The path to which the session files should be written.
7: *
8: * @var string
9: */
10: private $path;
11:
12: /**
13: * Create a new File session driver instance.
14: *
15: * @param string $path
16: * @return void
17: */
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: * @param string $id
29: * @return array
30: */
31: public function load($id)
32: {
33: if (file_exists($path = $this->path.$id))
34: {
35: return unserialize(file_get_contents($path));
36: }
37: }
38:
39: /**
40: * Save a given session to storage.
41: *
42: * @param array $session
43: * @param array $config
44: * @param bool $exists
45: * @return void
46: */
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: * @return void
57: */
58: public function delete($id)
59: {
60: if (file_exists($this->path.$id))
61: {
62: @unlink($this->path.$id);
63: }
64: }
65:
66: /**
67: * Delete all expired sessions from persistent storage.
68: *
69: * @param int $expiration
70: * @return void
71: */
72: public function sweep($expiration)
73: {
74: $files = glob($this->path.'*');
75:
76: if ($files === false) return;
77:
78: foreach ($files as $file)
79: {
80: if (filetype($file) == 'file' and filemtime($file) < $expiration)
81: {
82: @unlink($file);
83: }
84: }
85: }
86:
87: }