From 1c663bbaf7aea9b3807b746c9a7cff8b6efd5d50 Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Sun, 13 Jul 2025 14:28:56 +0300 Subject: [PATCH 1/4] refactor: use AppTypeEnum --- .../views/clerk_user_repository.blade.php | 20 +++++++ src/Commands/InitCommand.php | 58 ++++++++++++------- src/Enums/AppTypeEnum.php | 14 +++++ 3 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 resources/views/clerk_user_repository.blade.php create mode 100644 src/Enums/AppTypeEnum.php diff --git a/resources/views/clerk_user_repository.blade.php b/resources/views/clerk_user_repository.blade.php new file mode 100644 index 0000000..4e8954f --- /dev/null +++ b/resources/views/clerk_user_repository.blade.php @@ -0,0 +1,20 @@ +namespace App\Support\Clerk; + +use App\Models\Role; +use App\Models\User; +use Lcobucci\JWT\Token; +use RonasIT\Clerk\Repositories\UserRepository; + +class ClerkUserRepository extends UserRepository +{ + public function fromToken(Token $token): User + { + $user = parent::fromToken($token); + + return User::firstOrCreate([ + 'clerk_id' => $user->getAuthIdentifier(), + ], [ + 'role_id' => Role::USER, + ]); + } +} \ No newline at end of file diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index e3c3ea9..8921bc1 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -13,6 +13,7 @@ use RonasIT\ProjectInitializator\Enums\AuthTypeEnum; use RonasIT\ProjectInitializator\Enums\RoleEnum; use Winter\LaravelConfigWriter\ArrayFile; +use RonasIT\ProjectInitializator\Enums\AppTypeEnum; class InitCommand extends Command implements Isolatable { @@ -69,6 +70,8 @@ class InitCommand extends Command implements Isolatable protected string $appName; + protected AppTypeEnum $appType; + public function handle(): void { $this->prepareAppName(); @@ -80,7 +83,7 @@ public function handle(): void field: 'email of code owner / team lead', rules: 'required|email', ); - + $this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$kebabName}.com"); $envFile = (file_exists('.env')) ? '.env' : '.env.example'; @@ -96,6 +99,14 @@ public function handle(): void $this->info('Project initialized successfully!'); + $this->appType = AppTypeEnum::from( + $this->choice( + question: 'What type of application will your API serve?', + choices: AppTypeEnum::values(), + default: AppTypeEnum::Multiplatform->value + ) + ); + $this->authType = AuthTypeEnum::from($this->choice( question: 'Please choose the authentication type', choices: AuthTypeEnum::values(), @@ -198,7 +209,7 @@ public function handle(): void protected function setAutoDocContactEmail(string $email): void { $config = ArrayFile::open(base_path('config/auto-doc.php')); - + $config->set('info.contact.email', $email); $config->write(); @@ -214,17 +225,19 @@ protected function createAdminUser(string $kebabName): void ]; if ($this->authType === AuthTypeEnum::Clerk) { - $this->publishMigration( + $this->publishView( view: view('initializator::admins_create_table')->with($this->adminCredentials), - migrationName: 'admins_create_table', + viewName: Carbon::now()->format('Y_m_d_His') . '_admins_create_table', + path: 'database/migration', ); } else { $this->adminCredentials['name'] = $this->ask('Please enter an admin name', 'Admin'); $this->adminCredentials['role_id'] = $this->ask('Please enter an admin role id', RoleEnum::Admin->value); - $this->publishMigration( + $this->publishView( view: view('initializator::add_default_user')->with($this->adminCredentials), - migrationName: 'add_default_user' + viewName: Carbon::now()->format('Y_m_d_His') . '_add_default_user', + path: 'database/migration', ); } } @@ -235,13 +248,7 @@ protected function fillReadme(): void $this->setReadmeValue($file, 'project_name', $this->appName); - $type = $this->choice( - question: 'What type of application will your API serve?', - choices: ['Mobile', 'Web', 'Multiplatform'], - default: 'Multiplatform' - ); - - $this->setReadmeValue($file, 'type', $type); + $this->setReadmeValue($file, 'type', $this->appType); $this->readmeContent = $file; } @@ -300,9 +307,9 @@ protected function fillContacts(): void $this->removeTag($filePart, $key); } - + $this->setReadmeValue($filePart, 'team_lead_link', $this->codeOwnerEmail); - + $this->updateReadmeFile($filePart); } @@ -381,15 +388,13 @@ protected function addQuotes($string): string return (Str::contains($string, ' ')) ? "\"{$string}\"" : $string; } - protected function publishMigration(View $view, string $migrationName): void + protected function publishView(View $view, string $viewName, string $path): void { - $time = Carbon::now()->format('Y_m_d_His'); - - $migrationName = "{$time}_{$migrationName}.php"; + $viewName = "{$viewName}.php"; $data = $view->render(); - file_put_contents("database/migrations/{$migrationName}", "updateAuthClerkConfig(); - $this->publishMigration( + $this->publishView( view: view('initializator::users_add_clerk_id_field'), - migrationName: 'users_add_clerk_id_field', + viewName: Carbon::now()->format('Y_m_d_His') . '_users_add_clerk_id_field', + path: 'database/migrations', ); + + $this->publishView( + view: view('initializator::clerk_user_repository'), + viewName: 'ClerkUserRepository', + path: 'app/Support/Clerk', + ); + + $this->addClerkRepositoryBind(); } // TODO: try to use package after fixing https://github.com/wintercms/laravel-config-writer/issues/6 diff --git a/src/Enums/AppTypeEnum.php b/src/Enums/AppTypeEnum.php new file mode 100644 index 0000000..e92872b --- /dev/null +++ b/src/Enums/AppTypeEnum.php @@ -0,0 +1,14 @@ + Date: Sat, 23 Aug 2025 15:55:03 +0300 Subject: [PATCH 2/4] feat: add clerk repository blade --- src/Commands/InitCommand.php | 26 +++++++++++-------- tests/InitCommandTest.php | 8 ++++++ .../InitCommandTest/clerk_user_repository.php | 22 ++++++++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 tests/fixtures/InitCommandTest/clerk_user_repository.php diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 3311aa6..40bac89 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -242,19 +242,17 @@ protected function createAdminUser(string $kebabName): void ]; if ($this->authType === AuthTypeEnum::Clerk) { - $this->publishView( + $this->publishMigration( view: view('initializator::admins_create_table')->with($this->adminCredentials), - viewName: Carbon::now()->format('Y_m_d_His') . '_admins_create_table', - path: 'database/migration', + migrationName: 'admins_create_table', ); } else { $this->adminCredentials['name'] = $this->ask('Please enter an admin name', 'Admin'); $this->adminCredentials['role_id'] = $this->ask('Please enter an admin role id', RoleEnum::Admin->value); - $this->publishView( + $this->publishMigration( view: view('initializator::add_default_user')->with($this->adminCredentials), - viewName: Carbon::now()->format('Y_m_d_His') . '_add_default_user', - path: 'database/migration', + migrationName: 'add_default_user', ); } } @@ -414,6 +412,15 @@ protected function publishView(View $view, string $viewName, string $path): void file_put_contents("{$path}/{$viewName}", "format('Y_m_d_His'); + + $migrationName = "{$time}_{$migrationName}"; + + $this->publishView($view, $migrationName, "database/migrations"); + } + protected function createOrUpdateConfigFile(string $fileName, string $separator, array $data): void { $parsed = file_get_contents($fileName); @@ -543,10 +550,9 @@ protected function enableClerk(): void $this->updateAuthClerkConfig(); - $this->publishView( + $this->publishMigration( view: view('initializator::users_add_clerk_id_field'), - viewName: Carbon::now()->format('Y_m_d_His') . '_users_add_clerk_id_field', - path: 'database/migrations', + migrationName: 'users_add_clerk_id_field', ); $this->publishView( @@ -554,8 +560,6 @@ protected function enableClerk(): void viewName: 'ClerkUserRepository', path: 'app/Support/Clerk', ); - - $this->addClerkRepositoryBind(); } // TODO: try to use package after fixing https://github.com/wintercms/laravel-config-writer/issues/6 diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index eb0b5f7..3004ea2 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -243,6 +243,10 @@ public function testRunWithAdminAndDefaultReadmeCreation() 'database/migrations/2018_11_11_111111_users_add_clerk_id_field.php', $this->getFixture('users_add_clerk_id_field_migration.php'), ], + [ + 'app/Support/Clerk/ClerkUserRepository.php', + $this->getFixture('clerk_user_repository.php'), + ], [ '.env.development', $this->getFixture('env.development_clerk_guard_added.yml'), @@ -834,6 +838,10 @@ public function testRunWithClerkMobileApp(): void 'database/migrations/2018_11_11_111111_users_add_clerk_id_field.php', $this->getFixture('users_add_clerk_id_field_migration.php'), ], + [ + 'app/Support/Clerk/ClerkUserRepository.php', + $this->getFixture('clerk_user_repository.php'), + ], [ '.env.development', $this->getFixture('env.development_clerk_guard_added.yml'), diff --git a/tests/fixtures/InitCommandTest/clerk_user_repository.php b/tests/fixtures/InitCommandTest/clerk_user_repository.php new file mode 100644 index 0000000..d35ab1c --- /dev/null +++ b/tests/fixtures/InitCommandTest/clerk_user_repository.php @@ -0,0 +1,22 @@ + $user->getAuthIdentifier(), + ], [ + 'role_id' => Role::USER, + ]); + } +} \ No newline at end of file From de0b7c795e1323b109b45d1a768b5cee22872c2e Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Thu, 28 Aug 2025 13:27:05 +0300 Subject: [PATCH 3/4] refactor: publish view method --- src/Commands/InitCommand.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index 06fd512..668f4e7 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -128,10 +128,10 @@ public function handle(): void } $this->createOrUpdateConfigFile('.env.development', '=', $data); - $this->createOrUpdateConfigFile($envFile, '=', $data); + $this->createOrUpdateConfigFile('.env.example', '=', $data); - if ($envFile !== '.env.example') { - $this->createOrUpdateConfigFile('.env.example', '=', $data); + if ($envFile === '.env') { + $this->createOrUpdateConfigFile($envFile, '=', $data); } } @@ -400,6 +400,10 @@ protected function publishView(View $view, string $viewName, string $path): void { $viewName = "{$viewName}.php"; + if (!is_dir($path)) { + mkdir($path, 0777, true); + } + $data = $view->render(); file_put_contents("{$path}/{$viewName}", "publishView( + $this-> publishView( view: view('initializator::clerk_user_repository'), viewName: 'ClerkUserRepository', path: 'app/Support/Clerk', From 2570d3151b1f0cb8259c286237e85bd038212054 Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Thu, 4 Sep 2025 11:32:12 +0300 Subject: [PATCH 4/4] style:fix --- src/Commands/InitCommand.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index ac5e194..c07f4f3 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -396,17 +396,17 @@ protected function addQuotes($string): string return (Str::contains($string, ' ')) ? "\"{$string}\"" : $string; } - protected function publishView(View $view, string $viewName, string $path): void + protected function publishClass(View $template, string $fileName, string $filePath): void { - $viewName = "{$viewName}.php"; + $fileName = "{$fileName}.php"; - if (!is_dir($path)) { - mkdir($path, 0777, true); + if (!is_dir($filePath)) { + mkdir($filePath, 0777, true); } - $data = $view->render(); + $data = $template->render(); - file_put_contents("{$path}/{$viewName}", "publishView($view, $migrationName, "database/migrations"); + $this->publishClass($view, $migrationName, 'database/migrations'); } protected function createOrUpdateConfigFile(string $fileName, string $separator, array $data): void @@ -555,10 +555,10 @@ protected function enableClerk(): void migrationName: 'users_add_clerk_id_field', ); - $this-> publishView( - view: view('initializator::clerk_user_repository'), - viewName: 'ClerkUserRepository', - path: 'app/Support/Clerk', + $this->publishClass( + template: view('initializator::clerk_user_repository'), + fileName: 'ClerkUserRepository', + filePath: 'app/Support/Clerk', ); } }