1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Subreality\Dilmun\Nabu\LoggerHandler; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Log file object implementing a standard HandlerInterface for log entry handlers. |
7
|
|
|
* |
8
|
|
|
* @author derek |
9
|
|
|
*/ |
10
|
|
|
class File extends AbstractHandler |
11
|
|
|
{ |
12
|
|
|
private $file_name; |
13
|
|
|
private $file_path; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \SplFileObject |
17
|
|
|
*/ |
18
|
|
|
private $file_pointer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Sets up the file name and path if optional parameters provided. |
22
|
|
|
* |
23
|
|
|
* @param string|null $file_name [optional] The name of the file used for logging; file_path is not required if |
24
|
|
|
* file_name is fully-qualified. |
25
|
|
|
* @param string|null $file_path [optional] The path of the file used for logging without the trailing directory |
26
|
|
|
* separator |
27
|
|
|
*/ |
28
|
18 |
|
public function __construct($file_name = null, $file_path = null) |
29
|
|
|
{ |
30
|
18 |
|
$this->file_name = $file_name; |
31
|
|
|
|
32
|
|
|
//Trim any trailing directory separators from the path. |
33
|
18 |
|
if (isset($file_path)) { |
34
|
1 |
|
$this->setFilePath($file_path); |
35
|
1 |
|
} |
36
|
18 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Clean-up |
40
|
|
|
*/ |
41
|
18 |
|
public function __destruct() |
42
|
|
|
{ |
43
|
18 |
|
$this->close(); |
44
|
18 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string|null The file_name set for the File instance |
48
|
|
|
*/ |
49
|
2 |
|
public function getFileName() |
50
|
|
|
{ |
51
|
2 |
|
return $this->file_name; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string|null The file_path set for the File instance |
56
|
|
|
*/ |
57
|
3 |
|
public function getFilePath() |
58
|
|
|
{ |
59
|
3 |
|
return $this->file_path; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets the name of the file to be used for logging; setting file_path is not required if the specified file name |
64
|
|
|
* is fully-qualified. |
65
|
|
|
* |
66
|
|
|
* @param string $file_name The name of the file to be used for logging |
67
|
|
|
*/ |
68
|
2 |
|
public function setFileName($file_name) |
69
|
|
|
{ |
70
|
2 |
|
$this->file_name = $file_name; |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets the path of the file to be used for logging; file path should not be set if the File's file name is |
75
|
|
|
* fully-qualified. |
76
|
|
|
* |
77
|
|
|
* @see File::setFileName() |
78
|
|
|
* |
79
|
|
|
* @param string $file_path The path of the file to be used for logging |
80
|
|
|
*/ |
81
|
3 |
|
public function setFilePath($file_path) |
82
|
|
|
{ |
83
|
|
|
//Trip any trailing directory separators from the path. |
84
|
3 |
|
$this->file_path = rtrim($file_path, '\/'); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Truncates the File to zero bytes, effectively clearing the file's contents. Does not delete the file. |
89
|
|
|
* |
90
|
|
|
* @return bool Returns true if truncate succeeded; returns false on failure. |
91
|
|
|
*/ |
92
|
2 |
|
public function clearFile() |
93
|
|
|
{ |
94
|
2 |
|
$cleared = false; |
95
|
|
|
|
96
|
2 |
|
if (isset($this->file_pointer)) { |
97
|
1 |
|
$cleared = $this->file_pointer->ftruncate(0); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
2 |
|
return $cleared; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets the File's file_pointer to null to clean up resource. |
105
|
|
|
*/ |
106
|
18 |
|
public function close() |
107
|
|
|
{ |
108
|
18 |
|
$this->file_pointer = null; |
109
|
18 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Initializes the File for writing via append. Initialization attempts to create an SplFileObject pointer using |
113
|
|
|
* the File's name and path properties. |
114
|
|
|
* |
115
|
|
|
* @see \SplFileObject |
116
|
|
|
* |
117
|
|
|
* @return bool Returns true on successful file pointer creation; returns false on failure. |
118
|
|
|
*/ |
119
|
11 |
|
public function initialize() |
120
|
|
|
{ |
121
|
11 |
|
$full_file = ""; |
122
|
|
|
|
123
|
11 |
|
if (isset($this->file_path)) { |
124
|
1 |
|
$full_file = $this->file_path . DIRECTORY_SEPARATOR; |
125
|
1 |
|
} |
126
|
|
|
|
127
|
11 |
|
$full_file .= $this->file_name; |
128
|
|
|
|
129
|
|
|
try { |
130
|
11 |
|
$this->file_pointer = new \SplFileObject($full_file, "a"); |
131
|
|
|
|
132
|
5 |
|
return true; |
133
|
6 |
|
} catch (\Exception $e) { |
134
|
6 |
|
return false; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Writes the specified message to the File pointer. Requires initialization via File::initialize(). |
140
|
|
|
* |
141
|
|
|
* @see File::initialize() |
142
|
|
|
* |
143
|
|
|
* @param string $message Message to be written to the file. |
144
|
|
|
* |
145
|
|
|
* @return bool Returns true on successful write; returns false on failure. |
146
|
|
|
*/ |
147
|
7 |
|
public function write($message) |
148
|
|
|
{ |
149
|
7 |
|
$file_writable = false; |
150
|
7 |
|
$message_written = false; |
151
|
|
|
|
152
|
7 |
|
if ($this->output_allowed) { |
153
|
6 |
|
if (isset($this->file_pointer)) { |
154
|
2 |
|
$file_writable = $this->file_pointer->flock(LOCK_EX); |
155
|
2 |
|
} |
156
|
|
|
|
157
|
6 |
|
if ($file_writable) { |
158
|
2 |
|
$now = date('Y-m-d H:i:s'); |
159
|
2 |
|
$this->file_pointer->fwrite("$now : $message" . "\r\n"); |
160
|
2 |
|
$this->file_pointer->flock(LOCK_UN); |
161
|
|
|
|
162
|
2 |
|
$message_written = true; |
163
|
2 |
|
} |
164
|
6 |
|
} |
165
|
|
|
|
166
|
7 |
|
return $message_written; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|