Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Session;
4
 
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Carbon;
7
use SessionHandlerInterface;
8
use Symfony\Component\Finder\Finder;
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
     * @return bool
52
     */
53
    public function open($savePath, $sessionName): bool
54
    {
55
        return true;
56
    }
57
 
58
    /**
59
     * {@inheritdoc}
60
     *
61
     * @return bool
62
     */
63
    public function close(): bool
64
    {
65
        return true;
66
    }
67
 
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @return string|false
72
     */
73
    public function read($sessionId): string|false
74
    {
75
        if ($this->files->isFile($path = $this->path.'/'.$sessionId) &&
76
            $this->files->lastModified($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
77
            return $this->files->sharedGet($path);
78
        }
79
 
80
        return '';
81
    }
82
 
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @return bool
87
     */
88
    public function write($sessionId, $data): bool
89
    {
90
        $this->files->put($this->path.'/'.$sessionId, $data, true);
91
 
92
        return true;
93
    }
94
 
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @return bool
99
     */
100
    public function destroy($sessionId): bool
101
    {
102
        $this->files->delete($this->path.'/'.$sessionId);
103
 
104
        return true;
105
    }
106
 
107
    /**
108
     * {@inheritdoc}
109
     *
110
     * @return int
111
     */
112
    public function gc($lifetime): int
113
    {
114
        $files = Finder::create()
115
                    ->in($this->path)
116
                    ->files()
117
                    ->ignoreDotFiles(true)
118
                    ->date('<= now - '.$lifetime.' seconds');
119
 
120
        $deletedSessions = 0;
121
 
122
        foreach ($files as $file) {
123
            $this->files->delete($file->getRealPath());
124
            $deletedSessions++;
125
        }
126
 
127
        return $deletedSessions;
128
    }
129
}