Projekt

Obecné

Profil

Stáhnout (2.19 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace Illuminate\Session;
4

    
5
use Carbon\Carbon;
6
use SessionHandlerInterface;
7
use Symfony\Component\Finder\Finder;
8
use Illuminate\Filesystem\Filesystem;
9

    
10
class FileSessionHandler implements SessionHandlerInterface
11
{
12
    /**
13
     * The filesystem instance.
14
     *
15
     * @var \Illuminate\Filesystem\Filesystem
16
     */
17
    protected $files;
18

    
19
    /**
20
     * The path where sessions should be stored.
21
     *
22
     * @var string
23
     */
24
    protected $path;
25

    
26
    /**
27
     * The number of minutes the session should be valid.
28
     *
29
     * @var int
30
     */
31
    protected $minutes;
32

    
33
    /**
34
     * Create a new file driven handler instance.
35
     *
36
     * @param  \Illuminate\Filesystem\Filesystem  $files
37
     * @param  string  $path
38
     * @param  int  $minutes
39
     * @return void
40
     */
41
    public function __construct(Filesystem $files, $path, $minutes)
42
    {
43
        $this->path = $path;
44
        $this->files = $files;
45
        $this->minutes = $minutes;
46
    }
47

    
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function open($savePath, $sessionName)
52
    {
53
        return true;
54
    }
55

    
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function close()
60
    {
61
        return true;
62
    }
63

    
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function read($sessionId)
68
    {
69
        if ($this->files->exists($path = $this->path.'/'.$sessionId)) {
70
            if (filemtime($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
71
                return $this->files->get($path);
72
            }
73
        }
74

    
75
        return '';
76
    }
77

    
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function write($sessionId, $data)
82
    {
83
        $this->files->put($this->path.'/'.$sessionId, $data, true);
84
    }
85

    
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function destroy($sessionId)
90
    {
91
        $this->files->delete($this->path.'/'.$sessionId);
92
    }
93

    
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function gc($lifetime)
98
    {
99
        $files = Finder::create()
100
                    ->in($this->path)
101
                    ->files()
102
                    ->ignoreDotFiles(true)
103
                    ->date('<= now - '.$lifetime.' seconds');
104

    
105
        foreach ($files as $file) {
106
            $this->files->delete($file->getRealPath());
107
        }
108
    }
109
}
(6-6/13)