1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Logger |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Logger\Processor; |
16
|
|
|
|
17
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* InterpolateProcessor |
21
|
|
|
* |
22
|
|
|
* Replace '{placeholder}' with values from context in the log message. |
23
|
|
|
* Should be the last processor in the queue before log handling. |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Logger |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see ProcessorAbstract |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
class InterpolateProcessor extends ProcessorAbstract |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Replace any '{item}' in the messsage with context['item'] value |
35
|
|
|
* |
36
|
|
|
* @see http://www.php-fig.org/psr/psr-3/ |
37
|
|
|
* |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
public function __invoke(LogEntryInterface $logEntry) |
41
|
|
|
{ |
42
|
|
|
$message = $logEntry->getMessage(); |
43
|
|
|
$context = $logEntry->getContext(); |
44
|
|
|
|
45
|
|
|
$replace = []; |
46
|
|
|
foreach ($this->getPlaceHolders($message) as $name => $ph) { |
47
|
|
|
$replace[$ph] = $this->replaceWith($name, $ph, $context); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$logEntry->setMessage(strtr($message, $replace)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get placeholders in array |
55
|
|
|
* |
56
|
|
|
* @param string $message |
57
|
|
|
* @return array |
58
|
|
|
* @access protected |
59
|
|
|
*/ |
60
|
|
|
protected function getPlaceHolders(/*# string */ $message)/*# : array */ |
61
|
|
|
{ |
62
|
|
|
// not found |
63
|
|
|
if (false === strpos($message, '{')) { |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$matches = []; |
68
|
|
|
$pattern = '~\{([^\}]+)\}~'; |
69
|
|
|
if (preg_match_all($pattern, $message, $matches)) { |
70
|
|
|
return array_combine($matches[1], $matches[0]); |
71
|
|
|
} |
72
|
|
|
return []; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Replace with values from the context array |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* @param string $placeholder |
80
|
|
|
* @param array &$context |
81
|
|
|
* @return string |
82
|
|
|
* @access protected |
83
|
|
|
*/ |
84
|
|
|
protected function replaceWith( |
85
|
|
|
/*# string */ $name, |
86
|
|
|
/*# string */ $placeholder, |
87
|
|
|
array &$context |
88
|
|
|
)/*# : string */ { |
89
|
|
|
// exact match |
90
|
|
|
if (isset($context[$name])) { |
91
|
|
|
return $this->getString($context[$name]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// something like user.name |
95
|
|
|
$first = explode('.', $name)[0]; |
96
|
|
|
if (false !== strpos($name, '.') && isset($context[$first])) { |
97
|
|
|
return $this->getSubPart($name, $placeholder, $context[$first]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// not found |
101
|
|
|
return $placeholder; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get string representation of mixed-typed $data |
106
|
|
|
* |
107
|
|
|
* @param mixed $data |
108
|
|
|
* @return string |
109
|
|
|
* @access protected |
110
|
|
|
*/ |
111
|
|
|
protected function getString($data)/*# : string */ |
112
|
|
|
{ |
113
|
|
|
if (is_scalar($data)) { |
114
|
|
|
return strval($data); |
115
|
|
|
} elseif (is_array($data)) { |
116
|
|
|
return 'ARRAY[' . count($data) . ']'; |
117
|
|
|
} elseif (is_object($data)) { |
118
|
|
|
return $this->getObjectString($data); |
119
|
|
|
} else { |
120
|
|
|
return 'TYPE: ' . gettype($data); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get string representation of an object |
126
|
|
|
* |
127
|
|
|
* @param object $object |
128
|
|
|
* @return string |
129
|
|
|
* @access protected |
130
|
|
|
*/ |
131
|
|
|
protected function getObjectString($object)/*# : string */ |
132
|
|
|
{ |
133
|
|
|
// exception found |
134
|
|
|
if ($object instanceof \Exception) { |
135
|
|
|
return 'EXCEPTION: ' . $object->getMessage(); |
136
|
|
|
|
137
|
|
|
// toString() found |
138
|
|
|
} elseif (method_exists($object, '__toString')) { |
139
|
|
|
return (string) $object; |
140
|
|
|
|
141
|
|
|
// other type object |
142
|
|
|
} else { |
143
|
|
|
return 'OBJECT: ' . get_class($object); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get 'user.name' type of result, only support 2 level |
149
|
|
|
* |
150
|
|
|
* @param string $name |
151
|
|
|
* @param string $placeholder used only if nothing matched |
152
|
|
|
* @param mixed $data |
153
|
|
|
* @return string |
154
|
|
|
* @access protected |
155
|
|
|
*/ |
156
|
|
|
protected function getSubPart( |
157
|
|
|
/*# string */ $name, |
158
|
|
|
/*# string */ $placeholder, |
159
|
|
|
$data |
160
|
|
|
)/*# : string */ { |
161
|
|
|
list(, $second) = explode('.', $name, 2); |
162
|
|
|
|
163
|
|
|
$arr = (array) $data; |
164
|
|
|
if (isset($arr[$second])) { |
165
|
|
|
return $arr[$second]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $placeholder; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|