From 20c081259eef22576c0c563b342f956b778fc53a Mon Sep 17 00:00:00 2001 From: Jose' Pedro Saraiva Date: Mon, 11 Apr 2016 19:48:02 +0200 Subject: [PATCH] Add basic command line support (Fixes #40) --- src/rollbar.php | 19 ++++++++++++++++--- tests/RollbarNotifierTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/rollbar.php b/src/rollbar.php index d5f32a71..6a25f43a 100644 --- a/src/rollbar.php +++ b/src/rollbar.php @@ -122,6 +122,7 @@ class RollbarNotifier { 'scrub_fields', 'shift_function', 'timeout', 'report_suppressed', 'use_error_reporting', 'proxy'); // cached values for request/server/person data + private $_php_context = null; private $_request_data = null; private $_server_data = null; private $_person_data = null; @@ -260,7 +261,9 @@ protected function _report_exception(Exception $exc, $extra_data = null, $payloa } // request, server, person data - $data['request'] = $this->build_request_data(); + if ('http' === $this->_php_context) { + $data['request'] = $this->build_request_data(); + } $data['server'] = $this->build_server_data(); $data['person'] = $this->build_person_data(); @@ -746,6 +749,7 @@ protected function build_server_data() { } } $server_data['host'] = $this->host; + $server_data['argv'] = isset($_SERVER['argv']) ? $_SERVER['argv'] : null; if ($this->branch) { $server_data['branch'] = $this->branch; @@ -787,12 +791,17 @@ protected function build_person_data() { } protected function build_base_data($level = 'error') { + if (null === $this->_php_context) { + $this->_php_context = $this->get_php_context(); + } + $data = array( 'timestamp' => time(), 'environment' => $this->environment, 'level' => $level, 'language' => 'php', 'framework' => 'php', + 'php_context' => $this->_php_context, 'notifier' => array( 'name' => 'rollbar-php', 'version' => self::VERSION @@ -813,7 +822,7 @@ protected function build_payload($data) { ); if ($this->access_token) { - $payload['access_token'] = $this->access_token; + $payload['access_token'] = $this->access_token; } return $payload; @@ -892,6 +901,10 @@ protected function send_batch_blocking($batch) { $this->make_api_call('item_batch', $access_token, $post_data); } + protected function get_php_context() { + return php_sapi_name() === 'cli' || defined('STDIN') ? 'cli' : 'http'; + } + protected function make_api_call($action, $access_token, $post_data) { $url = $this->base_api_url . $action . '/'; @@ -919,7 +932,7 @@ protected function make_api_call($action, $access_token, $post_data) { } if ($this->_curl_ipresolve_supported) { - curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); } $result = curl_exec($ch); diff --git a/tests/RollbarNotifierTest.php b/tests/RollbarNotifierTest.php index 5935ada1..640d06d8 100644 --- a/tests/RollbarNotifierTest.php +++ b/tests/RollbarNotifierTest.php @@ -320,6 +320,33 @@ public function testMessageWithRequestData() { $this->assertEquals('example.com', $payload['data']['request']['headers']['Host']); } + public function testMessageWithCliData() { + $_SERVER = array( + 'REMOTE_ADDR' => '127.0.0.1', + 'argv' => array( + 'somescript', + '-vvvvv', + 'test:command', + '--arg=value', + ) + ); + $config = self::$simpleConfig; + + $notifier = m::mock('RollbarNotifier[send_payload]', array($config)) + ->shouldAllowMockingProtectedMethods(); + $notifier->shouldReceive('send_payload')->once() + ->with(m::on(function($input) use (&$payload) { + $payload = $input; + return true; + })); + + $notifier->report_message('Hello'); + + $this->assertEquals('127.0.0.1', $payload['data']['request']['user_ip']); + $this->assertEquals($_SERVER['argv'], $payload['data']['server']['argv']); + $this->assertEquals(php_sapi_name(), $payload['data']['php_context']); + } + public function testParamScrubbing() { $_GET = array( 'get_key' => 'get_value',