1 | <?php namespace Arcanedev\LogViewer\Utilities; |
||
15 | class LogChecker implements LogCheckerContract |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Properties |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** |
||
23 | * The config repository instance. |
||
24 | * |
||
25 | * @var \Illuminate\Contracts\Config\Repository |
||
26 | */ |
||
27 | private $config; |
||
28 | |||
29 | /** |
||
30 | * The filesystem instance. |
||
31 | * |
||
32 | * @var \Arcanedev\LogViewer\Contracts\Utilities\Filesystem |
||
33 | */ |
||
34 | private $filesystem; |
||
35 | |||
36 | /** |
||
37 | * Log handler mode. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $handler = ''; |
||
42 | |||
43 | /** |
||
44 | * The check status. |
||
45 | * |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $status = true; |
||
49 | |||
50 | /** |
||
51 | * The check messages. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private $messages; |
||
56 | |||
57 | /** |
||
58 | * Log files statuses. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | private $files = []; |
||
63 | |||
64 | /* ----------------------------------------------------------------- |
||
65 | | Constructor |
||
66 | | ----------------------------------------------------------------- |
||
67 | */ |
||
68 | |||
69 | /** |
||
70 | * LogChecker constructor. |
||
71 | * |
||
72 | * @param \Illuminate\Contracts\Config\Repository $config |
||
73 | * @param \Arcanedev\LogViewer\Contracts\Utilities\Filesystem $filesystem |
||
74 | */ |
||
75 | 12 | public function __construct(ConfigContract $config, FilesystemContract $filesystem) |
|
81 | |||
82 | /* ----------------------------------------------------------------- |
||
83 | | Getters & Setters |
||
84 | | ----------------------------------------------------------------- |
||
85 | */ |
||
86 | |||
87 | /** |
||
88 | * Set the config instance. |
||
89 | * |
||
90 | * @param \Illuminate\Contracts\Config\Repository $config |
||
91 | * |
||
92 | * @return self |
||
93 | */ |
||
94 | 12 | public function setConfig(ConfigContract $config) |
|
100 | |||
101 | /** |
||
102 | * Set the Filesystem instance. |
||
103 | * |
||
104 | * @param \Arcanedev\LogViewer\Contracts\Utilities\Filesystem $filesystem |
||
105 | * |
||
106 | * @return self |
||
107 | */ |
||
108 | 12 | public function setFilesystem(FilesystemContract $filesystem) |
|
114 | |||
115 | /** |
||
116 | * Set the log handler mode. |
||
117 | * |
||
118 | * @param string $handler |
||
119 | * |
||
120 | * @return self |
||
121 | */ |
||
122 | 12 | protected function setHandler($handler) |
|
128 | |||
129 | /* ----------------------------------------------------------------- |
||
130 | | Main Methods |
||
131 | | ----------------------------------------------------------------- |
||
132 | */ |
||
133 | |||
134 | /** |
||
135 | * Get messages. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | 4 | public function messages() |
|
145 | |||
146 | /** |
||
147 | * Check if the checker passes. |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | 2 | public function passes() |
|
157 | |||
158 | /** |
||
159 | * Check if the checker fails. |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | 2 | public function fails() |
|
167 | |||
168 | /** |
||
169 | * Get the requirements. |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | 6 | public function requirements() |
|
187 | |||
188 | /* ----------------------------------------------------------------- |
||
189 | | Check Methods |
||
190 | | ----------------------------------------------------------------- |
||
191 | */ |
||
192 | |||
193 | /** |
||
194 | * Is a daily handler mode ? |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 12 | protected function isDaily() |
|
202 | |||
203 | /** |
||
204 | * Is the handler is the same as the application log handler. |
||
205 | * |
||
206 | * @param string $handler |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | 12 | private function isSameHandler($handler) |
|
214 | |||
215 | /* ----------------------------------------------------------------- |
||
216 | | Other Methods |
||
217 | | ----------------------------------------------------------------- |
||
218 | */ |
||
219 | |||
220 | /** |
||
221 | * Refresh the checks. |
||
222 | * |
||
223 | * @return \Arcanedev\LogViewer\Utilities\LogChecker |
||
224 | */ |
||
225 | 12 | private function refresh() |
|
240 | |||
241 | /** |
||
242 | * Check the handler mode. |
||
243 | */ |
||
244 | 12 | private function checkHandler() |
|
250 | |||
251 | /** |
||
252 | * Check all log files. |
||
253 | */ |
||
254 | 12 | private function checkLogFiles() |
|
260 | |||
261 | /** |
||
262 | * Check a log file. |
||
263 | * |
||
264 | * @param string $path |
||
265 | */ |
||
266 | 12 | private function checkLogFile($path) |
|
267 | { |
||
268 | 12 | $status = true; |
|
269 | 12 | $filename = basename($path); |
|
270 | 12 | $message = "The log file [$filename] is valid."; |
|
271 | $pattern = $this->filesystem->getPattern(); |
||
272 | 12 | ||
273 | 12 | if ($this->isSingleLogFile($filename)) { |
|
274 | 12 | $this->status = $status = false; |
|
275 | 12 | $this->messages['files'][$filename] = $message = |
|
276 | "You have a single log file in your application, you should split the [$filename] into separate log files."; |
||
277 | 12 | } |
|
278 | 12 | elseif ($this->isInvalidLogPattern($filename, $pattern)) { |
|
279 | 12 | $this->status = $status = false; |
|
280 | 12 | $this->messages['files'][$filename] = $message = |
|
281 | "The log file [$filename] has an invalid date, the format must be like {$pattern}."; |
||
282 | } |
||
283 | 12 | ||
284 | 12 | $this->files[$filename] = compact('filename', 'status', 'message', 'path'); |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Check if it's not a single log file. |
||
289 | * |
||
290 | * @param string $file |
||
291 | * |
||
292 | * @return bool |
||
293 | 12 | */ |
|
294 | private function isSingleLogFile($file) |
||
295 | 12 | { |
|
296 | return $file === 'laravel.log'; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Check the date of the log file. |
||
301 | * |
||
302 | * @param string $file |
||
303 | * @param string $pattern |
||
304 | * |
||
305 | 12 | * @return bool |
|
306 | */ |
||
307 | 12 | private function isInvalidLogPattern($file, $pattern) |
|
311 | } |
||
312 |