From a8febf2f666a1057893a62e3b5b4d2bc9009b98e Mon Sep 17 00:00:00 2001 From: rap2h Date: Mon, 16 Jul 2018 11:08:37 +0200 Subject: [PATCH] refactor --- .../LaravelLogViewer/LaravelLogViewer.php | 74 ++++++------------- src/Rap2hpoutre/LaravelLogViewer/Level.php | 68 +++++++++++++++++ src/controllers/LogViewerController.php | 6 +- 3 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 src/Rap2hpoutre/LaravelLogViewer/Level.php diff --git a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php index 7ce0212..fa6c37c 100644 --- a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php +++ b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php @@ -1,4 +1,5 @@ 'info', - 'info' => 'info', - 'notice' => 'info', - 'warning' => 'warning', - 'error' => 'danger', - 'critical' => 'danger', - 'alert' => 'danger', - 'emergency' => 'danger', - 'processed' => 'info', - 'failed' => 'warning', - ]; - - private $levels_imgs = [ - 'debug' => 'info-circle', - 'info' => 'info-circle', - 'notice' => 'info-circle', - 'warning' => 'exclamation-triangle', - 'error' => 'exclamation-triangle', - 'critical' => 'exclamation-triangle', - 'alert' => 'exclamation-triangle', - 'emergency' => 'exclamation-triangle', - 'processed' => 'info-circle', - 'failed' => 'exclamation-triangle' - ]; + /** + * Why? Uh... Sorry + */ + const MAX_FILE_SIZE = 52428800; /** - * Log levels that are used - * @var array + * @var Level level */ - private $log_levels = [ - 'emergency', - 'alert', - 'critical', - 'error', - 'warning', - 'notice', - 'info', - 'debug', - 'processed', - 'failed' - ]; - - const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry + private $level; + + /** + * LaravelLogViewer constructor. + */ + public function __construct() + { + $this->level = new Level(); + } /** * @param string $folder @@ -95,8 +69,8 @@ public function setFile($file) public function pathToLogFile($file) { $logsPath = storage_path('logs'); - $logsPath .= ($this->folder) ? '/' . $this->folder : '' ; - + $logsPath .= ($this->folder) ? '/' . $this->folder : ''; + if (app('files')->exists($file)) { // try the absolute path return $file; } @@ -138,7 +112,7 @@ public function all() if (!$this->file) { $log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles(); - if(!count($log_file)) { + if (!count($log_file)) { return []; } $this->file = $log_file[0]; @@ -161,8 +135,8 @@ public function all() } foreach ($headings as $h) { - for ($i=0, $j = count($h); $i < $j; $i++) { - foreach ($this->log_levels as $level) { + for ($i = 0, $j = count($h); $i < $j; $i++) { + foreach ($this->level->all() as $level) { if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) { preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)' . $level . ': (.*?)( in .*?:[0-9]+)?$/i', $h[$i], $current); @@ -171,8 +145,8 @@ public function all() $log[] = array( 'context' => $current[3], 'level' => $level, - 'level_class' => $this->levels_classes[$level], - 'level_img' => $this->levels_imgs[$level], + 'level_class' => $this->level->cssClass($level), + 'level_img' => $this->level->img($level), 'date' => $current[1], 'text' => $current[4], 'in_file' => isset($current[5]) ? $current[5] : null, @@ -188,13 +162,13 @@ public function all() $lines = explode(PHP_EOL, $file); $log = []; - foreach($lines as $key => $line) { + foreach ($lines as $key => $line) { $log[] = [ 'context' => '', 'level' => '', 'level_class' => '', 'level_img' => '', - 'date' => $key+1, + 'date' => $key + 1, 'text' => $line, 'in_file' => null, 'stack' => '', diff --git a/src/Rap2hpoutre/LaravelLogViewer/Level.php b/src/Rap2hpoutre/LaravelLogViewer/Level.php new file mode 100644 index 0000000..e4bb78a --- /dev/null +++ b/src/Rap2hpoutre/LaravelLogViewer/Level.php @@ -0,0 +1,68 @@ + 'info', + 'info' => 'info', + 'notice' => 'info', + 'warning' => 'warning', + 'error' => 'danger', + 'critical' => 'danger', + 'alert' => 'danger', + 'emergency' => 'danger', + 'processed' => 'info', + 'failed' => 'warning', + ]; + + /** + * @var array + */ + private $levels_imgs = [ + 'debug' => 'info-circle', + 'info' => 'info-circle', + 'notice' => 'info-circle', + 'warning' => 'exclamation-triangle', + 'error' => 'exclamation-triangle', + 'critical' => 'exclamation-triangle', + 'alert' => 'exclamation-triangle', + 'emergency' => 'exclamation-triangle', + 'processed' => 'info-circle', + 'failed' => 'exclamation-triangle' + ]; + + /** + * @return array + */ + public function all() + { + return array_keys($this->levels_imgs); + } + + /** + * @param $level + * @return string + */ + public function img($level) + { + return $this->levels_imgs[$level]; + } + + /** + * @param $level + * @return string + */ + public function cssClass($level) + { + return $this->levels_classes[$level]; + } +} \ No newline at end of file diff --git a/src/controllers/LogViewerController.php b/src/controllers/LogViewerController.php index ecffa1e..26b34a3 100644 --- a/src/controllers/LogViewerController.php +++ b/src/controllers/LogViewerController.php @@ -1,5 +1,7 @@ log_viewer = new LaravelLogViewer(); $this->request = app('request'); @@ -88,7 +90,7 @@ private function earlyReturn() app('files')->delete($this->pathFromInput('del')); return $this->redirect($this->request->url()); } elseif ($this->request->has('delall')) { - foreach($this->log_viewer->getFiles(true) as $file){ + foreach ($this->log_viewer->getFiles(true) as $file) { app('files')->delete($this->log_viewer->pathToLogFile($file)); } return $this->redirect($this->request->url());