diff --git a/composer.json b/composer.json index 0c40447..b449688 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/database/migrations/2017_07_17_040159_create_exceptions_table.php b/database/migrations/2017_07_17_040159_create_exceptions_table.php index 4a6f088..3f5b5df 100644 --- a/database/migrations/2017_07_17_040159_create_exceptions_table.php +++ b/database/migrations/2017_07_17_040159_create_exceptions_table.php @@ -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'); diff --git a/src/BootExtension.php b/src/BootExtension.php index 0295bab..40a01ab 100644 --- a/src/BootExtension.php +++ b/src/BootExtension.php @@ -3,6 +3,7 @@ namespace Encore\Admin\Reporter; use Encore\Admin\Admin; +use Illuminate\Routing\Router; trait BootExtension { @@ -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'); }); } diff --git a/src/ExceptionController.php b/src/ExceptionController.php index 32872bf..7bb5f68 100644 --- a/src/ExceptionController.php +++ b/src/ExceptionController.php @@ -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 { @@ -17,7 +18,7 @@ class ExceptionController * * @return Content */ - public function index() + public function index(): Content { return Admin::content(function (Content $content) { $content->header('Exception'); @@ -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'); @@ -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(); @@ -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'), diff --git a/src/ExceptionModel.php b/src/ExceptionModel.php index d3c1e82..7b5cc96 100644 --- a/src/ExceptionModel.php +++ b/src/ExceptionModel.php @@ -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. * diff --git a/src/Reporter.php b/src/Reporter.php index fe8f94b..2a0aec8 100644 --- a/src/Reporter.php +++ b/src/Reporter.php @@ -4,6 +4,8 @@ use Encore\Admin\Extension; use Illuminate\Http\Request; +use Illuminate\Support\Arr; +use Throwable; class Reporter extends Extension { @@ -25,23 +27,23 @@ 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 = [ @@ -49,13 +51,13 @@ public function reportException(\Exception $exception) '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(), @@ -67,7 +69,7 @@ public function reportException(\Exception $exception) try { $this->store($data); - } catch (\Exception $e) { + } catch (Throwable $e) { // $result = $this->reportException($e); } @@ -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; @@ -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(); } }