From e14ce7c8ba4e7b4a1141f09762175ec712f3855c Mon Sep 17 00:00:00 2001 From: Kamisama Date: Tue, 10 Jul 2012 11:46:23 -0400 Subject: [PATCH] PSR-2 fixes and lazy connection --- src/Monolog/Handler/CubeHandler.php | 218 +++++++++++++++------------- 1 file changed, 116 insertions(+), 102 deletions(-) diff --git a/src/Monolog/Handler/CubeHandler.php b/src/Monolog/Handler/CubeHandler.php index 94420c20..5d910864 100644 --- a/src/Monolog/Handler/CubeHandler.php +++ b/src/Monolog/Handler/CubeHandler.php @@ -3,7 +3,7 @@ /* * This file is part of the Monolog package. * - * (c) Wan Chen + * (c) Jordi Boggiano * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -15,136 +15,150 @@ use Monolog\Logger; /** * Logs to Cube. - * Cube url : http://square.github.com/cube/ - * - * usage example: - * - * $log = new Logger('application'); - * // To use UDP - * $cube = new CubeHandler("udp://127.0.0.1:1180"); - * // To use HTTP - * $cube = new CubeHandler("http://localhost:1080"); - * $log->pushHandler($cube); - * - * Given host and port are cube's default + * @link http://square.github.com/cube/ * * @author Wan Chen */ class CubeHandler extends AbstractProcessingHandler { - private $socket = null; - private $curlConnection = null; - private $urlScheme = null; - private $acceptedUrlScheme = array('http', 'udp'); - - - /** - * Create a Cube handler - * - * @throws UnexpectedValueException when given url is not a valid url. - * A valid url must consists of three parts : protocol://host:port - * Only valid protocol used by Cube are http and udp - */ + private $udpConnection = null; + private $httpConnection = null; + + private $scheme = null; + private $host = null; + private $port = null; + private $acceptedScheme = array('http', 'udp'); + + + /** + * Create a Cube handler + * + * @throws UnexpectedValueException when given url is not a valid url. + * A valid url must consists of three parts : protocol://host:port + * Only valid protocol used by Cube are http and udp + */ public function __construct($url, $level = Logger::DEBUG, $bubble = true) { - $urlInfos = parse_url($url); - - if(!$urlInfos || !isset($urlInfos['scheme']) || !isset($urlInfos['host']) || !isset($urlInfos['port'])) - throw new \UnexpectedValueException('URL "'.$url.'" is not valid'); - - if (!in_array($urlInfos['scheme'], $this->acceptedUrlScheme)) - { - throw new \UnexpectedValueException('Invalid ' . $urlInfos['scheme'] . ' protocol. Valid options are ' . implode(', ', $this->acceptedUrlScheme)); - } - else - $this->urlScheme = $urlInfos['scheme']; - - switch($this->urlScheme) - { - case 'http' : $this->connectHttp($urlInfos['host'], $urlInfos['port']); break; - case 'udp' : $this->connectUdp($urlInfos['host'], $urlInfos['port']); break; - default; - } - + $urlInfos = parse_url($url); + + if (!$urlInfos || !isset($urlInfos['scheme']) + || !isset($urlInfos['host']) || !isset($urlInfos['port'])) { + throw new \UnexpectedValueException('URL "'.$url.'" is not valid'); + } + + if (!in_array($urlInfos['scheme'], $this->acceptedScheme)) { + throw new \UnexpectedValueException( + 'Invalid ' . $urlInfos['scheme'] . ' protocol.' + . 'Valid options are ' . implode(', ', $this->acceptedScheme)); + } else { + $this->scheme = $urlInfos['scheme']; + $this->host = $urlInfos['host']; + $this->port = $urlInfos['port']; + } + parent::__construct($level, $bubble); } + + /** + * Check if a connection resource is available + * + * @return boolean + */ + private function isConnected($scheme) + { + return $this->{$scheme . 'Connection'} !== null; + } + /** * Establish a connection to an UDP socket * * @throws LogicException when unable to connect to the socket */ - protected function connectUdp($host = '127.0.0.1', $port = 1180) + protected function connectUdp() { - if (!extension_loaded('sockets')) { - throw new \LogicException('The sockets extension is not loaded'); - } - - $this->socket = socket_create(AF_INET, SOCK_DGRAM, 0); - if (!$this->socket) - throw new \LogicException('Unable to create a socket'); - - $result = socket_connect($this->socket, $host, $port); - if (!$result) - { - throw new \LogicException('Unable to connect to the socket at ' . $host . ':' . $port); - } + if (!extension_loaded('sockets')) { + throw new \LogicException('The sockets extension is not loaded'); + } + + $this->udpConnection = socket_create(AF_INET, SOCK_DGRAM, 0); + if (!$this->udpConnection) { + throw new \LogicException('Unable to create a socket'); + } + + if (!socket_connect($this->udpConnection, $this->host, $this->port)) { + throw new \LogicException('Unable to connect to the socket at ' + . $this->host . ':' . $this->port); + } } - + /** * Establish a connection to a http server */ - protected function connectHttp($host = 'localhost', $port = 1080) + protected function connectHttp() { - $this->curlConnection = curl_init('http://'.$host.':'.$port.'/1.0/event/put'); - - if(!$this->curlConnection) - throw new \LogicException('Unable to connect to ' . $host . ':' . $port); - - curl_setopt($this->curlConnection, CURLOPT_CUSTOMREQUEST, "POST"); - curl_setopt($this->curlConnection, CURLOPT_RETURNTRANSFER, true); - + $this->httpConnection = + curl_init('http://'.$this->host.':'.$this->port.'/1.0/event/put'); + + if (!$this->httpConnection) { + throw new \LogicException('Unable to connect to ' + . $this->host . ':' . $this->port); + } + + curl_setopt($this->httpConnection, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($this->httpConnection, CURLOPT_RETURNTRANSFER, true); + } /** - * Send Event to Cube - * - * This assumes that the mandatory fields : - * - type is in $record['context']['type'] - * - data is $record['context'] + * {@inheritdoc} */ protected function write(array $record) { - $date = $record['datetime']; - - $datas = array( - 'type' => $record['context']['type'], - 'time' => $date->format('Y-m-d H:i:s') - ); - - unset($record['context']['type'], $record['datetime']); - - $datas['data'] = $record['context']; - $datas['data']['level'] = $record['level']; - - $datas = json_encode($datas); - - switch($this->urlScheme) - { - case 'http' : - curl_setopt($this->curlConnection, CURLOPT_POSTFIELDS, '['.$datas.']'); - curl_setopt($this->curlConnection, CURLOPT_HTTPHEADER, array( - 'Content-Type: application/json', - 'Content-Length: ' . strlen('['.$datas.']')) - ); - $result = curl_exec($this->curlConnection); - break; - case 'udp' : socket_send($this->socket, $datas, strlen($datas), 0); - } - - + $date = $record['datetime']; + + $datas = array('time' => $date->format('Y-m-d H:i:s')); + unset($record['datetime']); + + if (isset($record['context']['type'])) { + $datas['type'] = $record['context']['type']; + unset($record['context']['type']); + } + + $datas['data'] = $record['context']; + $datas['data']['level'] = $record['level']; + + call_user_func( + array($this, 'send'.ucwords($this->scheme)), json_encode($datas)); + } + + + private function sendUdp($datas) + { + if (!$this->isConnected($this->scheme)) { + call_user_func( + array($this, 'connect' . ucwords($this->scheme))); + } + + socket_send($this->udpConnection, $datas, strlen($datas), 0); + } + + + private function sendHttp($datas) + { + if (!$this->isConnected($this->scheme)) { + call_user_func( + array($this, 'connect' . ucwords($this->scheme))); + } + + curl_setopt($this->httpConnection, CURLOPT_POSTFIELDS, '['.$datas.']'); + curl_setopt($this->httpConnection, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'Content-Length: ' . strlen('['.$datas.']')) + ); + return curl_exec($this->httpConnection); } } \ No newline at end of file