1 | <?php |
||
32 | class CurlLogger implements LoggerInterface { |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | const PARAM_DEFAULT_FLAG = 'd'; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | const PARAM_URLENCODE_FLAG = '-data-urlencode'; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | const PARAM_POST_FLAG = 'F'; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | const METHOD_DEFAULT_FLAG = ''; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | const METHOD_GET_FLAG = 'G'; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | const METHOD_PUT_FLAG = 'X PUT'; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | const METHOD_DELETE_FLAG = 'X DELETE'; |
||
68 | |||
69 | /** |
||
70 | * @var resource |
||
71 | */ |
||
72 | protected $handle; |
||
73 | |||
74 | /** |
||
75 | * @var bool |
||
76 | */ |
||
77 | protected $jsonPrettyPrint = false; |
||
78 | |||
79 | /** |
||
80 | * @param resource $handle |
||
81 | */ |
||
82 | 7 | public function __construct($handle = null) { |
|
85 | |||
86 | /** |
||
87 | * @return bool |
||
88 | */ |
||
89 | 5 | public function isJsonPrettyPrint() { |
|
92 | |||
93 | /** |
||
94 | * @param bool $json_pretty_print |
||
95 | * @return $this |
||
96 | */ |
||
97 | 1 | public function setJsonPrettyPrint($json_pretty_print) { |
|
101 | |||
102 | /** |
||
103 | * @param string $method |
||
104 | * @return string |
||
105 | */ |
||
106 | 5 | public static function getMethodFlag($method) { |
|
118 | |||
119 | /** |
||
120 | * @param string $method |
||
121 | * @param string $value |
||
122 | * @return string |
||
123 | */ |
||
124 | 5 | public static function getParamFlag($method, $value) { |
|
125 | 5 | return $method === RequestInterface::METHOD_POST |
|
126 | 1 | ? static::PARAM_POST_FLAG |
|
127 | 4 | : (strstr($value, "\n") |
|
128 | 1 | ? static::PARAM_URLENCODE_FLAG |
|
129 | 5 | : static::PARAM_DEFAULT_FLAG); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $string |
||
134 | * @param int $indent |
||
135 | * @return string |
||
136 | */ |
||
137 | 1 | protected function indent($string, $indent) { |
|
140 | |||
141 | /** |
||
142 | * @param Parameters $params |
||
143 | * @param string $method |
||
144 | * @param bool $is_file |
||
145 | * @return string |
||
146 | */ |
||
147 | 5 | protected function processParams(Parameters $params, $method, $is_file) { |
|
148 | 5 | $chunks = array(); |
|
149 | 5 | if ($this->isJsonPrettyPrint()) { |
|
150 | 1 | $params = new JsonAwareParameters($params); |
|
151 | } |
||
152 | 5 | foreach ($params->export() as $name => $value) { |
|
153 | 5 | $value = addcslashes( |
|
154 | 5 | strpos($value, "\n") !== false |
|
155 | 1 | ? $this->indent($value, 2) |
|
156 | 5 | : $value, |
|
157 | 5 | '\''); |
|
158 | 5 | $chunks[$name] = sprintf( |
|
159 | 5 | '-%s \'%s=%s%s\'', |
|
160 | 5 | $this->getParamFlag($method, $value), |
|
161 | $name, |
||
162 | 5 | $is_file ? '@' : '', |
|
163 | 5 | $value); |
|
164 | } |
||
165 | |||
166 | 5 | return $chunks; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param RequestInterface $request |
||
171 | * @return string |
||
172 | */ |
||
173 | 5 | protected function processUrl(RequestInterface $request) { |
|
177 | |||
178 | /** |
||
179 | * @param string $buffer |
||
180 | */ |
||
181 | 5 | protected function flush($buffer) { |
|
184 | |||
185 | /** |
||
186 | * @param mixed $level |
||
187 | * @param string $message |
||
188 | * @param array $context |
||
189 | */ |
||
190 | 1 | public function log($level, $message, array $context = array()) { |
|
193 | |||
194 | /** |
||
195 | * @param array $array |
||
196 | * @param mixed $key |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 5 | protected function removeArrayKey(array &$array, $key) { |
|
208 | |||
209 | /** |
||
210 | * @param array $params |
||
211 | * @return array |
||
212 | */ |
||
213 | 5 | protected function sortParams(array $params) { |
|
221 | |||
222 | /** |
||
223 | * @param string $level |
||
224 | * @param RequestInterface $request |
||
225 | * @param array $context |
||
226 | */ |
||
227 | 5 | public function logRequest( |
|
228 | $level, RequestInterface $request, array $context = array()) { |
||
229 | |||
230 | 5 | $new_line = ' \\'.PHP_EOL.' '; |
|
231 | 5 | $method = $request->getMethod(); |
|
232 | 5 | $method_flag = static::getMethodFlag($method); |
|
233 | 5 | $params = $this->sortParams(array_merge( |
|
234 | 5 | $this->processParams($request->getQueryParams(), $method, false), |
|
235 | 5 | $this->processParams($request->getBodyParams(), $method, false), |
|
236 | 5 | $this->processParams($request->getFileParams(), $method, true))); |
|
237 | |||
238 | 5 | $buffer = 'curl'.($method_flag ? ' -'.$method_flag : ''); |
|
239 | 5 | foreach ($params as $param) { |
|
240 | 5 | $buffer .= $new_line.$param; |
|
241 | } |
||
242 | 5 | $buffer .= $new_line.$this->processUrl($request); |
|
243 | |||
244 | 5 | $this->flush($buffer); |
|
245 | 5 | } |
|
246 | |||
247 | /** |
||
248 | * @param string $level |
||
249 | * @param ResponseInterface $response |
||
250 | * @param array $context |
||
251 | */ |
||
252 | 1 | public function logResponse( |
|
256 | } |
||
257 |