Skip to content

Commit

Permalink
Merge pull request #6 from TheoKouzelis/feature/airbrake-0.5.2
Browse files Browse the repository at this point in the history
Feature/airbrake 0.5.2
  • Loading branch information
TheoKouzelis authored Feb 6, 2018
2 parents 1709099 + f6a1d47 commit 2311f9d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
.php_cs.cache
composer.lock
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ This is a Laravel service provider for the latest Airbrake PHP package https://g

The service provider will configure an instance of Airbrake\Notifier with an ID, key and environment name.

The service provider will also filter out sensitive variables set in your project's .env file out of notifications sent to Airbrake. Any variable set in the .env file will appear with a value of "FILTERED" in the env tab of an Airbrake report.
```
"APP_KEY": "FILTERED",
```

## Install
Require via composer.
```
composer require kouz/laravel-airbrake
```
Add package to list of service providers in config/app.php
For Laravel >=5.5 the package will be discoverd. For Laravel <=5.4 add package to list of service providers in config/app.php
```
<?php
//config/app.php
Expand All @@ -27,6 +22,20 @@ Publish and fill out the config/airbrake.php file with your ID and key.
```
php artisan vendor:publish --provider="Kouz\Providers\AirbrakeServiceProvider"
```
## Config
The variables projectId and projectKey are required. Leave the rest empty to use Airbrake's default values.
```
'projectId' => '',
'projectKey' => '',
'environment' => env('APP_ENV', 'production'),
//leave the following options empty to use defaults
'appVersion' => '',
'host' => '',
'rootDirectory' => '',
'httpClient' => '',
```

## Basic Usage
### Exception Handler
Expand All @@ -41,17 +50,17 @@ of a Airbrake\Notifier object then pass a exception to the notify function.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @param \Exception $exception
* @return void
*/
public function report(Exception $e)
public function report(Exception $exception)
{
if ($this->shouldReport($e)) {
if ($this->shouldReport($exception)) {
$airbrakeNotifier = \App::make('Airbrake\Notifier');
$airbrakeNotifier->notify($e);
$airbrakeNotifier->notify($exception);
}
parent::report($e);
parent::report($exception);
}
```

Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["airbrake", "laravel"],
"require": {
"php": ">=5.4",
"airbrake/phpbrake": "^0.0.12",
"airbrake/phpbrake": "^0.5.2",
"illuminate/support": "5.*"
},
"license": "MIT",
Expand All @@ -20,5 +20,12 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
"Kouz\\Providers\\AirbrakeServiceProvider"
]
}
}
}
11 changes: 9 additions & 2 deletions config/airbrake.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

return [

'id' => '',
'projectId' => '',
'projectKey' => '',
'environment' => env('APP_ENV', 'production'),

'key' => '',
//leave the following options empty to use defaults

'appVersion' => '',
'host' => '',
'rootDirectory' => '',
'httpClient' => '',

];
56 changes: 5 additions & 51 deletions src/AirbrakeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Airbrake\Notifier;
use Illuminate\Contracts\Foundation\Application;

class AirbrakeHandler
class AirbrakeHandler
{
protected $app;

Expand All @@ -26,56 +26,10 @@ public function __construct(Application $app)
*/
public function handle()
{
$airbrake = new Notifier([
'projectId' => config('airbrake.id'),
'projectKey' => config('airbrake.key'),
]);
$options = collect(config('airbrake'))
->filter()
->toArray();

$airbrake->addFilter(function ($notice) {
$this->setEnvName($notice);

foreach ($this->getEnvKeys() as $envKey) {
$this->filterEnvKey($notice, $envKey);
}

return $notice;
});

return $airbrake;
}

protected function filterEnvKey(&$notice, $envKey)
{
if (isset($notice['environment'][$envKey])) {
$notice['environment'][$envKey] = 'FILTERED';
}
}

protected function getEnvFile()
{
$filePath = $this->app->environmentPath() . '/' . $this->app->environmentFile();

$envFile = @file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

return is_array($envFile) ? $envFile : [];
}

protected function getEnvKeyFromLine($envLine)
{
return trim(current(explode('=', $envLine)));
}

protected function getEnvKeys()
{
$envFile = $this->getEnvFile();

$envKeys = array_map([$this, 'getEnvKeyFromLine'], $envFile);

return $envKeys;
}

protected function setEnvName(&$notice)
{
$notice['context']['environment'] = env('APP_ENV');
return new Notifier($options);
}
}

0 comments on commit 2311f9d

Please sign in to comment.