1 | <?php namespace Arcanedev\LogViewer\Utilities; |
||
15 | class LogChecker implements LogCheckerContract |
||
16 | { |
||
17 | /* ------------------------------------------------------------------------------------------------ |
||
18 | | Properties |
||
19 | | ------------------------------------------------------------------------------------------------ |
||
20 | */ |
||
21 | /** |
||
22 | * The config repository instance. |
||
23 | * |
||
24 | * @var \Illuminate\Contracts\Config\Repository |
||
25 | */ |
||
26 | private $config; |
||
27 | |||
28 | /** |
||
29 | * The filesystem instance. |
||
30 | * |
||
31 | * @var \Arcanedev\LogViewer\Contracts\Utilities\Filesystem |
||
32 | */ |
||
33 | private $filesystem; |
||
34 | |||
35 | /** |
||
36 | * Log handler mode. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $handler = ''; |
||
41 | |||
42 | /** |
||
43 | * The check status. |
||
44 | * |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $status = true; |
||
48 | |||
49 | /** |
||
50 | * The check messages. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | private $messages; |
||
55 | |||
56 | /** |
||
57 | * Log files statuses. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $files; |
||
62 | |||
63 | /* ------------------------------------------------------------------------------------------------ |
||
64 | | Constructor |
||
65 | | ------------------------------------------------------------------------------------------------ |
||
66 | */ |
||
67 | /** |
||
68 | * LogChecker constructor. |
||
69 | * |
||
70 | * @param \Illuminate\Contracts\Config\Repository $config |
||
71 | * @param \Arcanedev\LogViewer\Contracts\Utilities\Filesystem $filesystem |
||
72 | */ |
||
73 | 48 | public function __construct(ConfigContract $config, FilesystemContract $filesystem) |
|
74 | { |
||
75 | 48 | $this->files = []; |
|
76 | 48 | $this->setConfig($config); |
|
77 | 48 | $this->setFilesystem($filesystem); |
|
78 | 48 | $this->refresh(); |
|
79 | 48 | } |
|
80 | |||
81 | /* ------------------------------------------------------------------------------------------------ |
||
82 | | Getters & Setters |
||
83 | | ------------------------------------------------------------------------------------------------ |
||
84 | */ |
||
85 | /** |
||
86 | * Set the config instance. |
||
87 | * |
||
88 | * @param \Illuminate\Contracts\Config\Repository $config |
||
89 | * |
||
90 | * @return \Arcanedev\LogViewer\Utilities\LogChecker |
||
91 | */ |
||
92 | 48 | public function setConfig(ConfigContract $config) |
|
93 | { |
||
94 | 48 | $this->config = $config; |
|
95 | |||
96 | 48 | return $this; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Set the Filesystem instance. |
||
101 | * |
||
102 | * @param \Arcanedev\LogViewer\Contracts\Utilities\Filesystem $filesystem |
||
103 | * |
||
104 | * @return \Arcanedev\LogViewer\Utilities\LogChecker |
||
105 | */ |
||
106 | 48 | public function setFilesystem(FilesystemContract $filesystem) |
|
112 | |||
113 | /** |
||
114 | * Set the log handler mode. |
||
115 | * |
||
116 | * @param string $handler |
||
117 | * |
||
118 | * @return \Arcanedev\LogViewer\Utilities\LogChecker |
||
119 | */ |
||
120 | 48 | protected function setHandler($handler) |
|
126 | |||
127 | /* ------------------------------------------------------------------------------------------------ |
||
128 | | Main Functions |
||
129 | | ------------------------------------------------------------------------------------------------ |
||
130 | */ |
||
131 | /** |
||
132 | * Get messages. |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 16 | public function messages() |
|
137 | { |
||
138 | 16 | $this->refresh(); |
|
139 | |||
140 | 16 | return $this->messages; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Check if the checker passes. |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | 8 | public function passes() |
|
149 | { |
||
150 | 8 | $this->refresh(); |
|
151 | |||
152 | 8 | return $this->status; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Check if the checker fails. |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | 8 | public function fails() |
|
161 | { |
||
162 | 8 | return ! $this->passes(); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get the requirements. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | 24 | public function requirements() |
|
171 | { |
||
172 | 24 | $this->refresh(); |
|
173 | |||
174 | 24 | return $this->isDaily() ? [ |
|
175 | 16 | 'status' => 'success', |
|
176 | 8 | 'header' => 'Application requirements fulfilled.', |
|
177 | 8 | 'message' => 'Are you ready to rock ?', |
|
178 | 8 | ] : [ |
|
179 | 8 | 'status' => 'failed', |
|
180 | 8 | 'header' => 'Application requirements failed.', |
|
181 | 16 | 'message' => $this->messages['handler'] |
|
182 | 12 | ]; |
|
183 | } |
||
184 | |||
185 | /* ------------------------------------------------------------------------------------------------ |
||
186 | | Check Functions |
||
187 | | ------------------------------------------------------------------------------------------------ |
||
188 | */ |
||
189 | /** |
||
190 | * Is a daily handler mode ? |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | 48 | protected function isDaily() |
|
195 | { |
||
196 | 48 | return $this->isSameHandler(self::HANDLER_DAILY); |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Is the handler is the same as the application log handler. |
||
201 | * |
||
202 | * @param string $handler |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | 48 | private function isSameHandler($handler) |
|
207 | { |
||
208 | 48 | return $this->handler === $handler; |
|
209 | } |
||
210 | |||
211 | /* ------------------------------------------------------------------------------------------------ |
||
212 | | Other Functions |
||
213 | | ------------------------------------------------------------------------------------------------ |
||
214 | */ |
||
215 | /** |
||
216 | * Refresh the checks. |
||
217 | * |
||
218 | * @return \Arcanedev\LogViewer\Utilities\LogChecker |
||
219 | */ |
||
220 | 48 | private function refresh() |
|
221 | { |
||
222 | 48 | $this->setHandler($this->config->get('app.log', 'single')); |
|
223 | |||
224 | 48 | $this->messages = [ |
|
225 | 24 | 'handler' => '', |
|
226 | 24 | 'files' => [], |
|
227 | ]; |
||
228 | 48 | $this->files = []; |
|
229 | |||
230 | 48 | $this->checkHandler(); |
|
231 | 48 | $this->checkLogFiles(); |
|
232 | |||
233 | 48 | return $this; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Check the handler mode. |
||
238 | */ |
||
239 | 48 | private function checkHandler() |
|
245 | |||
246 | /** |
||
247 | * Check all log files. |
||
248 | */ |
||
249 | 48 | private function checkLogFiles() |
|
250 | { |
||
251 | 48 | foreach ($this->filesystem->all() as $path) { |
|
252 | 48 | $this->checkLogFile($path); |
|
253 | 24 | } |
|
254 | 48 | } |
|
255 | |||
256 | /** |
||
257 | * Check a log file. |
||
258 | * |
||
259 | * @param string $path |
||
260 | */ |
||
261 | 48 | private function checkLogFile($path) |
|
262 | { |
||
263 | 48 | $status = true; |
|
264 | 48 | $file = basename($path); |
|
265 | 48 | $message = "The log file [$file] is valid."; |
|
266 | |||
267 | 48 | if ($this->isSingleLogFile($file)) { |
|
268 | 48 | $this->status = $status = false; |
|
269 | 48 | $this->messages['files'][$file] = $message = |
|
270 | 48 | "You have a single log file in your application, you should split the [$file] into seperate log files."; |
|
271 | 24 | } |
|
272 | 48 | elseif ($this->isInvalidLogDate($file)) { |
|
273 | 48 | $this->status = $status = false; |
|
274 | 48 | $this->messages['files'][$file] = $message = |
|
275 | 48 | "The log file [$file] has an invalid date, the format must be like laravel-YYYY-MM-DD.log."; |
|
276 | 24 | } |
|
277 | |||
278 | 48 | $this->files[$file] = compact('filename', 'status', 'message', 'path'); |
|
279 | 48 | } |
|
280 | |||
281 | /** |
||
282 | * Check if it's not a single log file. |
||
283 | * |
||
284 | * @param string $file |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | 48 | private function isSingleLogFile($file) |
|
289 | { |
||
290 | 48 | return $file === 'laravel.log'; |
|
291 | } |
||
292 | |||
293 | /** |
||
294 | * Check the date of the log file. |
||
295 | * |
||
296 | * @param string $file |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | 48 | private function isInvalidLogDate($file) |
|
308 | } |
||
309 |