diff --git a/src/rollbar.php b/src/rollbar.php index 313aa6d3..54a44694 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(); @@ -747,6 +750,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; @@ -788,12 +792,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 @@ -814,7 +823,7 @@ protected function build_payload($data) { ); if ($this->access_token) { - $payload['access_token'] = $this->access_token; + $payload['access_token'] = $this->access_token; } return $payload; @@ -893,6 +902,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 . '/'; @@ -920,7 +933,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',