From 3b899755e0c2a09c6af09b0d87ba7c589f6f6749 Mon Sep 17 00:00:00 2001 From: JakubKermes Date: Mon, 24 Jun 2024 22:06:15 +0200 Subject: [PATCH 1/7] Add helper --- src/Features/Traits/Authentication.php | 4 +++- src/Helpers/GetUserHelper.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/Helpers/GetUserHelper.php diff --git a/src/Features/Traits/Authentication.php b/src/Features/Traits/Authentication.php index a2a5994..1901987 100644 --- a/src/Features/Traits/Authentication.php +++ b/src/Features/Traits/Authentication.php @@ -5,6 +5,7 @@ namespace Blumilk\BLT\Features\Traits; use Behat\Gherkin\Node\TableNode; +use Blumilk\BLT\Helpers\GetUserHelper; use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Container\BindingResolutionException; use PHPUnit\Framework\Assert; @@ -21,7 +22,8 @@ trait Authentication public function userIsAuthenticatedInSessionAs(string $value, string $field): void { $auth = $this->getContainer()->make(Guard::class); - $auth->login($this->getUserModel()::query()->where($field, $value)->first()); + $user = GetUserHelper::getUser($field, $value); + $auth->login($user); } /** diff --git a/src/Helpers/GetUserHelper.php b/src/Helpers/GetUserHelper.php new file mode 100644 index 0000000..e9645ca --- /dev/null +++ b/src/Helpers/GetUserHelper.php @@ -0,0 +1,15 @@ +where($field, $value)->first(); + } +} From aa8d6e53ce4feb07d8d68ae748ad191ff3168c18 Mon Sep 17 00:00:00 2001 From: JakubKermes Date: Mon, 24 Jun 2024 22:10:37 +0200 Subject: [PATCH 2/7] csf --- src/Helpers/GetUserHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/GetUserHelper.php b/src/Helpers/GetUserHelper.php index e9645ca..945d9b2 100644 --- a/src/Helpers/GetUserHelper.php +++ b/src/Helpers/GetUserHelper.php @@ -6,7 +6,7 @@ class GetUserHelper { - public static function getUser( string $field,string $value): object + public static function getUser(string $field, string $value): object { $userClass = RecognizeClassHelper::recognizeObjectClass("User"); From 99c1d72142948d5f2dcb2ccd22b1609e49cb35b4 Mon Sep 17 00:00:00 2001 From: Jakub Kermes <114694305+JakubKermes@users.noreply.github.com> Date: Wed, 26 Jun 2024 15:08:32 +0200 Subject: [PATCH 3/7] Update src/Helpers/GetUserHelper.php Co-authored-by: Piotr Fedak <115737114+PiotrFedak@users.noreply.github.com> --- src/Helpers/GetUserHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/GetUserHelper.php b/src/Helpers/GetUserHelper.php index 945d9b2..9fd511e 100644 --- a/src/Helpers/GetUserHelper.php +++ b/src/Helpers/GetUserHelper.php @@ -6,7 +6,7 @@ class GetUserHelper { - public static function getUser(string $field, string $value): object + public static function getUser(string $field, string $value): ?object { $userClass = RecognizeClassHelper::recognizeObjectClass("User"); From a0db695b6195a045eaa20c4a6bf0a19ffb3c4e03 Mon Sep 17 00:00:00 2001 From: JakubKermes Date: Thu, 27 Jun 2024 07:24:13 +0200 Subject: [PATCH 4/7] add searching by email --- src/Features/Traits/Authentication.php | 4 ++-- src/Helpers/GetUserHelper.php | 15 --------------- src/Helpers/UserHelper.php | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 17 deletions(-) delete mode 100644 src/Helpers/GetUserHelper.php create mode 100644 src/Helpers/UserHelper.php diff --git a/src/Features/Traits/Authentication.php b/src/Features/Traits/Authentication.php index 1901987..6cfc812 100644 --- a/src/Features/Traits/Authentication.php +++ b/src/Features/Traits/Authentication.php @@ -5,7 +5,7 @@ namespace Blumilk\BLT\Features\Traits; use Behat\Gherkin\Node\TableNode; -use Blumilk\BLT\Helpers\GetUserHelper; +use Blumilk\BLT\Helpers\UserHelper; use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Container\BindingResolutionException; use PHPUnit\Framework\Assert; @@ -22,7 +22,7 @@ trait Authentication public function userIsAuthenticatedInSessionAs(string $value, string $field): void { $auth = $this->getContainer()->make(Guard::class); - $user = GetUserHelper::getUser($field, $value); + $user = UserHelper::getBy($field, $value); $auth->login($user); } diff --git a/src/Helpers/GetUserHelper.php b/src/Helpers/GetUserHelper.php deleted file mode 100644 index 9fd511e..0000000 --- a/src/Helpers/GetUserHelper.php +++ /dev/null @@ -1,15 +0,0 @@ -where($field, $value)->first(); - } -} diff --git a/src/Helpers/UserHelper.php b/src/Helpers/UserHelper.php new file mode 100644 index 0000000..83f626b --- /dev/null +++ b/src/Helpers/UserHelper.php @@ -0,0 +1,22 @@ +where($field, $value)->first(); + } + + public static function getByEmail(string $value): ?object + { + $userClass = RecognizeClassHelper::recognizeObjectClass("User"); + + return $userClass::query()->where("email", $value)->first(); + } +} From 82a2d5334b4f1b6989d948aa1e8034d1fc643fb1 Mon Sep 17 00:00:00 2001 From: JakubKermes Date: Thu, 27 Jun 2024 08:30:41 +0200 Subject: [PATCH 5/7] add user namespace to config --- config/blt.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/blt.php b/config/blt.php index c7bd6a7..d1372ce 100644 --- a/config/blt.php +++ b/config/blt.php @@ -5,6 +5,8 @@ return [ "namespaces" => [ "default" => "App\\", - "types" => [], + "types" => [ + "User" => "App\Models\\", + ], ], ]; From eeaac5ccfafe5a2882fe593080de0aa3167b3403 Mon Sep 17 00:00:00 2001 From: JakubKermes Date: Thu, 27 Jun 2024 09:03:35 +0200 Subject: [PATCH 6/7] update handling from config --- src/Helpers/RecognizeClassHelper.php | 31 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Helpers/RecognizeClassHelper.php b/src/Helpers/RecognizeClassHelper.php index 7bfc705..83e9fff 100644 --- a/src/Helpers/RecognizeClassHelper.php +++ b/src/Helpers/RecognizeClassHelper.php @@ -14,22 +14,26 @@ public static function recognizeObjectClass(string $objectName): string return $objectName; } - $objectName = Str::ucfirst(Str::singular($objectName)); + $objectName = Str::ucfirst($objectName); - return self::getObjectNamespace($objectName) . $objectName; + return self::getObjectNamespace(Str::singular($objectName)) . $objectName; } public static function getObjectNamespace(string $objectName): string { $type = self::guessType($objectName); - $typeNamespace = self::getNamespaceFromConfig($objectName, $type); + $typeNamespaces = config("blt.namespaces.types"); + + if (array_key_exists($objectName, $typeNamespaces)) { + return $typeNamespaces[$objectName]; + } - if ($typeNamespace) { - return $typeNamespace; + if (array_key_exists($type, $typeNamespaces)) { + return $typeNamespaces[$type]; } $type = Str::plural(Str::ucfirst($type)); - $defaultNamespace = config("blt.namespaces.default", "App\\"); + $defaultNamespace = config("blt.namespaces.default") ?? "App\\"; return $defaultNamespace . $type . "\\"; } @@ -39,18 +43,13 @@ public static function guessType(string $objectName): string $slug = Str::slug($objectName); foreach (TypesEnum::cases() as $objectType) { - if (Str::contains($slug, $objectType->value)) { - return Str::singular($objectType->value); + $objectTypeName = $objectType->value; + + if (Str::contains($slug, $objectTypeName)) { + return Str::singular($objectTypeName); } } - return "model"; - } - - protected static function getNamespaceFromConfig(string $objectName, string $type): ?string - { - $typeNamespaces = config("blt.namespaces.types"); - - return $typeNamespaces[$objectName] ?? $typeNamespaces[$type] ?? null; + return TypesEnum::Model->value; } } From 71547954892443e70e7bdd4f6ebe8ac395e77a63 Mon Sep 17 00:00:00 2001 From: JakubKermes Date: Fri, 28 Jun 2024 14:32:57 +0200 Subject: [PATCH 7/7] refactor RecognizeClassHelper.php --- config/blt.php | 2 +- src/Helpers/RecognizeClassHelper.php | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/config/blt.php b/config/blt.php index d1372ce..3bc43bf 100644 --- a/config/blt.php +++ b/config/blt.php @@ -6,7 +6,7 @@ "namespaces" => [ "default" => "App\\", "types" => [ - "User" => "App\Models\\", + "User" => "App\Models\User", ], ], ]; diff --git a/src/Helpers/RecognizeClassHelper.php b/src/Helpers/RecognizeClassHelper.php index 83e9fff..257f931 100644 --- a/src/Helpers/RecognizeClassHelper.php +++ b/src/Helpers/RecognizeClassHelper.php @@ -14,24 +14,25 @@ public static function recognizeObjectClass(string $objectName): string return $objectName; } - $objectName = Str::ucfirst($objectName); - - return self::getObjectNamespace(Str::singular($objectName)) . $objectName; - } - - public static function getObjectNamespace(string $objectName): string - { - $type = self::guessType($objectName); $typeNamespaces = config("blt.namespaces.types"); + $objectName = Str::lcfirst($objectName); if (array_key_exists($objectName, $typeNamespaces)) { return $typeNamespaces[$objectName]; } + $type = self::guessType($objectName); + $objectName = Str::ucfirst($objectName); + if (array_key_exists($type, $typeNamespaces)) { - return $typeNamespaces[$type]; + return $typeNamespaces[$type] . $objectName; } + return self::getObjectNamespace(Str::singular($objectName), $type) . $objectName; + } + + public static function getObjectNamespace(string $objectName, string $type): string + { $type = Str::plural(Str::ucfirst($type)); $defaultNamespace = config("blt.namespaces.default") ?? "App\\";