Completed
Pull Request — master (#161)
by
unknown
01:06
created

LaravelLogViewer::setFileAll()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 4
nc 5
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
     * @var string storage_path
23
     */
24
    private $storage_path;
25
26
    /**
27
     * Why? Uh... Sorry
28
     */
29
    const MAX_FILE_SIZE = 52428800;
30
31
    /**
32
     * @var Level level
33
     */
34
    private $level;
35
36
    /**
37
     * @var Log Data log_data
38
     */
39
    private $log_data = [];
40
41
    /**
42
     * @var Pattern pattern
43
     */
44
    private $pattern;
45
46
    /**
47
     * LaravelLogViewer constructor.
48
     */
49
    public function __construct()
50
    {
51
        $this->level = new Level();
52
        $this->pattern = new Pattern();
53
        $this->storage_path = function_exists('config') ? config('logviewer.storage_path',
54
            storage_path('logs')) : storage_path('logs');
55
56
    }
57
58
    /**
59
     * @param string $folder
60
     */
61
    public function setFolder($folder)
62
    {
63
        $logsPath = $this->storage_path . '/' . $folder;
64
65
        if (app('files')->exists($logsPath)) {
66
            $this->folder = $folder;
67
        }
68
    }
69
70
    /**
71
     * @param string $file
72
     * @throws \Exception
73
     */
74
    public function setFile($file)
75
    {
76
        $file = $this->pathToLogFile($file);
77
78
        if (app('files')->exists($file)) {
79
            $this->file = $file;
80
        }
81
    }
82
83
    /**
84
     * @param string $file
85
     * @return string
86
     * @throws \Exception
87
     */
88
    public function pathToLogFile($file)
89
    {
90
        $logsPath = $this->storage_path;
91
        $logsPath .= ($this->folder) ? '/' . $this->folder : '';
92
93
        if (app('files')->exists($file)) { // try the absolute path
94
            return $file;
95
        }
96
97
        $file = $logsPath . '/' . $file;
98
99
        // check if requested file is really in the logs directory
100
        if (dirname($file) !== $logsPath) {
101
            throw new \Exception('No such log file');
102
        }
103
104
        return $file;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getFolderName()
111
    {
112
        return $this->folder;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getFileName()
119
    {
120
        return basename($this->file);
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    protected function setFileAll()
127
    {
128
        if (!$this->file) {
129
            $log_file = (!$this->folder) ? $this->getFiles() : $this->getFolderFiles();
130
            if (!count($log_file)) {
131
                return [];
132
            }
133
            $this->file = $log_file[0];
134
        }
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function all()
141
    {
142
        $log = array();
143
144
        //make sure $file is set
145
        $this->setFileAll();
146
147
        if (app('files')->size($this->file) > self::MAX_FILE_SIZE) {
148
            return null;
149
        }
150
151
        $file = app('files')->get($this->file);
152
153
        preg_match_all($this->pattern->getPattern('logs'), $file, $headings);
154
155
        if (!is_array($headings)) {
156
            return $log;
157
        }
158
159
        $this->log_data = preg_split($this->pattern->getPattern('logs'), $file);
0 ignored issues
show
Documentation Bug introduced by
It seems like preg_split($this->patter...Pattern('logs'), $file) of type array is incompatible with the declared type object<Rap2hpoutre\LaravelLogViewer\Log> of property $log_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
160
161
        if ($this->log_data[0] < 1) {
162
            array_shift($this->log_data);
163
        }
164
165
        $log = $this->getLogData($headings);
166
167
        if (empty($log)) {
168
169
            $lines = explode(PHP_EOL, $file);
170
            $log = [];
171
172
            foreach ($lines as $key => $line) {
173
                $log[] = $this->getArrayLog([
174
                    'index' => '',
175
                    'current' => [],
176
                    'level' => '',
177
                    'key' => $key,
178
                    'line' => $line
179
                ]);
180
            }
181
        }
182
183
        return array_reverse($log);
184
    }
185
186
    /**
187
     * @param $headings
188
     * @return array
189
     */
190
    protected function getLogData($headings)
191
    {
192
        $log = [];
193
        foreach ($headings as $h) {
194
            for ($i = 0, $j = count($h); $i < $j; $i++) {
195
                foreach ($this->level->all() as $key => $level) {
196
                    if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
197
198
                        preg_match($this->pattern->getPattern('current_log',
199
                                0) . $level . $this->pattern->getPattern('current_log', 1), $h[$i],
200
                            $current);
201
                        if (!isset($current[4])) {
202
                            continue;
203
                        }
204
                        $log[] = $this->getArrayLog([
205
                            'index' => $i,
206
                            'current' => $current,
207
                            'level' => $level,
208
                            'key' => $key,
209
                            'line' => ''
210
                        ]);
211
                    }
212
                }
213
            }
214
        }
215
        return $log;
216
    }
217
218
    /**
219
     * Create array data from log
220
     * @param $data
221
     * @return array
222
     */
223
    protected function getArrayLog($data)
224
    {
225
        $index = $data['index'];
226
        $current = $data['current'];
227
        $level = $data['level'];
228
        $key = $data['key'];
229
        $line = $data['line'];
230
        return array(
231
            'context' => isset($current[3]) ? $current[3] : '',
232
            'level' => isset($level) ? $level : '',
233
            'level_class' => isset($level) ? $this->level->cssClass($level) : '',
234
            'level_img' => isset($level) ? $this->level->img($level) : '',
235
            'date' => isset($current[1]) ? $current[1] : $key + 1,
236
            'text' => isset($current[4]) ? $current[4] : $line,
237
            'in_file' => isset($current[5]) ? $current[5] : null,
238
            'stack' => preg_replace("/^\n*/", '', $this->log_data[$index])
239
        );
240
    }
241
242
    /**
243
     * @return array
244
     */
245
    public function getFolders()
246
    {
247
        $folders = [];
248
        if (is_array($this->storage_path)) {
249
            foreach ($this->storage_path as $value) {
250
                $folders = array_merge(
251
                    $folders,
252
                    glob($value . '/*', GLOB_ONLYDIR)
253
                );
254
            }
255
        }
256
257
        if (!is_array($this->storage_path)) {
258
            $folders = glob($this->storage_path . '/*', GLOB_ONLYDIR);
259
        }
260
261
        if (is_array($folders)) {
262
            foreach ($folders as $k => $folder) {
263
                $folders[$k] = basename($folder);
264
            }
265
        }
266
        return array_values($folders);
267
    }
268
269
    /**
270
     * @param bool $basename
271
     * @return array
272
     */
273
    public function getFolderFiles($basename = false)
274
    {
275
        return $this->getFiles($basename, $this->folder);
276
    }
277
278
    /**
279
     * @param bool $basename
280
     * @param string $folder
281
     * @return array
282
     */
283
    public function getFiles($basename = false, $folder = '')
284
    {
285
        $files = [];
286
        $pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
287
        if (is_array($this->storage_path)) {
288
            foreach ($this->storage_path as $value) {
289
                $files = array_merge(
290
                    $files,
291
                    glob(
292
                        $value . '/' . $folder . '/' . $pattern,
293
                        preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
294
                    )
295
                );
296
            }
297
        }
298
299
        if (!is_array($this->storage_path)) {
300
            $files = glob(
301
                $this->storage_path . '/' . $folder . '/' . $pattern,
302
                preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0
303
            );
304
        }
305
306
        $files = array_reverse($files);
307
        $files = array_filter($files, 'is_file');
308
        if ($basename && is_array($files)) {
309
            foreach ($files as $k => $file) {
310
                $files[$k] = basename($file);
311
            }
312
        }
313
        return array_values($files);
314
    }
315
}
316