Skip to content

Commit

Permalink
patterns for preg_match* (#156)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
elminson authored and rap2hpoutre committed Jul 17, 2018
1 parent 4502f4e commit b0a7055
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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)) {
Expand All @@ -220,4 +224,4 @@ public function getFiles($basename = false, $folder = '')
}
return array_values($files);
}
}
}
34 changes: 34 additions & 0 deletions src/Rap2hpoutre/LaravelLogViewer/Pattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rap2hpoutre\LaravelLogViewer;

/**
* Class Pattern
* @property array patterns
* @package Rap2hpoutre\LaravelLogViewer
*/

class Pattern
{

/**
* @var array
*/
private static $patterns = [
'logs' => '/\[\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);
}

}

0 comments on commit b0a7055

Please sign in to comment.