Completed
Pull Request — master (#176)
by
unknown
01:13
created

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