From 4f6e9c509071379f3192d901783e86016afce7e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Thu, 14 Mar 2024 09:40:45 +0100 Subject: [PATCH] cleanup --- src/Models/Notification.php | 19 ++-------- src/Support/Filters.php | 66 ---------------------------------- src/Traits/ResolvesActions.php | 7 ++-- src/Traits/ResolvesFields.php | 7 ++-- src/Traits/ResolvesFilters.php | 7 ++-- src/Traits/ResolvesWidgets.php | 5 +-- tests/TestCase.php | 7 ++-- 7 files changed, 15 insertions(+), 103 deletions(-) delete mode 100644 src/Support/Filters.php diff --git a/src/Models/Notification.php b/src/Models/Notification.php index f6b2cf3a..6e445194 100644 --- a/src/Models/Notification.php +++ b/src/Models/Notification.php @@ -4,7 +4,6 @@ use Cone\Root\Database\Factories\NotificationFactory; use Cone\Root\Interfaces\Models\Notification as Contract; -use Cone\Root\Support\Filters; use Cone\Root\Traits\InteractsWithProxy; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Concerns\HasUuids; @@ -69,11 +68,7 @@ protected function formattedCreatedAt(): Attribute { return new Attribute( get: function (): ?string { - return Filters::apply( - 'root:notification.formatted_created_at_attribute', - $this->created_at?->isoFormat('YYYY. MMMM DD. HH:mm'), - $this - ); + return $this->created_at?->isoFormat('YYYY. MMMM DD. HH:mm'); } ); } @@ -87,11 +82,7 @@ protected function isRead(): Attribute { return new Attribute( get: function (): bool { - return Filters::apply( - 'root:notification.is_read_attribute', - ! is_null($this->read_at), - $this - ); + return ! is_null($this->read_at); } ); } @@ -105,11 +96,7 @@ protected function url(): Attribute { return new Attribute( get: function (): ?string { - return Filters::apply( - 'root:notification.url_attribute', - $this->exists ? URL::route('root.api.notifications.update', $this) : null, - $this - ); + return $this->exists ? URL::route('root.api.notifications.update', $this) : null; } ); } diff --git a/src/Support/Filters.php b/src/Support/Filters.php deleted file mode 100644 index 70d8667d..00000000 --- a/src/Support/Filters.php +++ /dev/null @@ -1,66 +0,0 @@ - $callback, - 'priority' => $priority, - ]; - } - - /** - * Remove the given filter. - */ - public static function remove(string $hook, callable $callback, int $priority = 10): void - { - foreach (static::$callbacks[$hook] ?? [] as $key => $filter) { - if ($filter['callback'] === $callback && $filter['priority'] === $priority) { - unset(static::$callbacks[$hook][$key]); - } - } - } - - /** - * Apply the filters on the given hook and value. - */ - public static function apply(string $hook, mixed $value, ...$parameters): mixed - { - return array_reduce( - Arr::sort(static::$callbacks[$hook] ?? [], 'priority'), - static function (mixed $value, array $filter) use ($parameters): mixed { - return call_user_func_array($filter['callback'], [$value, ...$parameters]); - }, - $value - ); - } - - /** - * Flush the filters. - */ - public static function flush(): void - { - static::$callbacks = []; - } - - /** - * Get all the registered filters. - */ - public static function all(): array - { - return static::$callbacks; - } -} diff --git a/src/Traits/ResolvesActions.php b/src/Traits/ResolvesActions.php index 45222d9d..30e8fcd8 100644 --- a/src/Traits/ResolvesActions.php +++ b/src/Traits/ResolvesActions.php @@ -5,7 +5,6 @@ use Closure; use Cone\Root\Actions\Action; use Cone\Root\Actions\Actions; -use Cone\Root\Support\Filters; use Illuminate\Http\Request; use Illuminate\Support\Arr; @@ -45,16 +44,14 @@ public function withActions(Closure $callback): static public function resolveActions(Request $request): Actions { if (is_null($this->actions)) { - $actions = new Actions($this->actions($request)); + $this->actions = new Actions($this->actions($request)); - $actions->when(! is_null($this->actionsResolver), function (Actions $actions) use ($request): void { + $this->actions->when(! is_null($this->actionsResolver), function (Actions $actions) use ($request): void { $actions->register( Arr::wrap(call_user_func_array($this->actionsResolver, [$request])) ); }); - $this->actions = Filters::apply(static::class.'.actions', $actions, $this); - $this->actions->each(function (Action $action) use ($request): void { $this->resolveAction($request, $action); }); diff --git a/src/Traits/ResolvesFields.php b/src/Traits/ResolvesFields.php index 38231f59..cfb4ec94 100644 --- a/src/Traits/ResolvesFields.php +++ b/src/Traits/ResolvesFields.php @@ -7,7 +7,6 @@ use Cone\Root\Fields\Fields; use Cone\Root\Fields\File; use Cone\Root\Fields\Media; -use Cone\Root\Support\Filters; use Illuminate\Http\Request; use Illuminate\Support\Arr; @@ -61,16 +60,14 @@ public function hasFileField(Request $request): bool public function resolveFields(Request $request): Fields { if (is_null($this->fields)) { - $fields = new Fields($this->fields($request)); + $this->fields = new Fields($this->fields($request)); - $fields->when(! is_null($this->fieldsResolver), function (Fields $fields) use ($request): void { + $this->fields->when(! is_null($this->fieldsResolver), function (Fields $fields) use ($request): void { $fields->register( Arr::wrap(call_user_func_array($this->fieldsResolver, [$request])) ); }); - $this->fields = Filters::apply(static::class.'.fields', $fields, $this); - $this->fields->each(function (Field $field) use ($request): void { $this->resolveField($request, $field); }); diff --git a/src/Traits/ResolvesFilters.php b/src/Traits/ResolvesFilters.php index 743562a6..ee75c0de 100644 --- a/src/Traits/ResolvesFilters.php +++ b/src/Traits/ResolvesFilters.php @@ -5,7 +5,6 @@ use Closure; use Cone\Root\Filters\Filter; use Cone\Root\Filters\Filters; -use Cone\Root\Support\Filters as ValueFilters; use Illuminate\Http\Request; use Illuminate\Support\Arr; @@ -47,16 +46,14 @@ public function withFilters(Closure $callback): static public function resolveFilters(Request $request): Filters { if (is_null($this->filters)) { - $filters = new Filters($this->filters($request)); + $this->filters = new Filters($this->filters($request)); - $filters->when(! is_null($this->filtersResolver), function (Filters $filters) use ($request): void { + $this->filters->when(! is_null($this->filtersResolver), function (Filters $filters) use ($request): void { $filters->register( Arr::wrap(call_user_func_array($this->filtersResolver, [$request])) ); }); - $this->filters = ValueFilters::apply(static::class.'.filters', $filters, $this); - $this->filters->each(function (Filter $filter) use ($request): void { $this->resolveFilter($request, $filter); }); diff --git a/src/Traits/ResolvesWidgets.php b/src/Traits/ResolvesWidgets.php index 737f4e93..0e0ff9f5 100644 --- a/src/Traits/ResolvesWidgets.php +++ b/src/Traits/ResolvesWidgets.php @@ -2,7 +2,6 @@ namespace Cone\Root\Traits; -use Cone\Root\Support\Filters; use Cone\Root\Widgets\Widget; use Cone\Root\Widgets\Widgets; use Illuminate\Http\Request; @@ -28,9 +27,7 @@ public function widgets(Request $request): array public function resolveWidgets(Request $request): Widgets { if (is_null($this->widgets)) { - $widgets = new Widgets($this->widgets($request)); - - $this->widgets = Filters::apply(static::class.'.widgets', $widgets, $this); + $this->widgets = new Widgets($this->widgets($request)); $this->widgets->each(function (Widget $widget) use ($request): void { $this->resolveWidget($request, $widget); diff --git a/tests/TestCase.php b/tests/TestCase.php index 836e48b8..786b9122 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,7 @@ namespace Cone\Root\Tests; use Cone\Root\Interfaces\Models\User as UserInterface; +use Cone\Root\Root; use Cone\Root\Support\Filters; use Cone\Root\Tests\Resources\UserResource; use Illuminate\Contracts\Console\Kernel; @@ -21,6 +22,10 @@ public function createApplication(): Application $app->booting(static function () use ($app): void { $app->bind(UserInterface::class, User::class); + + Root::instance()->resources->register([ + new UserResource(), + ]); }); $app->make(Kernel::class)->bootstrap(); @@ -32,8 +37,6 @@ public function setUp(): void { parent::setUp(); - Filters::register('root:resources', fn (): array => [new UserResource()]); - $this->app['router']->getRoutes()->refreshNameLookups(); $this->startSession();