From b95a2bcdfd2fba323daf44eea29afa165f39c747 Mon Sep 17 00:00:00 2001 From: Manwe Date: Thu, 14 Mar 2019 15:48:50 +0100 Subject: [PATCH] Split messaging layer from logger layer into 2nd package biteit/simple-slack-messenger --- composer.json | 5 ++- src/BiteIT/TracySlackLogger.php | 74 ++++++++------------------------- 2 files changed, 20 insertions(+), 59 deletions(-) diff --git a/composer.json b/composer.json index 43c7ade..1f30a6f 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "biteit/tracy-slack-logger", "description": "Integrates Tracy Logger to your Slack", "minimum-stability": "dev", - "version": "0.3.0", + "version": "0.4.0", "license": "MIT", "authors": [ { @@ -12,7 +12,8 @@ ], "require": { "tracy/tracy": "2.4.10", - "php": ">=5.6.0" + "php": ">=5.6.0", + "biteit/simple-slack-messenger": "dev-master" }, "autoload": { "psr-0": { diff --git a/src/BiteIT/TracySlackLogger.php b/src/BiteIT/TracySlackLogger.php index 3f82a4e..c01873d 100644 --- a/src/BiteIT/TracySlackLogger.php +++ b/src/BiteIT/TracySlackLogger.php @@ -14,11 +14,15 @@ class TracySlackLogger extends Logger { protected $slackUsername; protected $useFileLoggerAsWell = true; + protected $messenger = null; + protected $message = null; + public function __construct($webhookURL, $useDefaultLogger = false) { parent::__construct(Debugger::$logDirectory, Debugger::$email, Debugger::getBlueScreen()); - $this->slackWebhookURL = $webhookURL; + $this->messenger = new SimpleSlackMessenger($webhookURL); $this->useFileLoggerAsWell = $useDefaultLogger; + $this->message = new SimpleSlackMessage(); } public function addReportingLevel($level){ @@ -40,8 +44,8 @@ public function setReportingLevels($levels){ * @return $this */ public function setSlackIconURL($slackIconURL){ - $this->slackIconEmoji = null; - $this->slackIconURL = $slackIconURL; + $this->message->setIconEmoji(null); + $this->message->setIconUrl($slackIconURL); return $this; } @@ -50,8 +54,8 @@ public function setSlackIconURL($slackIconURL){ * @return $this */ public function setSlackIconEmoji($emoji){ - $this->slackIconEmoji = $emoji; - $this->slackIconURL = null; + $this->message->setIconEmoji($emoji); + $this->message->setIconUrl(null); return $this; } @@ -60,7 +64,7 @@ public function setSlackIconEmoji($emoji){ * @return $this */ public function setSlackUsername($slackUsername){ - $this->slackUsername = $slackUsername; + $this->message->setName($slackUsername); return $this; } @@ -86,60 +90,16 @@ function log($value, $priority = self::INFO) $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; - $payload['text'] = "*{$priority}* on *{$_SERVER['HTTP_HOST']}* (URL: $url): $value"; - if($this->slackUsername) - $payload['username'] = $this->slackUsername; - if($this->slackIconURL) - $payload['icon_url'] = $this->slackIconURL; - if($this->slackIconEmoji) - $payload['icon_emoji'] = $this->slackIconEmoji; - - $message = array( - 'payload' => json_encode($payload) - ); - // Use curl to send your message - try { - if (ini_get('allow_url_fopen')) { - $result = $this->sendByFileGetContents($message); - } else { - $result = $this->sendByCurl($message); - } + $text = "*{$priority}* on *{$_SERVER['HTTP_HOST']}* (URL: $url): $value"; + $this->message->setText($text); + + try{ + $this->messenger->sendSimpleMessage($this->message); } - catch (\Exception $e){ - echo 'Unable to use either file_get_conents nor curl'; + catch (SimpleSlackException $exception){ + throw $exception; } - - } - - protected function sendByCurl($message){ - $c = curl_init($this->slackWebhookURL); - curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($c, CURLOPT_POST, true); - curl_setopt($c, CURLOPT_POSTFIELDS, $message); - curl_setopt($c, CURLOPT_RETURNTRANSFER, true); - $result = curl_exec($c); - curl_close($c); - - return $result; } - protected function sendByFileGetContents($message){ - $postdata = http_build_query( - $message - ); - - $opts = array('http' => - array( - 'method' => 'POST', - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'content' => $postdata - ) - ); - - $context = stream_context_create($opts); - - $result = file_get_contents($this->slackWebhookURL, false, $context); - return $result; - } } \ No newline at end of file