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\Filesystem;
4
 
5
use Exception;
6
use Illuminate\Contracts\Filesystem\LockTimeoutException;
7
 
8
class LockableFile
9
{
10
    /**
11
     * The file resource.
12
     *
13
     * @var resource
14
     */
15
    protected $handle;
16
 
17
    /**
18
     * The file path.
19
     *
20
     * @var string
21
     */
22
    protected $path;
23
 
24
    /**
25
     * Indicates if the file is locked.
26
     *
27
     * @var bool
28
     */
29
    protected $isLocked = false;
30
 
31
    /**
32
     * Create a new File instance.
33
     *
34
     * @param  string  $path
35
     * @param  string  $mode
36
     * @return void
37
     */
38
    public function __construct($path, $mode)
39
    {
40
        $this->path = $path;
41
 
42
        $this->ensureDirectoryExists($path);
43
        $this->createResource($path, $mode);
44
    }
45
 
46
    /**
47
     * Create the file's directory if necessary.
48
     *
49
     * @param  string  $path
50
     * @return void
51
     */
52
    protected function ensureDirectoryExists($path)
53
    {
54
        if (! file_exists(dirname($path))) {
55
            @mkdir(dirname($path), 0777, true);
56
        }
57
    }
58
 
59
    /**
60
     * Create the file resource.
61
     *
62
     * @param  string  $path
63
     * @param  string  $mode
64
     * @return void
65
     *
66
     * @throws \Exception
67
     */
68
    protected function createResource($path, $mode)
69
    {
70
        $this->handle = @fopen($path, $mode);
71
 
72
        if (! $this->handle) {
73
            throw new Exception('Unable to create lockable file: '.$path.'. Please ensure you have permission to create files in this location.');
74
        }
75
    }
76
 
77
    /**
78
     * Read the file contents.
79
     *
80
     * @param  int|null  $length
81
     * @return string
82
     */
83
    public function read($length = null)
84
    {
85
        clearstatcache(true, $this->path);
86
 
87
        return fread($this->handle, $length ?? ($this->size() ?: 1));
88
    }
89
 
90
    /**
91
     * Get the file size.
92
     *
93
     * @return int
94
     */
95
    public function size()
96
    {
97
        return filesize($this->path);
98
    }
99
 
100
    /**
101
     * Write to the file.
102
     *
103
     * @param  string  $contents
104
     * @return $this
105
     */
106
    public function write($contents)
107
    {
108
        fwrite($this->handle, $contents);
109
 
110
        fflush($this->handle);
111
 
112
        return $this;
113
    }
114
 
115
    /**
116
     * Truncate the file.
117
     *
118
     * @return $this
119
     */
120
    public function truncate()
121
    {
122
        rewind($this->handle);
123
 
124
        ftruncate($this->handle, 0);
125
 
126
        return $this;
127
    }
128
 
129
    /**
130
     * Get a shared lock on the file.
131
     *
132
     * @param  bool  $block
133
     * @return $this
134
     *
135
     * @throws \Illuminate\Contracts\Filesystem\LockTimeoutException
136
     */
137
    public function getSharedLock($block = false)
138
    {
139
        if (! flock($this->handle, LOCK_SH | ($block ? 0 : LOCK_NB))) {
140
            throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}].");
141
        }
142
 
143
        $this->isLocked = true;
144
 
145
        return $this;
146
    }
147
 
148
    /**
149
     * Get an exclusive lock on the file.
150
     *
151
     * @param  bool  $block
152
     * @return bool
153
     *
154
     * @throws \Illuminate\Contracts\Filesystem\LockTimeoutException
155
     */
156
    public function getExclusiveLock($block = false)
157
    {
158
        if (! flock($this->handle, LOCK_EX | ($block ? 0 : LOCK_NB))) {
159
            throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}].");
160
        }
161
 
162
        $this->isLocked = true;
163
 
164
        return $this;
165
    }
166
 
167
    /**
168
     * Release the lock on the file.
169
     *
170
     * @return $this
171
     */
172
    public function releaseLock()
173
    {
174
        flock($this->handle, LOCK_UN);
175
 
176
        $this->isLocked = false;
177
 
178
        return $this;
179
    }
180
 
181
    /**
182
     * Close the file.
183
     *
184
     * @return bool
185
     */
186
    public function close()
187
    {
188
        if ($this->isLocked) {
189
            $this->releaseLock();
190
        }
191
 
192
        return fclose($this->handle);
193
    }
194
}