From b0a7055fcca10eb1b6981b4d9828a0eb050b7f52 Mon Sep 17 00:00:00 2001 From: Elminson De Oleo Baez Date: Tue, 17 Jul 2018 11:23:06 -0400 Subject: [PATCH] patterns for preg_match* (#156) * Creating a array that content all the patterns used in the class, this allow if in the feature we want to create a new featured that include patterns we cant add this for a best readeability (some patterns get really complicated * Modifying all the sugestion from rap2hpoutre in the PR comments --- .../LaravelLogViewer/LaravelLogViewer.php | 18 ++++++---- src/Rap2hpoutre/LaravelLogViewer/Pattern.php | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/Rap2hpoutre/LaravelLogViewer/Pattern.php diff --git a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php index fa6c37c..03fbcfc 100644 --- a/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php +++ b/src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php @@ -28,12 +28,18 @@ class LaravelLogViewer */ private $level; + /** + * @var Pattern pattern + */ + private $pattern; + /** * LaravelLogViewer constructor. */ public function __construct() { $this->level = new Level(); + $this->pattern = new Pattern(); } /** @@ -108,8 +114,6 @@ public function all() { $log = array(); - $pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/'; - if (!$this->file) { $log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles(); if (!count($log_file)) { @@ -122,13 +126,13 @@ public function all() $file = app('files')->get($this->file); - preg_match_all($pattern, $file, $headings); + preg_match_all($this->pattern['logs'], $file, $headings); if (!is_array($headings)) { return $log; } - $log_data = preg_split($pattern, $file); + $log_data = preg_split($this->pattern['logs'], $file); if ($log_data[0] < 1) { array_shift($log_data); @@ -139,7 +143,7 @@ public function all() 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); + preg_match($this->pattern['current_log'][0] . $level . $this->pattern['current_log'][1], $h[$i], $current); if (!isset($current[4])) continue; $log[] = array( @@ -210,7 +214,7 @@ public function getFolderFiles($basename = false) 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); + $files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match($this->pattern['files'], $pattern) ? GLOB_BRACE : 0); $files = array_reverse($files); $files = array_filter($files, 'is_file'); if ($basename && is_array($files)) { @@ -220,4 +224,4 @@ public function getFiles($basename = false, $folder = '') } return array_values($files); } -} +} \ No newline at end of file diff --git a/src/Rap2hpoutre/LaravelLogViewer/Pattern.php b/src/Rap2hpoutre/LaravelLogViewer/Pattern.php new file mode 100644 index 0000000..1fbda50 --- /dev/null +++ b/src/Rap2hpoutre/LaravelLogViewer/Pattern.php @@ -0,0 +1,34 @@ + '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/', + 'current_log' => [ + '/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)', + ': (.*?)( in .*?:[0-9]+)?$/i' + ], + 'files' => '/\{.*?\,.*?\}/i', + ]; + + /** + * @return array + */ + public function all() + { + return array_keys($this->patterns); + } + +} \ No newline at end of file