From 9ec57b29ea2a4ad8f4ae2c72d38b3c1defa34078 Mon Sep 17 00:00:00 2001 From: igwen6w Date: Tue, 16 Jan 2024 13:06:57 +0800 Subject: [PATCH] Configure the namespace of the policy file --- config/filament-shield.php | 2 ++ src/Commands/Concerns/CanGeneratePolicy.php | 4 ++-- src/Support/Utils.php | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config/filament-shield.php b/config/filament-shield.php index 09a6c37..7487bdb 100644 --- a/config/filament-shield.php +++ b/config/filament-shield.php @@ -58,6 +58,8 @@ 'generator' => [ 'option' => 'policies_and_permissions', 'policy_directory' => 'Policies', + // The namespace of the generated policy file will be App\Policies + 'namespace' => 'Policies', ], 'exclude' => [ diff --git a/src/Commands/Concerns/CanGeneratePolicy.php b/src/Commands/Concerns/CanGeneratePolicy.php index 166a93a..19ee769 100644 --- a/src/Commands/Concerns/CanGeneratePolicy.php +++ b/src/Commands/Concerns/CanGeneratePolicy.php @@ -62,8 +62,8 @@ protected function generatePolicyStubVariables(array $entity): array $path = $reflectionClass->getFileName(); $stubVariables['namespace'] = Str::of($path)->contains(['vendor', 'src']) - ? 'App\Policies' - : Str::of($namespace)->replace('Models', 'Policies'); /** @phpstan-ignore-line */ + ? 'App\\' . Utils::getGeneratorNamespace() + : Str::of($namespace)->replace('Models', Utils::getGeneratorNamespace()); /** @phpstan-ignore-line */ $stubVariables['model_name'] = $entity['model']; $stubVariables['model_fqcn'] = $namespace . '\\' . $entity['model']; $stubVariables['model_variable'] = Str::of($entity['model'])->camel(); diff --git a/src/Support/Utils.php b/src/Support/Utils.php index 7b12af5..da55090 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -149,6 +149,12 @@ public static function getGeneratorOption(): string return (string) config('filament-shield.generator.option', 'policies_and_permissions'); } + + public static function getGeneratorNamespace(): string + { + return (string) config('filament-shield.generator.namespace', 'Policies'); + } + public static function isGeneralExcludeEnabled(): bool { return (bool) config('filament-shield.exclude.enabled', true);