|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cesargb\LaravelLog\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use Cesargb\LaravelLog\Events\RotateHasFailed; |
|
6
|
|
|
use Cesargb\LaravelLog\Helpers\Log as LogHelper; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractHandler implements HandlerInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected $file; |
|
12
|
|
|
|
|
13
|
|
|
protected $compress; |
|
14
|
|
|
|
|
15
|
|
|
protected $dir_to_archive; |
|
16
|
|
|
|
|
17
|
|
|
protected $file_rotated; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct($file, bool $compress = true, $dir_to_archive = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->file = $file; |
|
22
|
|
|
$this->compress = $compress; |
|
23
|
|
|
|
|
24
|
|
|
if (empty($dir_to_archive)) { |
|
25
|
|
|
$this->dir_to_archive = dirname($file); |
|
26
|
|
|
} else { |
|
27
|
|
|
if (substr($dir_to_archive, 0, 1) == '/') { |
|
28
|
|
|
$this->dir_to_archive = $dir_to_archive; |
|
29
|
|
|
} else { |
|
30
|
|
|
$this->dir_to_archive = dirname($file).'/'.$dir_to_archive; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function validate() |
|
36
|
|
|
{ |
|
37
|
|
|
clearstatcache(); |
|
38
|
|
|
|
|
39
|
|
|
return $this->validateFile() && $this->validateDirectory(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function validateFile(): bool |
|
43
|
|
|
{ |
|
44
|
|
|
if (! is_file($this->file)) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (filesize($this->file) == 0) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (! is_writable($this->file)) { |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function validateDirectory(): bool |
|
60
|
|
|
{ |
|
61
|
|
|
if (is_dir($this->dir_to_archive)) { |
|
62
|
|
|
if (! is_writable($this->dir_to_archive)) { |
|
63
|
|
|
event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive logs is not writable'))); |
|
64
|
|
|
|
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (file_exists($this->dir_to_archive)) { |
|
72
|
|
|
event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive exists and is not a directory'))); |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (! mkdir($this->dir_to_archive, 0777, true)) { |
|
78
|
|
|
event(new RotateHasFailed($this->file, new Exception('Directory '.$this->dir_to_archive.' to archive logs is not writable'))); |
|
79
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function rebaseArchiveDir($file) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->dir_to_archive.'/'.basename($file); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function close() |
|
92
|
|
|
{ |
|
93
|
|
|
LogHelper::closeHandlers(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function moveData($fileSource, $fileDestination) |
|
97
|
|
|
{ |
|
98
|
|
|
$fdSource = fopen($fileSource, 'r+'); |
|
99
|
|
|
|
|
100
|
|
|
if ($fdSource === false) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (! flock($fdSource, LOCK_EX)) { |
|
105
|
|
|
fclose($fdSource); |
|
106
|
|
|
|
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (! copy($fileSource, $fileDestination)) { |
|
111
|
|
|
fclose($fdSource); |
|
112
|
|
|
|
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (! ftruncate($fdSource, 0)) { |
|
117
|
|
|
fclose($fdSource); |
|
118
|
|
|
|
|
119
|
|
|
unlink($fileDestination); |
|
120
|
|
|
|
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
flock($fdSource, LOCK_UN); |
|
125
|
|
|
|
|
126
|
|
|
fflush($fdSource); |
|
127
|
|
|
|
|
128
|
|
|
fclose($fdSource); |
|
129
|
|
|
|
|
130
|
|
|
clearstatcache(); |
|
131
|
|
|
|
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|