diff --git a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php index d913573..7ce0212 100644 --- a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php +++ b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php @@ -1,8 +1,6 @@ 'info', 'info' => 'info', 'notice' => 'info', @@ -32,7 +30,7 @@ class LaravelLogViewer 'failed' => 'warning', ]; - private static $levels_imgs = [ + private $levels_imgs = [ 'debug' => 'info-circle', 'info' => 'info-circle', 'notice' => 'info-circle', @@ -49,7 +47,7 @@ class LaravelLogViewer * Log levels that are used * @var array */ - private static $log_levels = [ + private $log_levels = [ 'emergency', 'alert', 'critical', @@ -67,24 +65,25 @@ class LaravelLogViewer /** * @param string $folder */ - public static function setFolder($folder) + public function setFolder($folder) { $logsPath = storage_path('logs') . '/' . $folder; if (app('files')->exists($logsPath)) { - self::$folder = $folder; + $this->folder = $folder; } } /** * @param string $file + * @throws \Exception */ - public static function setFile($file) + public function setFile($file) { - $file = self::pathToLogFile($file); + $file = $this->pathToLogFile($file); if (app('files')->exists($file)) { - self::$file = $file; + $this->file = $file; } } @@ -93,10 +92,10 @@ public static function setFile($file) * @return string * @throws \Exception */ - public static function pathToLogFile($file) + public function pathToLogFile($file) { $logsPath = storage_path('logs'); - $logsPath .= (self::$folder) ? '/' . self::$folder : '' ; + $logsPath .= ($this->folder) ? '/' . $this->folder : '' ; if (app('files')->exists($file)) { // try the absolute path return $file; @@ -115,39 +114,39 @@ public static function pathToLogFile($file) /** * @return string */ - public static function getFolderName() + public function getFolderName() { - return self::$folder; + return $this->folder; } /** * @return string */ - public static function getFileName() + public function getFileName() { - return basename(self::$file); + return basename($this->file); } /** * @return array */ - public static function all() + public function all() { $log = array(); $pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/'; - if (!self::$file) { - $log_file = (!self::$folder) ? self::getFiles() : self::getFolderFiles(); + if (!$this->file) { + $log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles(); if(!count($log_file)) { return []; } - self::$file = $log_file[0]; + $this->file = $log_file[0]; } - if (app('files')->size(self::$file) > self::MAX_FILE_SIZE) return null; + if (app('files')->size($this->file) > self::MAX_FILE_SIZE) return null; - $file = app('files')->get(self::$file); + $file = app('files')->get($this->file); preg_match_all($pattern, $file, $headings); @@ -163,7 +162,7 @@ public static function all() foreach ($headings as $h) { for ($i=0, $j = count($h); $i < $j; $i++) { - foreach (self::$log_levels as $level) { + foreach ($this->log_levels 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); @@ -172,8 +171,8 @@ public static function all() $log[] = array( 'context' => $current[3], 'level' => $level, - 'level_class' => self::$levels_classes[$level], - 'level_img' => self::$levels_imgs[$level], + 'level_class' => $this->levels_classes[$level], + 'level_img' => $this->levels_imgs[$level], 'date' => $current[1], 'text' => $current[4], 'in_file' => isset($current[5]) ? $current[5] : null, @@ -209,7 +208,7 @@ public static function all() /** * @return array */ - public static function getFolders() + public function getFolders() { $folders = glob(storage_path() . '/logs/*', GLOB_ONLYDIR); if (is_array($folders)) { @@ -224,16 +223,17 @@ public static function getFolders() * @param bool $basename * @return array */ - public static function getFolderFiles($basename = false) + public function getFolderFiles($basename = false) { - return self::getFiles($basename, self::$folder); + return $this->getFiles($basename, $this->folder); } /** * @param bool $basename + * @param string $folder * @return array */ - public static function getFiles($basename = false, $folder = '') + public function getFiles($basename = false, $folder = '') { $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log'; $files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0); diff --git a/src/controllers/LogViewerController.php b/src/controllers/LogViewerController.php index ac10ea5..ecffa1e 100644 --- a/src/controllers/LogViewerController.php +++ b/src/controllers/LogViewerController.php @@ -14,13 +14,21 @@ class BaseController extends \Laravel\Lumen\Routing\Controller {} */ class LogViewerController extends BaseController { + /** + * @var + */ protected $request; + /** + * @var LaravelLogViewer + */ + private $log_viewer; /** * LogViewerController constructor. */ public function __construct () { + $this->log_viewer = new LaravelLogViewer(); $this->request = app('request'); } @@ -32,11 +40,11 @@ public function index() { $folderFiles = []; if ($this->request->input('f')) { - LaravelLogViewer::setFolder(Crypt::decrypt($this->request->input('f'))); - $folderFiles = LaravelLogViewer::getFolderFiles(true); + $this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f'))); + $folderFiles = $this->log_viewer->getFolderFiles(true); } if ($this->request->input('l')) { - LaravelLogViewer::setFile(Crypt::decrypt($this->request->input('l'))); + $this->log_viewer->setFile(Crypt::decrypt($this->request->input('l'))); } if ($early_return = $this->earlyReturn()) { @@ -44,12 +52,12 @@ public function index() } $data = [ - 'logs' => LaravelLogViewer::all(), - 'folders' => LaravelLogViewer::getFolders(), - 'current_folder' => LaravelLogViewer::getFolderName(), + 'logs' => $this->log_viewer->all(), + 'folders' => $this->log_viewer->getFolders(), + 'current_folder' => $this->log_viewer->getFolderName(), 'folder_files' => $folderFiles, - 'files' => LaravelLogViewer::getFiles(true), - 'current_file' => LaravelLogViewer::getFileName(), + 'files' => $this->log_viewer->getFiles(true), + 'current_file' => $this->log_viewer->getFileName(), 'standardFormat' => true, ]; @@ -80,8 +88,8 @@ private function earlyReturn() app('files')->delete($this->pathFromInput('del')); return $this->redirect($this->request->url()); } elseif ($this->request->has('delall')) { - foreach(LaravelLogViewer::getFiles(true) as $file){ - app('files')->delete(LaravelLogViewer::pathToLogFile($file)); + foreach($this->log_viewer->getFiles(true) as $file){ + app('files')->delete($this->log_viewer->pathToLogFile($file)); } return $this->redirect($this->request->url()); } @@ -95,7 +103,7 @@ private function earlyReturn() */ private function pathFromInput($input_string) { - return LaravelLogViewer::pathToLogFile(Crypt::decrypt($this->request->input($input_string))); + return $this->log_viewer->pathToLogFile(Crypt::decrypt($this->request->input($input_string))); } /**