1
|
|
|
<?php namespace Arcanedev\LogViewer\Utilities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LogViewer\Contracts\Utilities\Filesystem as FilesystemContract; |
4
|
|
|
use Arcanedev\LogViewer\Exceptions\FilesystemException; |
5
|
|
|
use Illuminate\Filesystem\Filesystem as IlluminateFilesystem; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Filesystem |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\LogViewer\Utilities |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Filesystem implements FilesystemContract |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The filesystem instance. |
22
|
|
|
* |
23
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
24
|
|
|
*/ |
25
|
|
|
protected $filesystem; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The base storage path. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $storagePath; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The log files prefix pattern. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $prefixPattern; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The log files date pattern. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $datePattern; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The log files extension. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $extension; |
54
|
|
|
|
55
|
|
|
/* ----------------------------------------------------------------- |
56
|
|
|
| Constructor |
57
|
|
|
| ----------------------------------------------------------------- |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Filesystem constructor. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
64
|
|
|
* @param string $storagePath |
65
|
|
|
*/ |
66
|
369 |
|
public function __construct(IlluminateFilesystem $files, $storagePath) |
67
|
|
|
{ |
68
|
369 |
|
$this->filesystem = $files; |
69
|
369 |
|
$this->setPath($storagePath); |
70
|
369 |
|
$this->setPattern(); |
71
|
369 |
|
} |
72
|
|
|
|
73
|
|
|
/* ----------------------------------------------------------------- |
74
|
|
|
| Getters & Setters |
75
|
|
|
| ----------------------------------------------------------------- |
76
|
|
|
*/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the files instance. |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\Filesystem\Filesystem |
82
|
|
|
*/ |
83
|
3 |
|
public function getInstance() |
84
|
|
|
{ |
85
|
3 |
|
return $this->filesystem; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the log storage path. |
90
|
|
|
* |
91
|
|
|
* @param string $storagePath |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
369 |
|
public function setPath($storagePath) |
96
|
|
|
{ |
97
|
369 |
|
$this->storagePath = $storagePath; |
98
|
|
|
|
99
|
369 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the log pattern. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
213 |
|
public function getPattern() |
108
|
|
|
{ |
109
|
213 |
|
return $this->prefixPattern.$this->datePattern.$this->extension; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set the log pattern. |
114
|
|
|
* |
115
|
|
|
* @param string $date |
116
|
|
|
* @param string $prefix |
117
|
|
|
* @param string $extension |
118
|
|
|
* |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
369 |
|
public function setPattern( |
122
|
|
|
$prefix = self::PATTERN_PREFIX, |
123
|
|
|
$date = self::PATTERN_DATE, |
124
|
|
|
$extension = self::PATTERN_EXTENSION |
125
|
|
|
) { |
126
|
369 |
|
$this->setPrefixPattern($prefix); |
127
|
369 |
|
$this->setDatePattern($date); |
128
|
369 |
|
$this->setExtension($extension); |
129
|
|
|
|
130
|
369 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the log date pattern. |
135
|
|
|
* |
136
|
|
|
* @param string $datePattern |
137
|
|
|
* |
138
|
|
|
* @return self |
139
|
|
|
*/ |
140
|
369 |
|
public function setDatePattern($datePattern) |
141
|
|
|
{ |
142
|
369 |
|
$this->datePattern = $datePattern; |
143
|
|
|
|
144
|
369 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the log prefix pattern. |
149
|
|
|
* |
150
|
|
|
* @param string $prefixPattern |
151
|
|
|
* |
152
|
|
|
* @return self |
153
|
|
|
*/ |
154
|
369 |
|
public function setPrefixPattern($prefixPattern) |
155
|
|
|
{ |
156
|
369 |
|
$this->prefixPattern = $prefixPattern; |
157
|
|
|
|
158
|
369 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set the log extension. |
163
|
|
|
* |
164
|
|
|
* @param string $extension |
165
|
|
|
* |
166
|
|
|
* @return self |
167
|
|
|
*/ |
168
|
369 |
|
public function setExtension($extension) |
169
|
|
|
{ |
170
|
369 |
|
$this->extension = $extension; |
171
|
|
|
|
172
|
369 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/* ----------------------------------------------------------------- |
176
|
|
|
| Main Methods |
177
|
|
|
| ----------------------------------------------------------------- |
178
|
|
|
*/ |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get all log files. |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
21 |
|
public function all() |
186
|
|
|
{ |
187
|
21 |
|
return $this->getFiles('*'.$this->extension); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get all valid log files. |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
204 |
|
public function logs() |
196
|
|
|
{ |
197
|
204 |
|
return $this->getFiles($this->getPattern()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* List the log files (Only dates). |
202
|
|
|
* |
203
|
|
|
* @param bool $withPaths |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
189 |
|
public function dates($withPaths = false) |
208
|
|
|
{ |
209
|
189 |
|
$files = array_reverse($this->logs()); |
210
|
189 |
|
$dates = $this->extractDates($files); |
211
|
|
|
|
212
|
189 |
|
if ($withPaths) { |
213
|
174 |
|
$dates = array_combine($dates, $files); // [date => file] |
214
|
|
|
} |
215
|
|
|
|
216
|
189 |
|
return $dates; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Read the log. |
221
|
|
|
* |
222
|
|
|
* @param string $date |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
* |
226
|
|
|
* @throws \Arcanedev\LogViewer\Exceptions\FilesystemException |
227
|
|
|
*/ |
228
|
252 |
|
public function read($date) |
229
|
|
|
{ |
230
|
|
|
try { |
231
|
252 |
|
$log = $this->filesystem->get( |
232
|
252 |
|
$this->getLogPath($date) |
233
|
|
|
); |
234
|
|
|
} |
235
|
3 |
|
catch (\Exception $e) { |
236
|
3 |
|
throw new FilesystemException($e->getMessage()); |
237
|
|
|
} |
238
|
|
|
|
239
|
249 |
|
return $log; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Delete the log. |
244
|
|
|
* |
245
|
|
|
* @param string $date |
246
|
|
|
* |
247
|
|
|
* @return bool |
248
|
|
|
* |
249
|
|
|
* @throws \Arcanedev\LogViewer\Exceptions\FilesystemException |
250
|
|
|
*/ |
251
|
15 |
|
public function delete($date) |
252
|
|
|
{ |
253
|
15 |
|
$path = $this->getLogPath($date); |
254
|
|
|
|
255
|
|
|
// @codeCoverageIgnoreStart |
256
|
|
|
if ( ! $this->filesystem->delete($path)) { |
257
|
|
|
throw new FilesystemException('There was an error deleting the log.'); |
258
|
|
|
} |
259
|
|
|
// @codeCoverageIgnoreEnd |
260
|
|
|
|
261
|
9 |
|
return true; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Clear the log files. |
266
|
|
|
* |
267
|
|
|
* @return bool |
268
|
|
|
*/ |
269
|
3 |
|
public function clear() |
270
|
|
|
{ |
271
|
3 |
|
return $this->filesystem->delete( |
272
|
3 |
|
$this->logs() |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get the log file path. |
278
|
|
|
* |
279
|
|
|
* @param string $date |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
69 |
|
public function path($date) |
284
|
|
|
{ |
285
|
69 |
|
return $this->getLogPath($date); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/* ----------------------------------------------------------------- |
289
|
|
|
| Other Methods |
290
|
|
|
| ----------------------------------------------------------------- |
291
|
|
|
*/ |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get all files. |
295
|
|
|
* |
296
|
|
|
* @param string $pattern |
297
|
|
|
* |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
225 |
|
private function getFiles($pattern) |
301
|
|
|
{ |
302
|
225 |
|
$files = $this->filesystem->glob( |
303
|
225 |
|
$this->storagePath.DS.$pattern, GLOB_BRACE |
304
|
|
|
); |
305
|
|
|
|
306
|
225 |
|
return array_filter(array_map('realpath', $files)); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Get the log file path. |
311
|
|
|
* |
312
|
|
|
* @param string $date |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
* |
316
|
|
|
* @throws \Arcanedev\LogViewer\Exceptions\FilesystemException |
317
|
|
|
*/ |
318
|
270 |
|
private function getLogPath($date) |
319
|
|
|
{ |
320
|
270 |
|
$path = $this->storagePath.DS.$this->prefixPattern.$date.$this->extension; |
321
|
|
|
|
322
|
270 |
|
if ( ! $this->filesystem->exists($path)) { |
323
|
9 |
|
throw new FilesystemException("The log(s) could not be located at : $path"); |
324
|
|
|
} |
325
|
|
|
|
326
|
261 |
|
return realpath($path); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Extract dates from files. |
331
|
|
|
* |
332
|
|
|
* @param array $files |
333
|
|
|
* |
334
|
|
|
* @return array |
335
|
|
|
*/ |
336
|
189 |
|
private function extractDates(array $files) |
337
|
|
|
{ |
338
|
|
|
return array_map(function ($file) { |
339
|
189 |
|
return extract_date(basename($file)); |
340
|
189 |
|
}, $files); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|