1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cion\LaravelLogReader\Reader; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Cion\LaravelLogReader\Reader\Exception\FolderNotFoundException; |
7
|
|
|
|
8
|
|
|
class LogReader |
9
|
|
|
{ |
10
|
|
|
protected $filesystem; |
11
|
|
|
|
12
|
|
|
protected $files = []; |
13
|
|
|
|
14
|
|
|
protected $time; |
15
|
|
|
|
16
|
|
|
public $loggers = []; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->filesystem = new Filesystem; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function read() |
24
|
|
|
{ |
25
|
|
|
$this->getLogFiles()->each(function ($file) { |
26
|
|
|
$this->getFileLines($file)->each(function ($line) { |
27
|
|
|
$this->handleFileLine($line); |
28
|
|
|
}); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function handleFileLine($line) |
35
|
|
|
{ |
36
|
|
|
if ($lineHandler = (new LineReader)->read($line)) { |
37
|
|
|
$this->loggers[] = $lineHandler->getArray(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function toArray() |
42
|
|
|
{ |
43
|
|
|
return array_reverse($this->loggers); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function toDatabase() |
47
|
|
|
{ |
48
|
|
|
// specify table or model |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getLogFiles() |
52
|
|
|
{ |
53
|
|
|
if (! $this->filesystem->exists($this->getPath())) { |
54
|
|
|
throw new FolderNotFoundException(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->getDiretoryFiles()->getSubDirectoriesFiles()->getFiles(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getFiles() |
61
|
|
|
{ |
62
|
|
|
return collect($this->files)->filter(function ($file) { |
63
|
|
|
if (! is_null($this->getTime())) { |
64
|
|
|
return $file->getFilename() === "laravel-{$this->getTime()}.log"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getSubDirectoriesFiles() |
72
|
|
|
{ |
73
|
|
|
collect($this->filesystem->directories($this->getPath()))->map(function ($directory) { |
74
|
|
|
collect($this->filesystem->files($directory))->filter(function ($file) { |
75
|
|
|
$this->files[] = $file; |
76
|
|
|
}); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getDiretoryFiles() |
83
|
|
|
{ |
84
|
|
|
collect($this->filesystem->files($this->getPath()))->filter(function ($file) { |
85
|
|
|
$this->files[] = $file; |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getFileLines($file) |
92
|
|
|
{ |
93
|
|
|
return collect(explode("\n", $this->filesystem->get($file->getPathname()))); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setTime($time) |
97
|
|
|
{ |
98
|
|
|
switch ($time) { |
99
|
|
|
case 'yesterday': |
100
|
|
|
return $this->yesterday(); |
101
|
|
|
break; |
|
|
|
|
102
|
|
|
case 'all': |
103
|
|
|
return $this->all(); |
104
|
|
|
break; |
105
|
|
|
default: |
106
|
|
|
return $this->today(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function all() |
111
|
|
|
{ |
112
|
|
|
$this->time = null; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function today() |
118
|
|
|
{ |
119
|
|
|
$this->time = now(); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function yesterday() |
125
|
|
|
{ |
126
|
|
|
$this->time = now()->yesterday(); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function getTime() |
132
|
|
|
{ |
133
|
|
|
if (is_null($this->time)) { |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->time->format('Y-m-d'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getPath() |
141
|
|
|
{ |
142
|
|
|
return config('logreader.path', storage_path('logs')); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.