From 87bfbe15e461c6c6e26ca9f8422aca922308dc9e Mon Sep 17 00:00:00 2001 From: ArshiaMohammadei Date: Mon, 26 May 2025 10:23:52 +0330 Subject: [PATCH] Update MigrationCommand.php Refactored command for better structure and maintainability: - Broke down `handle()` into smaller, focused methods - Improved readability with descriptive names and clear comments - Switched to array-driven table handling - Added return types and typed parameters - Extracted file writing and path checks into separate logic - Kept `fire()` for Laravel compatibility - Console output remains unchanged --- src/commands/MigrationCommand.php | 174 ++++++++++++++++++++---------- 1 file changed, 117 insertions(+), 57 deletions(-) diff --git a/src/commands/MigrationCommand.php b/src/commands/MigrationCommand.php index 6310ca35..70158e80 100644 --- a/src/commands/MigrationCommand.php +++ b/src/commands/MigrationCommand.php @@ -1,12 +1,6 @@ -laravel->view->addNamespace('entrust', substr(__DIR__, 0, -8).'views'); + $this->loadViews(); + + $tables = [ + 'roles' => Config::get('entrust.roles_table'), + 'role_user' => Config::get('entrust.role_user_table'), + 'permissions' => Config::get('entrust.permissions_table'), + 'permission_role' => Config::get('entrust.permission_role_table') + ]; + + $this->displayTableInfo($tables); + + if ($this->confirmCreation()) { + $this->createMigrationFile($tables); + } + } - $rolesTable = Config::get('entrust.roles_table'); - $roleUserTable = Config::get('entrust.role_user_table'); - $permissionsTable = Config::get('entrust.permissions_table'); - $permissionRoleTable = Config::get('entrust.permission_role_table'); + /** + * Load the package views. + */ + protected function loadViews(): void + { + $this->laravel->view->addNamespace('entrust', substr(__DIR__, 0, -8).'views'); + } + /** + * Display table information to the user. + */ + protected function displayTableInfo(array $tables): void + { $this->line(''); - $this->info( "Tables: $rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable" ); - - $message = "A migration that creates '$rolesTable', '$roleUserTable', '$permissionsTable', '$permissionRoleTable'". - " tables will be created in database/migrations directory"; - + $this->info('Tables: '.implode(', ', $tables)); + + $message = "A migration that creates '".implode("', '", $tables). + "' tables will be created in database/migrations directory"; + $this->comment($message); $this->line(''); + } - if ($this->confirm("Proceed with the migration creation? [Yes|no]", "Yes")) { - - $this->line(''); - - $this->info("Creating migration..."); - if ($this->createMigration($rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable)) { + /** + * Confirm with user before creating migration. + */ + protected function confirmCreation(): bool + { + return $this->confirm( + "Proceed with the migration creation? [Yes|no]", + "Yes" + ); + } - $this->info("Migration successfully created!"); - } else { - $this->error( - "Couldn't create migration.\n Check the write permissions". - " within the database/migrations directory." - ); - } + /** + * Create the migration file. + */ + protected function createMigrationFile(array $tables): void + { + $this->info("Creating migration..."); + + if ($this->createMigration( + $tables['roles'], + $tables['role_user'], + $tables['permissions'], + $tables['permission_role'] + )) { + $this->info("Migration successfully created!"); + } else { + $this->error( + "Couldn't create migration.\nCheck the write permissions ". + "within the database/migrations directory." + ); + } + + $this->line(''); + } - $this->line(''); + /** + * Generate the migration file content. + */ + protected function createMigration( + string $rolesTable, + string $roleUserTable, + string $permissionsTable, + string $permissionRoleTable + ): bool { + $migrationPath = $this->getMigrationPath(); + $data = $this->getMigrationData($rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable); + + $output = $this->laravel->view->make('entrust::generators.migration')->with($data)->render(); - } + return $this->writeMigrationFile($migrationPath, $output); } /** - * Create the migration. - * - * @param string $name - * - * @return bool + * Get the path for the migration file. */ - protected function createMigration($rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable) + protected function getMigrationPath(): string { - $migrationFile = base_path("/database/migrations")."/".date('Y_m_d_His')."_entrust_setup_tables.php"; + return database_path('migrations/'.date('Y_m_d_His').'_entrust_setup_tables.php'); + } + /** + * Prepare data for the migration template. + */ + protected function getMigrationData( + string $rolesTable, + string $roleUserTable, + string $permissionsTable, + string $permissionRoleTable + ): array { $userModelName = Config::get('auth.providers.users.model'); $userModel = new $userModelName(); - $usersTable = $userModel->getTable(); - $userKeyName = $userModel->getKeyName(); - - $data = compact('rolesTable', 'roleUserTable', 'permissionsTable', 'permissionRoleTable', 'usersTable', 'userKeyName'); - - $output = $this->laravel->view->make('entrust::generators.migration')->with($data)->render(); + + return [ + 'rolesTable' => $rolesTable, + 'roleUserTable' => $roleUserTable, + 'permissionsTable' => $permissionsTable, + 'permissionRoleTable' => $permissionRoleTable, + 'usersTable' => $userModel->getTable(), + 'userKeyName' => $userModel->getKeyName() + ]; + } - if (!file_exists($migrationFile) && $fs = fopen($migrationFile, 'x')) { - fwrite($fs, $output); + /** + * Write the migration file to disk. + */ + protected function writeMigrationFile(string $path, string $content): bool + { + if (!file_exists($path) && $fs = fopen($path, 'x')) { + fwrite($fs, $content); fclose($fs); return true; } - + return false; } }