- Added a process to native handler
This commit is contained in:
parent
d94ee83230
commit
3590993925
|
@ -8,5 +8,22 @@ class NativeHandler extends Handler {
|
||||||
{
|
{
|
||||||
return $this->exceptionHandler->extractExceptionData($ex, $_SERVER, $_POST);
|
return $this->exceptionHandler->extractExceptionData($ex, $_SERVER, $_POST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function process(callable $callback) : mixed
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return $callback();
|
||||||
|
}
|
||||||
|
catch (\Throwable $ex)
|
||||||
|
{
|
||||||
|
$this->pushData($ex);
|
||||||
|
|
||||||
|
if ( $this->callback ?? false ) {
|
||||||
|
return call_user_func_array($this->callback, [ $ex ] );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw $ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,11 @@ namespace Negundo\Client\Transport;
|
||||||
|
|
||||||
class Curl implements TransportInterface {
|
class Curl implements TransportInterface {
|
||||||
|
|
||||||
public $timeout = 1;
|
public $timeout = 2;
|
||||||
|
|
||||||
public $throwErrors = false;
|
public $throwErrors = true;
|
||||||
|
|
||||||
|
public $verifySsl = false;
|
||||||
|
|
||||||
public $headers = [];
|
public $headers = [];
|
||||||
|
|
||||||
|
@ -20,22 +22,46 @@ class Curl implements TransportInterface {
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200);
|
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySsl);
|
||||||
|
|
||||||
$exec = curl_exec($ch);
|
$exec = curl_exec($ch);
|
||||||
|
|
||||||
if ( ( false === $exec ) && $this->throwErrors ) {
|
if ( ( false === $exec ) && $this->throwErrors ) {
|
||||||
$errno = curl_errno($ch);
|
$errno = curl_errno($ch);
|
||||||
$errors = array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ]);
|
curl_close($ch);
|
||||||
|
|
||||||
throw new CurlException(implode(PHP_EOL, $errors), $errno);
|
throw new CurlException(implode(PHP_EOL, array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ])), $errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
return $exec;
|
return $exec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get(string $url) : ? object
|
||||||
|
{
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers );
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySsl);
|
||||||
|
$execute = curl_exec($ch);
|
||||||
|
|
||||||
|
if ( ( false === $execute ) && $this->throwErrors ) {
|
||||||
|
$errno = curl_errno($ch);
|
||||||
|
|
||||||
|
curl_close($ch);
|
||||||
|
throw new CurlException(implode(PHP_EOL, array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ])), $errno);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
curl_close($ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $execute ? (object) $execute : null;
|
||||||
|
}
|
||||||
|
|
||||||
const CURL_ERROR = [
|
const CURL_ERROR = [
|
||||||
0 => 'CURLE_OK',
|
0 => 'CURLE_OK',
|
||||||
1 => 'CURLE_UNSUPPORTED_PROTOCOL',
|
1 => 'CURLE_UNSUPPORTED_PROTOCOL',
|
||||||
|
|
|
@ -22,7 +22,7 @@ class ExceptionHandler {
|
||||||
'HTTP_USER_AGENT' => $serverData['HTTP_USER_AGENT'] ?? null,
|
'HTTP_USER_AGENT' => $serverData['HTTP_USER_AGENT'] ?? null,
|
||||||
'REMOTE_ADDR' => $serverData['REMOTE_ADDR'] ?? null,
|
'REMOTE_ADDR' => $serverData['REMOTE_ADDR'] ?? null,
|
||||||
];
|
];
|
||||||
|
|
||||||
$post = [
|
$post = [
|
||||||
'code' => $ex->getCode(),
|
'code' => $ex->getCode(),
|
||||||
'file' => $ex->getFile(),
|
'file' => $ex->getFile(),
|
||||||
|
|
Loading…
Reference in New Issue