Skip to content

Commit 1e57c31

Browse files
committed
initial commit
0 parents  commit 1e57c31

File tree

11 files changed

+632
-0
lines changed

11 files changed

+632
-0
lines changed

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "larvabug/larvabug-laravel",
3+
"description": "Larvabug monitoring",
4+
"require": {
5+
"php": ">=7.1",
6+
"illuminate/support": "5.0 - 5.8",
7+
"ext-curl": "*"
8+
},
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Larvabug",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"LarvaBug\\": "src/"
19+
}
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"LarvaBug\\Provider\\ServiceProvider"
25+
],
26+
"aliases": {
27+
"LarvaBug": "LarvaBug\\Facade"
28+
}
29+
}
30+
},
31+
"minimum-stability": "dev"
32+
}

config/larvabug.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
return [
4+
5+
'project_id' => env('LB_PROJECT_ID',''),
6+
7+
'project_secret' => env('LB_SECRET',''),
8+
9+
'environment' => ['production','local'],
10+
11+
'skip_errors' => [
12+
'\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class'
13+
],
14+
15+
'blacklist' => [
16+
'password'
17+
]
18+
19+
];

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

src/Client/HttpClient.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
4+
namespace LarvaBug\Client;
5+
6+
7+
use Illuminate\Support\Facades\Session;
8+
9+
class HttpClient
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private $projectId;
15+
/**
16+
* @var string
17+
*/
18+
private $projectSecret;
19+
20+
private const URL = 'http://larvabug.local/api/v1/exception';
21+
22+
/**
23+
* @param string $projectId
24+
* @param string $projectSecret
25+
*/
26+
public function __construct(string $projectId, string $projectSecret)
27+
{
28+
$this->projectId = $projectId;
29+
$this->projectSecret = $projectSecret;
30+
}
31+
32+
/**
33+
* Report error to larvabug website
34+
*
35+
* @param $exception
36+
*/
37+
public function report($exceptionData)
38+
{
39+
try {
40+
$data_string = json_encode($exceptionData);
41+
42+
$header = [
43+
'Content-Type:application/json',
44+
'Authorization-APP:' . $this->projectId,
45+
'Authorization-KEY:' . $this->projectSecret
46+
];
47+
48+
$ch = curl_init(self::URL);
49+
50+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
51+
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
52+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53+
54+
$result = curl_exec($ch);
55+
56+
curl_close($ch);
57+
58+
if ($result && $result != 404){
59+
Session::put('lb.lastExceptionId', $result);
60+
}
61+
62+
return true;
63+
}catch (\Exception $exception) {
64+
return false;
65+
}
66+
}
67+
68+
}

src/Commands/LarvaBugTestCommand.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace LarvaBug\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class LarvaBugTestCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'larvabug:test';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Larvabug test command to check configurations, and send test exception';
22+
23+
/**
24+
* Create a new command instance.
25+
*
26+
* @return void
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
}
32+
33+
/**
34+
* Execute the console command.
35+
*
36+
* @return mixed
37+
*/
38+
public function handle()
39+
{
40+
$this->info('Testing LarvaBug Configurations');
41+
42+
if (config('larvabug.project_id')){
43+
$this->info('1. ✓ [Larvabug] Found project id');
44+
}else{
45+
$this->info('1. ✗ [Larvabug] Could not find your project id, please set this in your .env');
46+
}
47+
48+
if (config('larvabug.project_secret')){
49+
$this->info('2. ✓ [Larvabug] Found secret key');
50+
}else{
51+
$this->info('2. ✗ [Larvabug] Could not find LarvaBug secret, please set this in your .env');
52+
}
53+
54+
55+
}
56+
57+
public function testCredentials()
58+
{
59+
60+
}
61+
}

src/Facade/LarvaBug.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
4+
namespace LarvaBug\Facade;
5+
6+
7+
use Illuminate\Support\Facades\Facade as FacadeAlias;
8+
9+
class LarvaBug extends FacadeAlias
10+
{
11+
protected static function getFacadeAccessor()
12+
{
13+
return 'larvabug';
14+
}
15+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
4+
namespace LarvaBug\Handler;
5+
6+
use Illuminate\Support\Facades\App;
7+
use LarvaBug\Client\HttpClient;
8+
9+
class LarvaBugExceptionHandler
10+
{
11+
/**
12+
* @var HttpClient
13+
*/
14+
private $client;
15+
/**
16+
* @var PrepareExceptionData
17+
*/
18+
private $exceptionData;
19+
20+
/**
21+
* LarvaBugExceptionHandler constructor.
22+
* @param HttpClient $client
23+
* @param PrepareExceptionData $exceptionData
24+
*/
25+
public function __construct(
26+
HttpClient $client,
27+
PrepareExceptionData $exceptionData
28+
)
29+
{
30+
$this->client = $client;
31+
$this->exceptionData = $exceptionData;
32+
}
33+
34+
public function handle(\Throwable $exception)
35+
{
36+
if (!$this->checkAppEnvironment()){
37+
return false;
38+
}
39+
40+
if ($this->skipError(get_class($exception))){
41+
return false;
42+
}
43+
44+
$data = $this->exceptionData->prepare($exception);
45+
46+
$this->client->report($data);
47+
48+
return true;
49+
}
50+
51+
/**
52+
* Check if larvabug environment configurations match with app environment
53+
*
54+
* @return bool
55+
*/
56+
public function checkAppEnvironment()
57+
{
58+
if (!config('larvabug.environment')){
59+
return false;
60+
}
61+
62+
if (is_array(config('larvabug.environment'))){
63+
if (count(config('larvabug.environment')) == 0){
64+
return false;
65+
}
66+
67+
if (in_array(App::environment(),config('larvabug.environment'))){
68+
return true;
69+
}
70+
}
71+
72+
return false;
73+
}
74+
75+
protected function skipError($class)
76+
{
77+
if (in_array($class,config('larvabug.skip_errors'))){
78+
return true;
79+
}
80+
81+
return false;
82+
}
83+
}

0 commit comments

Comments
 (0)