Completed
Pull Request — master (#145)
by
unknown
02:35
created

LaravelLogViewer::getFiles()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
namespace Rap2hpoutre\LaravelLogViewer;
3
4
use Psr\Log\LogLevel;
5
6
/**
7
 * Class LaravelLogViewer
8
 * @package Rap2hpoutre\LaravelLogViewer
9
 */
10
class LaravelLogViewer
11
{
12
    /**
13
     * @var string file
14
     */
15
    private static $file;
16
17
    private static $levels_classes = [
18
        'debug' => 'info',
19
        'info' => 'info',
20
        'notice' => 'info',
21
        'warning' => 'warning',
22
        'error' => 'danger',
23
        'critical' => 'danger',
24
        'alert' => 'danger',
25
        'emergency' => 'danger',
26
        'processed' => 'info',
27
        'failed' => 'warning',
28
    ];
29
30
    private static $levels_imgs = [
31
        'debug' => 'info-circle',
32
        'info' => 'info-circle',
33
        'notice' => 'info-circle',
34
        'warning' => 'exclamation-triangle',
35
        'error' => 'exclamation-triangle',
36
        'critical' => 'exclamation-triangle',
37
        'alert' => 'exclamation-triangle',
38
        'emergency' => 'exclamation-triangle',
39
        'processed' => 'info-circle',
40
        'failed' => 'exclamation-triangle'
41
    ];
42
43
    /**
44
     * Log levels that are used
45
     * @var array
46
     */
47
    private static $log_levels = [
48
        'emergency',
49
        'alert',
50
        'critical',
51
        'error',
52
        'warning',
53
        'notice',
54
        'info',
55
        'debug',
56
        'processed',
57
        'failed'
58
    ];
59
60
    const MAX_FILE_SIZE = 52428800; // Why? Uh... Sorry
61
62
    /**
63
     * @param string $file
64
     */
65
    public static function setFile($file)
66
    {
67
        $file = self::pathToLogFile($file);
68
69
        if (app('files')->exists($file)) {
70
            self::$file = $file;
71
        }
72
    }
73
74
    /**
75
     * @param string $file
76
     * @return string
77
     * @throws \Exception
78
     */
79
    public static function pathToLogFile($file)
80
    {
81
        $logsPath = storage_path('logs');
82
83
        if (app('files')->exists($file)) { // try the absolute path
84
            return $file;
85
        }
86
87
        $file = $logsPath . '/' . $file;
88
89
        // check if requested file is really in the logs directory
90
        if (dirname($file) !== $logsPath) {
91
            throw new \Exception('No such log file');
92
        }
93
94
        return $file;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public static function getFileName()
101
    {
102
        return basename(self::$file);
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public static function all()
109
    {
110
        $log = array();
111
112
        $pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/';
113
114
        if (!self::$file) {
115
            $log_file = self::getFiles();
116
            if(!count($log_file)) {
117
                return [];
118
            }
119
            self::$file = $log_file[0];
120
        }
121
122
        if (app('files')->size(self::$file) > self::MAX_FILE_SIZE) return null;
123
124
        $file = app('files')->get(self::$file);
125
126
        preg_match_all($pattern, $file, $headings);
127
128
        if (!is_array($headings)) return $log;
129
130
        $log_data = preg_split($pattern, $file);
131
132
        if ($log_data[0] < 1) {
133
            array_shift($log_data);
134
        }
135
136
        foreach ($headings as $h) {
137
            for ($i=0, $j = count($h); $i < $j; $i++) {
138
                foreach (self::$log_levels as $level) {
139
                    if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
140
141
                        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);
142
                        if (!isset($current[4])) continue;
143
144
                        $log[] = array(
145
                            'context' => $current[3],
146
                            'level' => $level,
147
                            'level_class' => self::$levels_classes[$level],
148
                            'level_img' => self::$levels_imgs[$level],
149
                            'date' => $current[1],
150
                            'text' => $current[4],
151
                            'in_file' => isset($current[5]) ? $current[5] : null,
152
                            'stack' => preg_replace("/^\n*/", '', $log_data[$i])
153
                        );
154
                    }
155
                }
156
            }
157
        }
158
159
        return array_reverse($log);
160
    }
161
162
    /**
163
     * @param bool $basename
0 ignored issues
show
Bug introduced by
There is no parameter named $basename. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
164
     * @return array
165
     */
166
    public static function getFiles()
167
    {
168
        $folders = [storage_path() . '/logs'];
169
        if (function_exists('config')) {
170
            $folders = array_merge($folders, config('logviewer.folders'));
171
        }
172
        $files = [];
173
        foreach ($folders as $folder) {
174
            $files = array_merge($files, self::getFilesByFolder($folder));
175
        }
176
        return $files;
177
    }
178
179
    /**
180
     * Retrieve all files that match the specific pattern.
181
     * @param string $folder
182
     * @return array
183
     */
184
    private static function getFilesByFolder($folder)
185
    {
186
        $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
187
        $folder_path = substr($folder, -1, 1) === '/' ? substr($folder, 0, strlen($folder) - 1) : $folder;
188
189
        if (!is_dir($folder_path)) {
190
            return [];
191
        }
192
193
        $files = glob($folder_path . '/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0);
194
        $files = array_reverse($files);
195
        $files = array_filter($files, 'is_file');
196
197
        if (is_array($files)) {
198
            return $files;
199
        }
200
        return [];
201
    }
202
}
203