Skip to content

Commit

Permalink
Merge pull request #80 from nocive/cli_support
Browse files Browse the repository at this point in the history
Add basic command line support (#40)
  • Loading branch information
Crisfole committed Apr 21, 2016
2 parents f048273 + 20c0812 commit 91ab79e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/rollbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 . '/';

Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions tests/RollbarNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 91ab79e

Please sign in to comment.