1
|
|
|
<?php namespace Arcanedev\LogViewer\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LogViewer\Contracts\Utilities\Filesystem as FilesystemContract; |
4
|
|
|
use Arcanedev\LogViewer\Exceptions\LogNotFoundException; |
5
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
6
|
|
|
use Illuminate\Support\LazyCollection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class LogCollection |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\LogViewer\Entities |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class LogCollection extends LazyCollection |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** @var \Arcanedev\LogViewer\Contracts\Utilities\Filesystem */ |
22
|
|
|
private $filesystem; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Constructor |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* LogCollection constructor. |
31
|
|
|
* |
32
|
|
|
* @param mixed $source |
33
|
|
|
*/ |
34
|
114 |
|
public function __construct($source = null) |
35
|
|
|
{ |
36
|
114 |
|
$this->setFilesystem(app(FilesystemContract::class)); |
37
|
|
|
|
38
|
114 |
|
if (is_null($source)) |
39
|
|
|
$source = function () { |
40
|
112 |
|
foreach($this->filesystem->dates(true) as $date => $path) { |
41
|
112 |
|
yield $date => Log::make($date, $path, $this->filesystem->read($date)); |
42
|
|
|
} |
43
|
114 |
|
}; |
44
|
|
|
|
45
|
114 |
|
parent::__construct($source); |
46
|
114 |
|
} |
47
|
|
|
|
48
|
|
|
/* ----------------------------------------------------------------- |
49
|
|
|
| Getters & Setters |
50
|
|
|
| ----------------------------------------------------------------- |
51
|
|
|
*/ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set the filesystem instance. |
55
|
|
|
* |
56
|
|
|
* @param \Arcanedev\LogViewer\Contracts\Utilities\Filesystem $filesystem |
57
|
|
|
* |
58
|
|
|
* @return \Arcanedev\LogViewer\Entities\LogCollection |
59
|
|
|
*/ |
60
|
114 |
|
public function setFilesystem(FilesystemContract $filesystem) |
61
|
|
|
{ |
62
|
114 |
|
$this->filesystem = $filesystem; |
63
|
|
|
|
64
|
114 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/* ----------------------------------------------------------------- |
68
|
|
|
| Main Methods |
69
|
|
|
| ----------------------------------------------------------------- |
70
|
|
|
*/ |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get a log. |
74
|
|
|
* |
75
|
|
|
* @param string $date |
76
|
|
|
* @param mixed|null $default |
77
|
|
|
* |
78
|
|
|
* @return \Arcanedev\LogViewer\Entities\Log |
79
|
|
|
* |
80
|
|
|
* @throws \Arcanedev\LogViewer\Exceptions\LogNotFoundException |
81
|
|
|
*/ |
82
|
30 |
|
public function get($date, $default = null) |
83
|
|
|
{ |
84
|
30 |
|
if ( ! $this->has($date)) |
85
|
8 |
|
throw new LogNotFoundException("Log not found in this date [$date]"); |
86
|
|
|
|
87
|
22 |
|
return parent::get($date, $default); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Paginate logs. |
92
|
|
|
* |
93
|
|
|
* @param int $perPage |
94
|
|
|
* |
95
|
|
|
* @return \Illuminate\Pagination\LengthAwarePaginator |
96
|
|
|
*/ |
97
|
4 |
|
public function paginate($perPage = 30) |
98
|
|
|
{ |
99
|
4 |
|
$page = request()->get('page', 1); |
100
|
4 |
|
$path = request()->url(); |
101
|
|
|
|
102
|
4 |
|
return new LengthAwarePaginator( |
103
|
4 |
|
$this->forPage($page, $perPage), |
104
|
4 |
|
$this->count(), |
105
|
4 |
|
$perPage, |
106
|
4 |
|
$page, |
107
|
4 |
|
compact('path') |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get a log (alias). |
113
|
|
|
* |
114
|
|
|
* @see get() |
115
|
|
|
* |
116
|
|
|
* @param string $date |
117
|
|
|
* |
118
|
|
|
* @return \Arcanedev\LogViewer\Entities\Log |
119
|
|
|
*/ |
120
|
14 |
|
public function log($date) |
121
|
|
|
{ |
122
|
14 |
|
return $this->get($date); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get log entries. |
128
|
|
|
* |
129
|
|
|
* @param string $date |
130
|
|
|
* @param string $level |
131
|
|
|
* |
132
|
|
|
* @return \Arcanedev\LogViewer\Entities\LogEntryCollection |
133
|
|
|
*/ |
134
|
14 |
|
public function entries($date, $level = 'all') |
135
|
|
|
{ |
136
|
14 |
|
return $this->get($date)->entries($level); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get logs statistics. |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
30 |
|
public function stats() |
145
|
|
|
{ |
146
|
30 |
|
$stats = []; |
147
|
|
|
|
148
|
30 |
|
foreach ($this->all() as $date => $log) { |
149
|
|
|
/** @var \Arcanedev\LogViewer\Entities\Log $log */ |
150
|
30 |
|
$stats[$date] = $log->stats(); |
151
|
|
|
} |
152
|
|
|
|
153
|
30 |
|
return $stats; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* List the log files (dates). |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
8 |
|
public function dates() |
162
|
|
|
{ |
163
|
8 |
|
return $this->keys()->toArray(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get entries total. |
168
|
|
|
* |
169
|
|
|
* @param string $level |
170
|
|
|
* |
171
|
|
|
* @return int |
172
|
|
|
*/ |
173
|
10 |
|
public function total($level = 'all') |
174
|
|
|
{ |
175
|
|
|
return (int) $this->sum(function (Log $log) use ($level) { |
176
|
10 |
|
return $log->entries($level)->count(); |
177
|
10 |
|
}); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get logs tree. |
182
|
|
|
* |
183
|
|
|
* @param bool $trans |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
8 |
|
public function tree($trans = false) |
188
|
|
|
{ |
189
|
8 |
|
$tree = []; |
190
|
|
|
|
191
|
8 |
|
foreach ($this->all() as $date => $log) { |
192
|
|
|
/** @var \Arcanedev\LogViewer\Entities\Log $log */ |
193
|
8 |
|
$tree[$date] = $log->tree($trans); |
194
|
|
|
} |
195
|
|
|
|
196
|
8 |
|
return $tree; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get logs menu. |
201
|
|
|
* |
202
|
|
|
* @param bool $trans |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
8 |
|
public function menu($trans = true) |
207
|
|
|
{ |
208
|
8 |
|
$menu = []; |
209
|
|
|
|
210
|
8 |
|
foreach ($this->all() as $date => $log) { |
211
|
|
|
/** @var \Arcanedev\LogViewer\Entities\Log $log */ |
212
|
8 |
|
$menu[$date] = $log->menu($trans); |
213
|
|
|
} |
214
|
|
|
|
215
|
8 |
|
return $menu; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|