Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 8 compatibility #18

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
],
"require": {
"php": ">=7.0.0",
"laravel/framework": "~5.5",
"encore/laravel-admin": "~1.5"
"laravel/framework": ">=5.5",
"encore/laravel-admin": ">=1.5",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
"laravel/laravel": "~5.5"
"phpunit/phpunit": ">=6.0",
"laravel/laravel": ">=5.5"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function up()

Schema::connection($connection)->create($table, function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->string('type', 255);
$table->string('code');
$table->string('message');
$table->string('file');
$table->string('message', 255);
$table->string('file', 255);
$table->integer('line');
$table->text('trace');
$table->string('method');
$table->string('path');
$table->string('path', 255);
$table->text('query');
$table->text('body');
$table->text('cookies');
Expand Down
3 changes: 2 additions & 1 deletion src/BootExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Encore\Admin\Reporter;

use Encore\Admin\Admin;
use Illuminate\Routing\Router;

trait BootExtension
{
Expand All @@ -23,7 +24,7 @@ public static function boot()
protected static function registerRoutes()
{
parent::routes(function ($router) {
/* @var \Illuminate\Routing\Router $router */
/* @var Router $router */
$router->resource('exceptions', 'Encore\Admin\Reporter\ExceptionController');
});
}
Expand Down
11 changes: 6 additions & 5 deletions src/ExceptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Reporter\Tracer\Parser;
use Illuminate\Http\JsonResponse;

class ExceptionController
{
Expand All @@ -17,7 +18,7 @@ class ExceptionController
*
* @return Content
*/
public function index()
public function index(): Content
{
return Admin::content(function (Content $content) {
$content->header('Exception');
Expand All @@ -27,7 +28,7 @@ public function index()
});
}

public function grid()
public function grid(): Grid
{
return Admin::grid(ExceptionModel::class, function (Grid $grid) {
$grid->model()->orderBy('id', 'desc');
Expand Down Expand Up @@ -76,7 +77,7 @@ public function grid()
$filter->disableIdFilter();
$filter->like('type');
$filter->like('message');
$filter->between("created_at")->datetime();
$filter->between('created_at')->datetime();
});

$grid->disableCreation();
Expand Down Expand Up @@ -112,11 +113,11 @@ public function show($id)
});
}

public function destroy($id)
public function destroy($id): JsonResponse
{
$ids = explode(',', $id);

if (ExceptionModel::whereIn('id', $ids)->delete()) {
if (ExceptionModel::query()->whereIn('id', $ids)->delete()) {
return response()->json([
'status' => true,
'message' => trans('admin.delete_succeeded'),
Expand Down
16 changes: 16 additions & 0 deletions src/ExceptionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ class ExceptionModel extends Model
'OPTIONS' => 'grey',
];

protected $fillable = [
'type',
'code',
'message',
'file',
'line',
'trace',
'method',
'path',
'query',
'body',
'cookies',
'headers',
'ip',
];

/**
* Settings constructor.
*
Expand Down
53 changes: 20 additions & 33 deletions src/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Encore\Admin\Extension;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Throwable;

class Reporter extends Extension
{
Expand All @@ -25,37 +27,37 @@ public function __construct(Request $request)
}

/**
* @param \Exception $exception
* @param Throwable $exception
*
* @return mixed
* @return void
*/
public static function report(\Exception $exception)
public static function report(Throwable $exception)
{
$reporter = new static(request());

return $reporter->reportException($exception);
$reporter->reportException($exception);
}

/**
* @param \Exception $exception
* @param Throwable $exception
*
* @return bool
* @return void
*/
public function reportException(\Exception $exception)
public function reportException(Throwable $exception)
{
$data = [

// Request info.
'method' => $this->request->getMethod(),
'ip' => $this->request->getClientIps(),
'path' => $this->request->path(),
'query' => array_except($this->request->all(), ['_pjax', '_token', '_method', '_previous_']),
'query' => Arr::except($this->request->all(), ['_pjax', '_token', '_method', '_previous_']),
'body' => $this->request->getContent(),
'cookies' => $this->request->cookies->all(),
'headers' => array_except($this->request->headers->all(), 'cookie'),
'headers' => Arr::except($this->request->headers->all(), 'cookie'),

// Exception info.
'exception' => get_class($exception),
'type' => get_class($exception),
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
Expand All @@ -67,7 +69,7 @@ public function reportException(\Exception $exception)

try {
$this->store($data);
} catch (\Exception $e) {
} catch (Throwable $e) {
// $result = $this->reportException($e);
}

Expand All @@ -81,7 +83,7 @@ public function reportException(\Exception $exception)
*
* @return array
*/
public function stringify($data)
public function stringify($data): array
{
return array_map(function ($item) {
return is_array($item) ? json_encode($item, JSON_OBJECT_AS_ARRAY) : (string) $item;
Expand All @@ -95,30 +97,15 @@ public function stringify($data)
*
* @return bool
*/
public function store(array $data)
public function store(array $data): bool
{
$exception = new ExceptionModel();

$exception->type = $data['exception'];
$exception->code = $data['code'];
$exception->message = $data['message'];
$exception->file = $data['file'];
$exception->line = $data['line'];
$exception->trace = $data['trace'];
$exception->method = $data['method'];
$exception->path = $data['path'];
$exception->query = $data['query'];
$exception->body = $data['body'];
$exception->cookies = $data['cookies'];
$exception->headers = $data['headers'];
$exception->ip = $data['ip'];

try {
$exception->save();
} catch (\Exception $e) {
ExceptionModel::query()->create($data);

return true;
} catch (Throwable $e) {
return false;
//dd($e);
}

return $exception->save();
}
}