Skip to content

Commit b0a7055

Browse files
elminsonrap2hpoutre
authored andcommitted
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
1 parent 4502f4e commit b0a7055

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ class LaravelLogViewer
2828
*/
2929
private $level;
3030

31+
/**
32+
* @var Pattern pattern
33+
*/
34+
private $pattern;
35+
3136
/**
3237
* LaravelLogViewer constructor.
3338
*/
3439
public function __construct()
3540
{
3641
$this->level = new Level();
42+
$this->pattern = new Pattern();
3743
}
3844

3945
/**
@@ -108,8 +114,6 @@ public function all()
108114
{
109115
$log = array();
110116

111-
$pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/';
112-
113117
if (!$this->file) {
114118
$log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles();
115119
if (!count($log_file)) {
@@ -122,13 +126,13 @@ public function all()
122126

123127
$file = app('files')->get($this->file);
124128

125-
preg_match_all($pattern, $file, $headings);
129+
preg_match_all($this->pattern['logs'], $file, $headings);
126130

127131
if (!is_array($headings)) {
128132
return $log;
129133
}
130134

131-
$log_data = preg_split($pattern, $file);
135+
$log_data = preg_split($this->pattern['logs'], $file);
132136

133137
if ($log_data[0] < 1) {
134138
array_shift($log_data);
@@ -139,7 +143,7 @@ public function all()
139143
foreach ($this->level->all() as $level) {
140144
if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
141145

142-
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);
146+
preg_match($this->pattern['current_log'][0] . $level . $this->pattern['current_log'][1], $h[$i], $current);
143147
if (!isset($current[4])) continue;
144148

145149
$log[] = array(
@@ -210,7 +214,7 @@ public function getFolderFiles($basename = false)
210214
public function getFiles($basename = false, $folder = '')
211215
{
212216
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
213-
$files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0);
217+
$files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match($this->pattern['files'], $pattern) ? GLOB_BRACE : 0);
214218
$files = array_reverse($files);
215219
$files = array_filter($files, 'is_file');
216220
if ($basename && is_array($files)) {
@@ -220,4 +224,4 @@ public function getFiles($basename = false, $folder = '')
220224
}
221225
return array_values($files);
222226
}
223-
}
227+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rap2hpoutre\LaravelLogViewer;
4+
5+
/**
6+
* Class Pattern
7+
* @property array patterns
8+
* @package Rap2hpoutre\LaravelLogViewer
9+
*/
10+
11+
class Pattern
12+
{
13+
14+
/**
15+
* @var array
16+
*/
17+
private static $patterns = [
18+
'logs' => '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/',
19+
'current_log' => [
20+
'/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)',
21+
': (.*?)( in .*?:[0-9]+)?$/i'
22+
],
23+
'files' => '/\{.*?\,.*?\}/i',
24+
];
25+
26+
/**
27+
* @return array
28+
*/
29+
public function all()
30+
{
31+
return array_keys($this->patterns);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)