diff --git a/src/Support/Config.php b/src/Support/Config.php index 0f16692..6c4b696 100644 --- a/src/Support/Config.php +++ b/src/Support/Config.php @@ -4,8 +4,22 @@ class Config { + /** + * @var callable|null + */ + protected static $callback; + + public static function resolvePrunableModelsUsing(?callable $callback): void + { + static::$callback = $callback; + } + public static function getPrunableModels(): array { + if (is_callable(static::$callback)) { + return call_user_func(static::$callback); + } + return config('prunable-fields.models', []); } } diff --git a/tests/PrunableFieldsTest.php b/tests/PrunableFieldsTest.php index 2730a1d..cb33689 100644 --- a/tests/PrunableFieldsTest.php +++ b/tests/PrunableFieldsTest.php @@ -3,6 +3,7 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Log; use Maize\PrunableFields\Events\ModelsFieldsPruned; +use Maize\PrunableFields\Support\Config; use Maize\PrunableFields\Tests\Events\UserUpdatedEvent; use Maize\PrunableFields\Tests\Models\PrunableUser; @@ -67,3 +68,18 @@ fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === PrunableUser::class ); })->with('user_with_prunable_fields'); + +test('should allow the prunable models to be overridden at runtime', function (PrunableUser $model) { + Config::resolvePrunableModelsUsing(fn () => [PrunableUser::class]); + + Event::fake(); + + pruneFields([]); + + Event::assertDispatched( + ModelsFieldsPruned::class, + fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === PrunableUser::class + ); + + Config::resolvePrunableModelsUsing(null); +})->with('user_with_prunable_fields');