-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaBasicDataFormatter.php
174 lines (159 loc) · 5.03 KB
/
aBasicDataFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace phpWTL;
use phpWTL\aSingleton;
require_once 'aSingleton.php';
/**
* Abstract class for a content (logger field) formatter (also handling field enclosing marks).
*
* @author Michael Beyer <[email protected]>
* @version v0.2.0
* @api
*/
abstract class aBasicDataFormatter extends aSingleton {
protected static $loggerContent= null;
protected static $fieldDescriptor= null;
/**
* Format only a single log field and store it in the associated LoggerContent object.
*
* @param string $field_name ID of log format field.
* @param string $value Provide an (optional) value to format and pass thru to the LoggerContent object, so allowing for the injection of external data.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
abstract public function formatField($field_name, $value= null);
/**
* Format and enclose (prefix/suffix) all log fields.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function formatAll() {
if (static::$fieldDescriptor && static::$loggerContent) {
static::format();
static::enclose();
}
}
/**
* Format only all log fields.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function format() {
if (static::$fieldDescriptor && static::$loggerContent) {
foreach (static::$fieldDescriptor->getFieldNames() as $k=>$f) {
static::formatField($f);
}
}
}
/**
* Format and enclose (prefix/suffix) a single log field and store it in the associated LoggerContent object.
*
* @param string $field_name ID of log format field.
* @param string $value Provide an (optional) value to format and pass thru to the LoggerContent object, so allowing for the injection of external data.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function formatAllField($field_name, $value= null) {
if (static::$fieldDescriptor && static::$loggerContent) {
static::formatField($field_name, $value);
static::encloseField($field_name, $value);
}
}
/**
* Enclose only (prefix/suffix) all log fields.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function enclose() {
if (static::$fieldDescriptor && static::$loggerContent) {
foreach (static::$fieldDescriptor->getFieldNames() as $k=>$f) {
static::encloseField($f);
}
}
}
/**
* Enclose (prefix/suffix) a single log field and store it in the associated LoggerContent object.
*
* @param string $field_name ID of log format field.
* @param string $value Provide an (optional) value to format and pass thru to the LoggerContent object, so allowing for the injection of external data.
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function encloseField($field_name, $value= null) {
if (static::$fieldDescriptor && static::$loggerContent) {
$prefix= static::$fieldDescriptor->getPrefix($field_name);
$suffix= static::$fieldDescriptor->getSuffix($field_name);
if (!$value) $value= static::$loggerContent->__get($field_name);
static::$loggerContent->__set($field_name, $prefix.$value.$suffix);
}
}
/**
* Helper method to build content for the "request line" of the "common" and "combined" log format.
*
* @param string $request_method
* @param string $request_uri
* @param string $query_string May be blank
* @param string $protocol
* @return string Proper request line, format: request_method request_uri[?query_string] protocol
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function buildRequestLineString($request_method, $request_uri, $query_string = null, $protocol) {
$uri= $query_string ? rtrim($request_uri, "?".$query_string) : $request_uri;
return $request_method." ".$uri.($query_string ? "?".$query_string : "")." ".$protocol;
}
/**
* Helper method to split the content of a "request line" of the "common" and "combined" log format into its chunks.
*
* @param string $request_line
* @return array An associative array representing the single portions of the request line: request_method[0], request_uri[1], query_string[2], protocol[3].
*
* @author Michael Beyer <[email protected]>
* @version v0.1.0
* @api
*/
public function explodeRequestLineString($request_line) {
$ret= null;
$prefix= $suffix= "";
foreach (static::$formatFields as $k=>$f) {
if ($f->name=="request_line") {
$prefix= $f->prefix;
$suffix= $f->suffix;
}
}
$request_line= ltrim($request_line, $prefix);
$request_line= rtrim($request_line, $suffix);
$main= explode(" ", $request_line);
$uri= explode("?", $main[1]);
if (count($uri)<=1) {
$ret= array(
'request_method' => $main[0],
'request_uri' => $main[1],
'query_string' => "",
'protocol' => $main[2]
);
} else {
$ret= array(
'request_method' => $main[0],
'request_uri' => $uri[0],
'query_string' => $uri[1],
'protocol' => $main[2]
);
}
return $ret;
}
}
?>