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