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\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* InterpolateProcessor |
22
|
|
|
* |
23
|
|
|
* @package Phossa2\Logger |
24
|
|
|
* @author Hong Zhang <[email protected]> |
25
|
|
|
* @see ObjectAbstract |
26
|
|
|
* @see ProcessorInterface |
27
|
|
|
* @version 2.0.0 |
28
|
|
|
* @since 2.0.0 added |
29
|
|
|
*/ |
30
|
|
|
class InterpolateProcessor extends ObjectAbstract implements ProcessorInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Replace any '{item}' in the messsage with context['item'] value |
34
|
|
|
* |
35
|
|
|
* @see http://www.php-fig.org/psr/psr-3/ |
36
|
|
|
* |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
|
|
public function __invoke(LogEntryInterface $logEntry) |
40
|
|
|
{ |
41
|
|
|
$message = $logEntry->getMessage(); |
42
|
|
|
$context = $logEntry->getContexts(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$replace = []; |
45
|
|
|
foreach ($this->getPlaceHolders($message) as $name => $ph) { |
46
|
|
|
$replace[$ph] = $this->replaceWith($name, $ph, $context); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$logEntry->setMessage(strtr($message, $replace)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get placeholders in array |
54
|
|
|
* |
55
|
|
|
* @param string $message |
56
|
|
|
* @return array |
57
|
|
|
* @access protected |
58
|
|
|
*/ |
59
|
|
|
protected function getPlaceHolders(/*# string */ $message)/*# : array */ |
60
|
|
|
{ |
61
|
|
|
// not found |
62
|
|
|
if (false === strpos($message, '{')) { |
63
|
|
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$matches = []; |
67
|
|
|
$pattern = '~\{([^\}]+)\}~'; |
68
|
|
|
if (preg_match_all($pattern, $message, $matches)) { |
69
|
|
|
return array_combine($matches[1], $matches[0]); |
70
|
|
|
} |
71
|
|
|
return []; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Replace with values from the context array |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* @param string $placeholder |
79
|
|
|
* @param array &$context |
80
|
|
|
* @return string |
81
|
|
|
* @access protected |
82
|
|
|
*/ |
83
|
|
|
protected function replaceWith( |
84
|
|
|
/*# string */ $name, |
85
|
|
|
/*# string */ $placeholder, |
86
|
|
|
array &$context |
87
|
|
|
)/*# : string */ { |
88
|
|
|
// exact match |
89
|
|
|
if (isset($context[$name])) { |
90
|
|
|
return $this->getString($context[$name]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// something like user.name |
94
|
|
|
$first = explode('.', $name)[0]; |
95
|
|
|
if (false !== strpos($name, '.') && isset($context[$first])) { |
96
|
|
|
return $this->getSubPart($name, $placeholder, $context[$first]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// not found |
100
|
|
|
return $placeholder; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get string representation of mixed-typed $data |
105
|
|
|
* |
106
|
|
|
* @param mixed $data |
107
|
|
|
* @return string |
108
|
|
|
* @access protected |
109
|
|
|
*/ |
110
|
|
|
protected function getString($data)/*# : string */ |
111
|
|
|
{ |
112
|
|
|
if (is_scalar($data)) { |
113
|
|
|
return strval($data); |
114
|
|
|
|
115
|
|
|
} elseif (is_array($data)) { |
116
|
|
|
return 'ARRAY[' . count($data) . ']'; |
117
|
|
|
|
118
|
|
|
} elseif (is_object($data)) { |
119
|
|
|
return $this->getObjectString($data); |
120
|
|
|
|
121
|
|
|
} else { |
122
|
|
|
return 'TYPE: ' . gettype($data); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get string representation of an object |
128
|
|
|
* |
129
|
|
|
* @param object $object |
130
|
|
|
* @return string |
131
|
|
|
* @access protected |
132
|
|
|
*/ |
133
|
|
|
protected function getObjectString($object)/*# : string */ |
134
|
|
|
{ |
135
|
|
|
// exception found |
136
|
|
|
if ($object instanceof \Exception) { |
137
|
|
|
return 'EXCEPTION: ' . $object->getMessage(); |
138
|
|
|
|
139
|
|
|
// toString() found |
140
|
|
|
} elseif (method_exists($object, '__toString')) { |
141
|
|
|
return (string) $object; |
142
|
|
|
|
143
|
|
|
// other type object |
144
|
|
|
} else { |
145
|
|
|
return 'OBJECT: ' . get_class($object); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get 'user.name' type of result, only support 2 level |
151
|
|
|
* |
152
|
|
|
* @param string $name |
153
|
|
|
* @param string $placeholder used only if nothing matched |
154
|
|
|
* @param mixed $data |
155
|
|
|
* @return string |
156
|
|
|
* @access protected |
157
|
|
|
*/ |
158
|
|
|
protected function getSubPart( |
159
|
|
|
/*# string */ $name, |
160
|
|
|
/*# string */ $placeholder, |
161
|
|
|
$data |
162
|
|
|
)/*# : string */ { |
163
|
|
|
list(, $second) = explode('.', $name, 2); |
164
|
|
|
|
165
|
|
|
if ((is_array($data) && isset($data[$second]))) { |
166
|
|
|
return $data[$second]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ((is_object($data) && isset($data->$second))) { |
170
|
|
|
return $data->$second; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $placeholder; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.