1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-20 16:16:37 +02:00

Replace Curl with SocketHandler in FleepHookHandler

This commit is contained in:
Ando Roots
2014-08-28 23:33:42 +03:00
parent c8d0830860
commit b25697a3f0
2 changed files with 55 additions and 203 deletions

View File

@@ -22,32 +22,18 @@ use Monolog\Logger;
* @see https://fleep.io/integrations/webhooks/ Fleep Webhooks Documentation
* @author Ando Roots <ando@sqroot.eu>
*/
class FleepHookHandler extends AbstractProcessingHandler
class FleepHookHandler extends SocketHandler
{
const HOOK_ENDPOINT = 'https://fleep.io/hook/';
const FLEEP_HOST = 'fleep.io';
const FLEEP_HOOK_URI = '/hook/';
/**
* @var string Webhook token (specifies the conversation where logs are sent)
*/
protected $token;
/**
* @var string Full URI to the webhook endpoint (HOOK_ENDPOINT + token)
*/
protected $url;
/**
* @var array Default options to Curl
*/
protected $curlOptions = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_RETURNTRANSFER => true
);
/**
* Construct a new Fleep.io Handler.
*
@@ -57,26 +43,18 @@ class FleepHookHandler extends AbstractProcessingHandler
* @param string $token Webhook token
* @param bool|int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @throws \LogicException
* @throws MissingExtensionException
*/
public function __construct($token, $level = Logger::DEBUG, $bubble = true)
{
if (!extension_loaded('curl')) {
throw new \LogicException('The curl extension is needed to use FleepHookHandler');
if (!extension_loaded('openssl')) {
throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FleepHookHandler');
}
$this->token = $token;
$this->url = self::HOOK_ENDPOINT . $this->token;
parent::__construct($level, $bubble);
}
/**
* @return array
*/
public function getCurlOptions()
{
return $this->curlOptions;
$connectionString = 'ssl://' . self::FLEEP_HOST . ':443';
parent::__construct($connectionString, $level, $bubble);
}
/**
@@ -97,59 +75,58 @@ class FleepHookHandler extends AbstractProcessingHandler
*
* @param array $record
*/
protected function write(array $record)
public function write(array $record)
{
$this->send($record['formatted']);
parent::write($record);
$this->closeSocket();
}
/**
* Prepares the record for sending to Fleep
* {@inheritdoc}
*
* @param string $message The formatted log message to send
* @param array $record
* @return string
*/
protected function send($message)
protected function generateDataStream($record)
{
$this->addCurlOptions(
array(
CURLOPT_POSTFIELDS => http_build_query(array('message' => $message)),
CURLOPT_URL => $this->url,
)
$content = $this->buildContent($record);
return $this->buildHeader($content) . $content;
}
/**
* Builds the header of the API Call
*
* @param string $content
* @return string
*/
private function buildHeader($content)
{
$header = "POST " . self::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n";
$header .= "Host: " . self::FLEEP_HOST . "\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($content) . "\r\n";
$header .= "\r\n";
return $header;
}
/**
* Builds the body of API call
*
* @param array $record
* @return string
*/
private function buildContent($record)
{
$dataArray = array(
'message' => $record['formatted']
);
$this->execCurl($this->curlOptions);
return http_build_query($dataArray);
}
/**
* Sends a new Curl request
*
* @param array $options Curl parameters, including the endpoint URL and POST payload
*/
protected function execCurl(array $options)
{
$curl = curl_init();
curl_setopt_array($curl, $options);
curl_exec($curl);
curl_close($curl);
}
/**
* Adds or overwrites a curl option
*
* @param array $options An assoc array of Curl options, indexed by CURL_* constants
* @return $this
*/
public function addCurlOptions(array $options)
{
$this->curlOptions = array_replace(
$this->curlOptions,
$options
);
return $this;
}
}