Completed
Push — master ( 0edf84...a8febf )
by raphael
01:18
created

LaravelLogViewer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Rap2hpoutre\LaravelLogViewer;
4
5
/**
6
 * Class LaravelLogViewer
7
 * @package Rap2hpoutre\LaravelLogViewer
8
 */
9
class LaravelLogViewer
10
{
11
    /**
12
     * @var string file
13
     */
14
    private $file;
15
16
    /**
17
     * @var string folder
18
     */
19
    private $folder;
20
21
    /**
22
     * Why? Uh... Sorry
23
     */
24
    const MAX_FILE_SIZE = 52428800;
25
26
    /**
27
     * @var Level level
28
     */
29
    private $level;
30
31
    /**
32
     * LaravelLogViewer constructor.
33
     */
34
    public function __construct()
35
    {
36
        $this->level = new Level();
37
    }
38
39
    /**
40
     * @param string $folder
41
     */
42
    public function setFolder($folder)
43
    {
44
        $logsPath = storage_path('logs') . '/' . $folder;
45
46
        if (app('files')->exists($logsPath)) {
47
            $this->folder = $folder;
48
        }
49
    }
50
51
    /**
52
     * @param string $file
53
     * @throws \Exception
54
     */
55
    public function setFile($file)
56
    {
57
        $file = $this->pathToLogFile($file);
58
59
        if (app('files')->exists($file)) {
60
            $this->file = $file;
61
        }
62
    }
63
64
    /**
65
     * @param string $file
66
     * @return string
67
     * @throws \Exception
68
     */
69
    public function pathToLogFile($file)
70
    {
71
        $logsPath = storage_path('logs');
72
        $logsPath .= ($this->folder) ? '/' . $this->folder : '';
73
74
        if (app('files')->exists($file)) { // try the absolute path
75
            return $file;
76
        }
77
78
        $file = $logsPath . '/' . $file;
79
80
        // check if requested file is really in the logs directory
81
        if (dirname($file) !== $logsPath) {
82
            throw new \Exception('No such log file');
83
        }
84
85
        return $file;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getFolderName()
92
    {
93
        return $this->folder;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getFileName()
100
    {
101
        return basename($this->file);
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function all()
108
    {
109
        $log = array();
110
111
        $pattern = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/';
112
113
        if (!$this->file) {
114
            $log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles();
115
            if (!count($log_file)) {
116
                return [];
117
            }
118
            $this->file = $log_file[0];
119
        }
120
121
        if (app('files')->size($this->file) > self::MAX_FILE_SIZE) return null;
122
123
        $file = app('files')->get($this->file);
124
125
        preg_match_all($pattern, $file, $headings);
126
127
        if (!is_array($headings)) {
128
            return $log;
129
        }
130
131
        $log_data = preg_split($pattern, $file);
132
133
        if ($log_data[0] < 1) {
134
            array_shift($log_data);
135
        }
136
137
        foreach ($headings as $h) {
138
            for ($i = 0, $j = count($h); $i < $j; $i++) {
139
                foreach ($this->level->all() as $level) {
140
                    if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
141
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);
143
                        if (!isset($current[4])) continue;
144
145
                        $log[] = array(
146
                            'context' => $current[3],
147
                            'level' => $level,
148
                            'level_class' => $this->level->cssClass($level),
149
                            'level_img' => $this->level->img($level),
150
                            'date' => $current[1],
151
                            'text' => $current[4],
152
                            'in_file' => isset($current[5]) ? $current[5] : null,
153
                            'stack' => preg_replace("/^\n*/", '', $log_data[$i])
154
                        );
155
                    }
156
                }
157
            }
158
        }
159
160
        if (empty($log)) {
161
162
            $lines = explode(PHP_EOL, $file);
163
            $log = [];
164
165
            foreach ($lines as $key => $line) {
166
                $log[] = [
167
                    'context' => '',
168
                    'level' => '',
169
                    'level_class' => '',
170
                    'level_img' => '',
171
                    'date' => $key + 1,
172
                    'text' => $line,
173
                    'in_file' => null,
174
                    'stack' => '',
175
                ];
176
            }
177
        }
178
179
        return array_reverse($log);
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public function getFolders()
186
    {
187
        $folders = glob(storage_path() . '/logs/*', GLOB_ONLYDIR);
188
        if (is_array($folders)) {
189
            foreach ($folders as $k => $folder) {
190
                $folders[$k] = basename($folder);
191
            }
192
        }
193
        return array_values($folders);
194
    }
195
196
    /**
197
     * @param bool $basename
198
     * @return array
199
     */
200
    public function getFolderFiles($basename = false)
201
    {
202
        return $this->getFiles($basename, $this->folder);
203
    }
204
205
    /**
206
     * @param bool $basename
207
     * @param string $folder
208
     * @return array
209
     */
210
    public function getFiles($basename = false, $folder = '')
211
    {
212
        $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
213
        $files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match('/\{.*?\,.*?\}/i', $pattern) ? GLOB_BRACE : 0);
214
        $files = array_reverse($files);
215
        $files = array_filter($files, 'is_file');
216
        if ($basename && is_array($files)) {
217
            foreach ($files as $k => $file) {
218
                $files[$k] = basename($file);
219
            }
220
        }
221
        return array_values($files);
222
    }
223
}
224