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

Add Custom Model Support & Fix Iterator Errors #46

Merged
merged 12 commits into from
Jan 24, 2024
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/pest
run: vendor/bin/pest
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,42 @@ class Handler extends ExceptionHandler

## Configuration
The configuration file filament-exceptions.php is automatically published into your config directory.
You can change icons and navigations settings as well as the active pill and slug there.

* **Mass Pruning**: By default exceptions older than a week are scheduled to be pruned daily. You can change the `period` by providing a date in the config or using carbon.
> **Note**
> in order for the schedule to work you need to make sure that you have configured your server if not follow this link on how to configure it. [Running The Scheduler](https://laravel.com/docs/9.x/scheduling#running-the-scheduler)
The config file provides you with multiple options to customize the plugin.

### Mass Pruning
By default Filament Exceptions is configured to prune exceptions older than 1 week.

To modify how long you'd like to store records for you can supply a Carbon object like so

```php
'period' => now()->subWeek(), // 1 week
'period' => now()->subDay(), // 1 day
'period' => now()->subDays(3), // 3 days
```
> **Note** This requires laravel scheduler to be setup and configured in order to work. You can see how to do that here [Running The Scheduler](https://laravel.com/docs/10.x/scheduling#running-the-scheduler)

### Custom Exception Model
For those who need to change the model this is possible using the configuration file.

```php
'exception_model' => Exception::class,
```

When creating your new exception model you should extend the default model

```php
<?php

namespace App\Models;

use BezhanSalleh\FilamentExceptions\Models\Exception as BaseException;

class Exception extends BaseException
{

}
```

## Theme
By default the plugin uses the default theme of Filamentphp, but if you are using a custom theme then include the plugins view path into the content array of your tailwind.config.js file:
Expand Down
40 changes: 22 additions & 18 deletions config/filament-exceptions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

use BezhanSalleh\FilamentExceptions\Models\Exception;

return [

'exception_model' => Exception::class,

'slug' => 'exceptions',

/** Show or hide in navigation/sidebar */
Expand Down Expand Up @@ -29,27 +33,27 @@
'is_globally_searchable' => false,

/**-------------------------------------------------
* Change the default active tab
*
* Exception => 1 (Default)
* Headers => 2
* Cookies => 3
* Body => 4
* Queries => 5
*/
* Change the default active tab
*
* Exception => 1 (Default)
* Headers => 2
* Cookies => 3
* Body => 4
* Queries => 5
*/
'active_tab' => 5,

/**-------------------------------------------------
* Here you can define when the exceptions should be pruned
* The default is 7 days (a week)
* The format for providing period should follow carbon's format. i.e.
* 1 day => 'subDay()',
* 3 days => 'subDays(3)',
* 7 days => 'subWeek()',
* 1 month => 'subMonth()',
* 2 months => 'subMonths(2)',
*
*/
* Here you can define when the exceptions should be pruned
* The default is 7 days (a week)
* The format for providing period should follow carbon's format. i.e.
* 1 day => 'subDay()',
* 3 days => 'subDays(3)',
* 7 days => 'subWeek()',
* 1 month => 'subMonth()',
* 2 months => 'subMonths(2)',
*
*/

'period' => now()->subWeek(),
];
32 changes: 17 additions & 15 deletions src/FilamentExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace BezhanSalleh\FilamentExceptions;

use BezhanSalleh\FilamentExceptions\Models\Exception;

use BezhanSalleh\FilamentExceptions\Models\Exception as ExceptionModel;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Spatie\LaravelIgnition\Recorders\QueryRecorder\QueryRecorder;
use Throwable;

Expand Down Expand Up @@ -37,6 +37,14 @@ public static function report(Throwable $exception)
$reporter->reportException($exception);
}

public static function model() : string
{
if (config('filament-exceptions.exception_model') === null) {
return ExceptionModel::class;
}
return config('filament-exceptions.exception_model');
}

/**
* @return void
*/
Expand All @@ -63,15 +71,16 @@ public function reportException(Throwable $exception)

try {
$this->store($data);
} catch (Throwable $e) {
}
catch (Throwable $e) {
throw $e;
}
}

/**
* Convert all items to string.
*/
public function stringify($data): array
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 @@ -81,23 +90,16 @@ public function stringify($data): array
/**
* Store exception info to db.
*/
public function store(array $data): bool
public function store(array $data) : bool
{
try {
Exception::query()->create($data);
$this->model()::query()->create($data);

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

public static function formatFileName(string $fileName): string
{
return str($fileName)
->after(str(request()->getHost())->beforeLast('.')->toString())
->afterLast('/')
->prepend('.../')
->toString();
}
}
84 changes: 44 additions & 40 deletions src/Resources/ExceptionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace BezhanSalleh\FilamentExceptions\Resources;

use BezhanSalleh\FilamentExceptions\Models\Exception;
use BezhanSalleh\FilamentExceptions\Facades\FilamentExceptions;
use BezhanSalleh\FilamentExceptions\Resources\ExceptionResource\Pages;
use Filament\Forms;
use Filament\Forms\Form;
Expand All @@ -12,53 +12,57 @@

class ExceptionResource extends Resource
{
protected static ?string $model = Exception::class;

public static function getModelLabel(): string
public static function getModel() : string
{
return FilamentExceptions::model();
}

public static function getModelLabel() : string
{
return __('filament-exceptions::filament-exceptions.labels.model');
}

public static function getPluralModelLabel(): string
public static function getPluralModelLabel() : string
{
return __('filament-exceptions::filament-exceptions.labels.model_plural');
}

public static function getNavigationGroup(): ?string
public static function getNavigationGroup() : ?string
{
return __('filament-exceptions::filament-exceptions.labels.navigation_group');
}

public static function getNavigationLabel(): string
public static function getNavigationLabel() : string
{
return __('filament-exceptions::filament-exceptions.labels.navigation');
}

public static function getNavigationIcon(): string
public static function getNavigationIcon() : string
{
return config('filament-exceptions.icons.navigation');
}

public static function getSlug(): string
public static function getSlug() : string
{
return config('filament-exceptions.slug');
}

public static function getNavigationBadge(): ?string
public static function getNavigationBadge() : ?string
{
if (config('filament-exceptions.navigation_badge')) {
return static::$model::count();
return static::getModel()::count();
}

return null;
}

public static function shouldRegisterNavigation(): bool
public static function shouldRegisterNavigation() : bool
{
return (bool) config('filament-exceptions.navigation_enabled');
}

public static function getNavigationSort(): ?int
public static function getNavigationSort() : ?int
{
return config('filament-exceptions.navigation_sort');
}
Expand All @@ -75,41 +79,41 @@ public static function canGloballySearch(): bool
&& static::canViewAny();
}

public static function form(Form $form): Form
public static function form(Form $form) : Form
{
return $form
->schema([
Forms\Components\Tabs::make('Heading')
->activeTab(static fn (): int => config('filament-exceptions.active_tab'))
->activeTab(static fn () : int => config('filament-exceptions.active_tab'))
->tabs([
Forms\Components\Tabs\Tab::make('Exception')
->label(static fn (): string => __('filament-exceptions::filament-exceptions.labels.tabs.exception'))
->icon(static fn (): string => config('filament-exceptions.icons.exception'))
->label(static fn () : string => __('filament-exceptions::filament-exceptions.labels.tabs.exception'))
->icon(static fn () : string => config('filament-exceptions.icons.exception'))
->schema([
Forms\Components\View::make('filament-exceptions::exception'),
]),
Forms\Components\Tabs\Tab::make('Headers')
->label(static fn (): string => __('filament-exceptions::filament-exceptions.labels.tabs.headers'))
->icon(static fn (): string => config('filament-exceptions.icons.headers'))
->label(static fn () : string => __('filament-exceptions::filament-exceptions.labels.tabs.headers'))
->icon(static fn () : string => config('filament-exceptions.icons.headers'))
->schema([
Forms\Components\View::make('filament-exceptions::headers'),
])->columns(1),
Forms\Components\Tabs\Tab::make('Cookies')
->label(static fn (): string => __('filament-exceptions::filament-exceptions.labels.tabs.cookies'))
->icon(static fn (): string => config('filament-exceptions.icons.cookies'))
->label(static fn () : string => __('filament-exceptions::filament-exceptions.labels.tabs.cookies'))
->icon(static fn () : string => config('filament-exceptions.icons.cookies'))
->schema([
Forms\Components\View::make('filament-exceptions::cookies'),
]),
Forms\Components\Tabs\Tab::make('Body')
->label(static fn (): string => __('filament-exceptions::filament-exceptions.labels.tabs.body'))
->icon(static fn (): string => config('filament-exceptions.icons.body'))
->label(static fn () : string => __('filament-exceptions::filament-exceptions.labels.tabs.body'))
->icon(static fn () : string => config('filament-exceptions.icons.body'))
->schema([
Forms\Components\View::make('filament-exceptions::body'),
]),
Forms\Components\Tabs\Tab::make('Queries')
->label(static fn (): string => __('filament-exceptions::filament-exceptions.labels.tabs.queries'))
->icon(static fn (): string => config('filament-exceptions.icons.queries'))
->badge(static fn ($record): string => collect(json_decode($record->query, true, 512, JSON_THROW_ON_ERROR))->count())
->label(static fn () : string => __('filament-exceptions::filament-exceptions.labels.tabs.queries'))
->icon(static fn () : string => config('filament-exceptions.icons.queries'))
->badge(static fn ($record) : string => collect(json_decode($record->query, true, 512, JSON_THROW_ON_ERROR))->count())
->schema([
Forms\Components\View::make('filament-exceptions::query'),
]),
Expand All @@ -118,46 +122,46 @@ public static function form(Form $form): Form
])->columns(1);
}

public static function table(Table $table): Table
public static function table(Table $table) : Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('method')
->label(fn (): string => __('filament-exceptions::filament-exceptions.columns.method'))
->label(fn () : string => __('filament-exceptions::filament-exceptions.columns.method'))
->badge()
->colors([
'gray',
'success' => fn ($state): bool => $state === 'GET',
'primary' => fn ($state): bool => $state === 'POST',
'warning' => fn ($state): bool => $state === 'PUT',
'danger' => fn ($state): bool => $state === 'DELETE',
'warning' => fn ($state): bool => $state === 'PATCH',
'gray' => fn ($state): bool => $state === 'OPTIONS',
'success' => fn ($state) : bool => $state === 'GET',
'primary' => fn ($state) : bool => $state === 'POST',
'warning' => fn ($state) : bool => $state === 'PUT',
'danger' => fn ($state) : bool => $state === 'DELETE',
'warning' => fn ($state) : bool => $state === 'PATCH',
'gray' => fn ($state) : bool => $state === 'OPTIONS',

])
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('path')
->label(fn (): string => __('filament-exceptions::filament-exceptions.columns.path'))
->label(fn () : string => __('filament-exceptions::filament-exceptions.columns.path'))
->searchable(),
Tables\Columns\TextColumn::make('type')
->label(fn (): string => __('filament-exceptions::filament-exceptions.columns.type'))
->label(fn () : string => __('filament-exceptions::filament-exceptions.columns.type'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('code')
->label(fn (): string => __('filament-exceptions::filament-exceptions.columns.code'))
->label(fn () : string => __('filament-exceptions::filament-exceptions.columns.code'))
->searchable()
->sortable()
->toggleable(isToggledHiddenByDefault: false),
Tables\Columns\TextColumn::make('ip')
->label(fn (): string => __('filament-exceptions::filament-exceptions.columns.ip'))
->label(fn () : string => __('filament-exceptions::filament-exceptions.columns.ip'))
->badge()
->extraAttributes(['class' => 'font-mono'])
->sortable()
->searchable()
->toggleable(isToggledHiddenByDefault: false),
Tables\Columns\TextColumn::make('created_at')
->label(fn (): string => __('filament-exceptions::filament-exceptions.columns.occurred_at'))
->label(fn () : string => __('filament-exceptions::filament-exceptions.columns.occurred_at'))
->sortable()
->searchable()
->dateTime()
Expand All @@ -178,14 +182,14 @@ public static function table(Table $table): Table
->defaultSort('created_at', 'desc');
}

public static function getRelations(): array
public static function getRelations() : array
{
return [
//
];
}

public static function getPages(): array
public static function getPages() : array
{
return [
'index' => Pages\ListExceptions::route('/'),
Expand Down
Loading
Loading